Complex classes like MapAPI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MapAPI, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class MapAPI extends ViewableData |
||
| 18 | { |
||
| 19 | |||
| 20 | /** GoogleMap key **/ |
||
| 21 | protected $googleMapKey = ''; |
||
| 22 | |||
| 23 | /** GoogleMap ID for the HTML DIV **/ |
||
| 24 | protected $googleMapId = 'googlemapapi'; |
||
| 25 | |||
| 26 | /* Additional CSS classes to use when rendering the map */ |
||
| 27 | protected $set_additional_css_classes = ''; |
||
| 28 | |||
| 29 | /** Width of the gmap **/ |
||
| 30 | protected $width = 800; |
||
| 31 | |||
| 32 | /** Height of the gmap **/ |
||
| 33 | protected $height = 600; |
||
| 34 | |||
| 35 | /** Icon width of the gmarker **/ |
||
| 36 | protected $iconWidth = 20; |
||
| 37 | |||
| 38 | /** Icon height of the gmarker **/ |
||
| 39 | protected $iconHeight = 34; |
||
| 40 | |||
| 41 | /* array of lines to be drawn on the map */ |
||
| 42 | protected $lines = array(); |
||
| 43 | |||
| 44 | /* kml file to be rendered */ |
||
| 45 | protected $kmlFiles = array(); |
||
| 46 | |||
| 47 | /** Default zoom of the gmap **/ |
||
| 48 | protected $zoom = 9; |
||
| 49 | |||
| 50 | /** Enable the zoom of the Infowindow **/ |
||
| 51 | protected $enableWindowZoom = false; |
||
| 52 | |||
| 53 | /** Default zoom of the Infowindow **/ |
||
| 54 | protected $infoWindowZoom = 13; |
||
| 55 | |||
| 56 | /** Lang of the gmap **/ |
||
| 57 | protected $lang = 'en'; |
||
| 58 | |||
| 59 | /**Center of the gmap **/ |
||
| 60 | protected $center = 'Paris, France'; |
||
| 61 | |||
| 62 | /* |
||
| 63 | Additional CSS classes to render as a class attribute for the div of the |
||
| 64 | map. Use this if you want more fine grained control over your map using |
||
| 65 | CSS. If blank it will be ignored |
||
| 66 | */ |
||
| 67 | protected $additional_css_classes = ''; |
||
| 68 | |||
| 69 | |||
| 70 | /* Decided whether or not to show the inline map css style on div creation */ |
||
| 71 | protected $show_inline_map_div_style = true; |
||
| 72 | |||
| 73 | protected $latLongCenter = null; |
||
| 74 | |||
| 75 | protected $jsonMapStyles = '[]'; |
||
| 76 | |||
| 77 | protected $delayLoadMapFunction = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Type of the gmap, can be: |
||
| 81 | * 'road' (roadmap), |
||
| 82 | * 'satellite' (sattelite/aerial photographs) |
||
| 83 | * 'hybrid' (hybrid of road and satellite) |
||
| 84 | * 'terrain' (terrain) |
||
| 85 | * The JavaScript for the mapping service will convert this into a suitable mapping type |
||
| 86 | */ |
||
| 87 | |||
| 88 | protected $mapType = 'road'; |
||
| 89 | |||
| 90 | |||
| 91 | /** Content of the HTML generated **/ |
||
| 92 | protected $content = ''; |
||
| 93 | |||
| 94 | protected $mapService = 'google'; |
||
| 95 | |||
| 96 | /** Hide the marker by default **/ |
||
| 97 | protected $defaultHideMarker = false; |
||
| 98 | |||
| 99 | /** Extra content (marker, etc...) **/ |
||
| 100 | protected $contentMarker = ''; |
||
| 101 | |||
| 102 | // a list of markers, markers being associative arrays |
||
| 103 | protected $markers = array(); |
||
| 104 | |||
| 105 | /** Use clusterer to display a lot of markers on the gmap **/ |
||
| 106 | protected $useClusterer = false; |
||
| 107 | protected $gridSize = 50; |
||
| 108 | protected $maxZoom = 17; |
||
| 109 | protected $clustererLibraryPath = "/mappable/javascript/google/markerclusterer.js"; |
||
| 110 | |||
| 111 | /** Enable automatic center/zoom **/ |
||
| 112 | protected $enableAutomaticCenterZoom = false; |
||
| 113 | |||
| 114 | /** maximum longitude of all markers **/ |
||
| 115 | protected $maxLng = -1000000; |
||
| 116 | |||
| 117 | /** minimum longitude of all markers **/ |
||
| 118 | protected $minLng = 1000000; |
||
| 119 | |||
| 120 | /** max latitude of all markers **/ |
||
| 121 | protected $maxLat = -1000000; |
||
| 122 | |||
| 123 | /** min latitude of all markers **/ |
||
| 124 | protected $minLat = 1000000; |
||
| 125 | |||
| 126 | /** map center latitude (horizontal), calculated automatically as markers |
||
| 127 | are added to the map **/ |
||
| 128 | protected $centerLat = null; |
||
| 129 | |||
| 130 | /** map center longitude (vertical), calculated automatically as markers |
||
| 131 | are added to the map **/ |
||
| 132 | protected $centerLng = null; |
||
| 133 | |||
| 134 | /** factor by which to fudge the boundaries so that when we zoom encompass, |
||
| 135 | the markers aren't too close to the edge **/ |
||
| 136 | protected $coordCoef = 0.01; |
||
| 137 | |||
| 138 | /* set this to true to render button to maximize / minimize a map */ |
||
| 139 | protected $allowFullScreen = null; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Class constructor |
||
| 143 | * |
||
| 144 | * @param string $googleMapKey the googleMapKey |
||
| 145 | */ |
||
| 146 | |||
| 147 | public function __construct($googleMapKey = '') { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Set the key of the gmap |
||
| 153 | * |
||
| 154 | * @param string $googleMapKey the googleMapKey |
||
| 155 | * |
||
| 156 | * @return void |
||
| 157 | */ |
||
| 158 | |||
| 159 | public function setKey($googleMapKey) { |
||
| 163 | |||
| 164 | public function setShowInlineMapDivStyle($new_show_inline_map_div_style) { |
||
| 168 | |||
| 169 | public function setAdditionalCSSClasses($new_additional_css_classes) { |
||
| 173 | |||
| 174 | |||
| 175 | public function setMapStyle($newStyles) { |
||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | public function setDelayLoadMapFunction($newDelay) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set the useClusterer parameter (optimization to display a lot of marker) |
||
| 189 | * |
||
| 190 | * @param boolean $useClusterer use cluster or not |
||
| 191 | * @param int $gridSize grid size |
||
| 192 | * @param int $maxZoom max zoom to cluster at |
||
| 193 | * |
||
| 194 | * * @return MapAPI This same object, in order to enable chaining of methods |
||
| 195 | */ |
||
| 196 | |||
| 197 | public function setClusterer($useClusterer, $gridSize = 50, $maxZoom = 17, |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set the ID of the default gmap DIV |
||
| 208 | * |
||
| 209 | * @param string $googleMapId the google div ID |
||
| 210 | * |
||
| 211 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 212 | */ |
||
| 213 | |||
| 214 | public function setDivId($googleMapId) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Set the size of the gmap. If these values are not provided |
||
| 221 | * then CSS is used instead |
||
| 222 | * |
||
| 223 | * @param int $width GoogleMap width |
||
| 224 | * @param int $height GoogleMap height |
||
| 225 | * |
||
| 226 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 227 | */ |
||
| 228 | |||
| 229 | public function setSize($width, $height) { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * Set the size of the icon markers |
||
| 238 | * |
||
| 239 | * @param int $iconWidth GoogleMap marker icon width |
||
| 240 | * @param int $iconHeight GoogleMap marker icon height |
||
| 241 | * |
||
| 242 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 243 | */ |
||
| 244 | |||
| 245 | public function setIconSize($iconWidth, $iconHeight) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set the lang of the gmap |
||
| 253 | * |
||
| 254 | * @param string $lang GoogleMap lang : fr,en,.. |
||
| 255 | * |
||
| 256 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 257 | */ |
||
| 258 | |||
| 259 | public function setLang($lang) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Set the zoom of the gmap |
||
| 266 | * |
||
| 267 | * @param int $zoom GoogleMap zoom. |
||
| 268 | * |
||
| 269 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 270 | */ |
||
| 271 | |||
| 272 | public function setZoom($zoom) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Set the zoom of the infowindow |
||
| 279 | * |
||
| 280 | * @param int $infoWindowZoom GoogleMap information window zoom. |
||
| 281 | * |
||
| 282 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 283 | */ |
||
| 284 | |||
| 285 | public function setInfoWindowZoom($infoWindowZoom) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Enable the zoom on the marker when you click on it |
||
| 292 | * |
||
| 293 | * @param int $enableWindowZoom info window enabled zoom. |
||
| 294 | * |
||
| 295 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 296 | */ |
||
| 297 | |||
| 298 | public function setEnableWindowZoom($enableWindowZoom) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Enable theautomatic center/zoom at the gmap load |
||
| 305 | * |
||
| 306 | * @param int $enableAutomaticCenterZoom enable automatic centre zoom |
||
| 307 | * |
||
| 308 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 309 | */ |
||
| 310 | |||
| 311 | public function setEnableAutomaticCenterZoom($enableAutomaticCenterZoom) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Set the center of the gmap (an address) |
||
| 318 | * |
||
| 319 | * @param string $center GoogleMap center (an address) |
||
| 320 | * |
||
| 321 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 322 | */ |
||
| 323 | |||
| 324 | public function setCenter($center) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set the type of the gmap. Also takes into account legacy settings |
||
| 331 | * |
||
| 332 | * FIXME - allow other valid settings in config for map type |
||
| 333 | * |
||
| 334 | * @param string $mapType Can be one of road,satellite,hybrid or terrain. Defaults to road |
||
| 335 | * |
||
| 336 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 337 | */ |
||
| 338 | |||
| 339 | public function setMapType($mapType) { |
||
| 360 | |||
| 361 | /* |
||
| 362 | Set whether or not to allow the full screen tools |
||
| 363 | @return MapAPI This same object, in order to enable chaining of methods |
||
| 364 | */ |
||
| 365 | public function setAllowFullScreen($allowed) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set the center of the gmap |
||
| 372 | * |
||
| 373 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 374 | **/ |
||
| 375 | public function setLatLongCenter($center) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set the defaultHideMarker |
||
| 394 | * |
||
| 395 | * @param boolean $defaultHideMarker hide all the markers on the map by default |
||
| 396 | * |
||
| 397 | * @return void |
||
| 398 | */ |
||
| 399 | |||
| 400 | public function setDefaultHideMarker($defaultHideMarker) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Get the google map content |
||
| 407 | * |
||
| 408 | * @return string the google map html code |
||
| 409 | */ |
||
| 410 | |||
| 411 | public function getGoogleMap() { |
||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * Get URL content using cURL. |
||
| 418 | * |
||
| 419 | * @param string $url the url |
||
| 420 | * |
||
| 421 | * @return string the html code |
||
| 422 | * |
||
| 423 | * @todo add proxy settings |
||
| 424 | */ |
||
| 425 | |||
| 426 | public function getContent($url) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Geocoding an address (address -> lat,lng) |
||
| 439 | * |
||
| 440 | * @param string $address an address |
||
| 441 | * |
||
| 442 | * @return array array with precision, lat & lng |
||
| 443 | */ |
||
| 444 | |||
| 445 | public function geocoding($address) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Add marker by his coord |
||
| 464 | * |
||
| 465 | * @param string $lat lat |
||
| 466 | * @param string $lng lngs |
||
| 467 | * @param string $html html code display in the info window |
||
| 468 | * @param string $category marker category |
||
| 469 | * @param string $icon an icon url |
||
| 470 | * |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | |||
| 474 | public function addMarkerByCoords($lat, $lng, $html = '', $category = '', $icon = '') { |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * Add marker by his address |
||
| 489 | * |
||
| 490 | * @param string $address an ddress |
||
| 491 | * @param string $content html code display in the info window |
||
| 492 | * @param string $category marker category |
||
| 493 | * @param string $icon an icon url |
||
| 494 | * |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | |||
| 498 | public function addMarkerByAddress($address, $content = '', $category = '', $icon = '') { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Add marker by an array of coord |
||
| 508 | * |
||
| 509 | * @param array $coordtab an array of lat,lng,content |
||
| 510 | * @param string $category marker category |
||
| 511 | * @param string $icon an icon url |
||
| 512 | * |
||
| 513 | * @return void |
||
| 514 | */ |
||
| 515 | |||
| 516 | public function addArrayMarkerByCoords($coordtab, $category = '', $icon = '') { |
||
| 522 | |||
| 523 | |||
| 524 | /** |
||
| 525 | * Adds a {@link ViewableData} object that implements {@link Mappable} |
||
| 526 | * to the map. |
||
| 527 | * @param $infowindowtemplateparams Optional array of extra parameters to pass to the map info window |
||
| 528 | * |
||
| 529 | * @param ViewableData $obj |
||
| 530 | */ |
||
| 531 | public function addMarkerAsObject(ViewableData $obj, $infowindowtemplateparams = null) { |
||
| 565 | |||
| 566 | |||
| 567 | /** |
||
| 568 | * Draws a line between two {@link ViewableData} objects |
||
| 569 | * |
||
| 570 | * @param ViewableData $one The first point |
||
| 571 | * @param ViewableData $two The second point |
||
| 572 | * @param string $color The hexidecimal color of the line |
||
| 573 | */ |
||
| 574 | public function connectPoints(ViewableData $one, ViewableData $two, $color = "#FF3300") { |
||
| 581 | |||
| 582 | |||
| 583 | public function forTemplate() { |
||
| 587 | |||
| 588 | |||
| 589 | /** |
||
| 590 | * Add marker by an array of address |
||
| 591 | * |
||
| 592 | * @param array $coordtab an array of address |
||
| 593 | * @param string $category marker category |
||
| 594 | * @param string $icon an icon url |
||
| 595 | * |
||
| 596 | * @return void |
||
| 597 | */ |
||
| 598 | |||
| 599 | public function addArrayMarkerByAddress($coordtab, $category = '', $icon = '') { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Parse a KML file and add markers to a category |
||
| 608 | * |
||
| 609 | * @param string $url url of the kml file compatible with gmap and gearth |
||
| 610 | * |
||
| 611 | * @return void |
||
| 612 | */ |
||
| 613 | |||
| 614 | public function addKML($url) { |
||
| 618 | |||
| 619 | |||
| 620 | /* |
||
| 621 | Add a line to the map |
||
| 622 | |||
| 623 | */ |
||
| 624 | public function addLine($from = array(), $to = array(), $color = "#FF3300") { |
||
| 636 | |||
| 637 | |||
| 638 | /* |
||
| 639 | For php 5.3 |
||
| 640 | */ |
||
| 641 | private function jsonRemoveUnicodeSequences($struct) { |
||
| 646 | |||
| 647 | |||
| 648 | /** |
||
| 649 | * Generate the gmap |
||
| 650 | * |
||
| 651 | * @return void |
||
| 652 | */ |
||
| 653 | |||
| 654 | public function generate() { |
||
| 757 | |||
| 758 | function processTemplateHTML($templateName, $templateVariables = null) { |
||
| 766 | } |
||
| 767 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.