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 | /** GoogleMap key **/ |
||
| 20 | protected $googleMapKey = ''; |
||
| 21 | |||
| 22 | /** GoogleMap ID for the HTML DIV **/ |
||
| 23 | protected $googleMapId = 'googlemapapi'; |
||
| 24 | |||
| 25 | /* Additional CSS classes to use when rendering the map */ |
||
| 26 | protected $set_additional_css_classes = ''; |
||
| 27 | |||
| 28 | /** Width of the gmap **/ |
||
| 29 | protected $width = 800; |
||
| 30 | |||
| 31 | /** Height of the gmap **/ |
||
| 32 | protected $height = 600; |
||
| 33 | |||
| 34 | /* array of lines to be drawn on the map */ |
||
| 35 | protected $lines = array(); |
||
| 36 | |||
| 37 | /* kml file to be rendered */ |
||
| 38 | protected $kmlFiles = array(); |
||
| 39 | |||
| 40 | /** Default zoom of the gmap **/ |
||
| 41 | protected $zoom = 9; |
||
| 42 | |||
| 43 | /** Enable the zoom of the Infowindow **/ |
||
| 44 | protected $enableWindowZoom = false; |
||
| 45 | |||
| 46 | /** Default zoom of the Infowindow **/ |
||
| 47 | protected $infoWindowZoom = 13; |
||
| 48 | |||
| 49 | /** Lang of the gmap **/ |
||
| 50 | protected $lang = 'en'; |
||
| 51 | |||
| 52 | /**Center of the gmap **/ |
||
| 53 | protected $center = 'Paris, France'; |
||
| 54 | |||
| 55 | /* |
||
| 56 | Additional CSS classes to render as a class attribute for the div of the |
||
| 57 | map. Use this if you want more fine grained control over your map using |
||
| 58 | CSS. If blank it will be ignored |
||
| 59 | */ |
||
| 60 | protected $additional_css_classes = ''; |
||
| 61 | |||
| 62 | /* Decided whether or not to show the inline map css style on div creation */ |
||
| 63 | protected $show_inline_map_div_style = true; |
||
| 64 | |||
| 65 | protected $latLongCenter = null; |
||
| 66 | |||
| 67 | protected $jsonMapStyles = '[]'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Type of the gmap, can be: |
||
| 71 | * 'road' (roadmap), |
||
| 72 | * 'satellite' (sattelite/aerial photographs) |
||
| 73 | * 'hybrid' (hybrid of road and satellite) |
||
| 74 | * 'terrain' (terrain) |
||
| 75 | * The JavaScript for the mapping service will convert this into a suitable mapping type. |
||
| 76 | */ |
||
| 77 | protected $mapType = 'road'; |
||
| 78 | |||
| 79 | /** Content of the HTML generated **/ |
||
| 80 | protected $content = ''; |
||
| 81 | |||
| 82 | protected $mapService = 'google'; |
||
| 83 | |||
| 84 | /** Hide the marker by default **/ |
||
| 85 | protected $defaultHideMarker = false; |
||
| 86 | |||
| 87 | /** Extra content (marker, etc...) **/ |
||
| 88 | protected $contentMarker = ''; |
||
| 89 | |||
| 90 | // a list of markers, markers being associative arrays |
||
| 91 | protected $markers = array(); |
||
| 92 | |||
| 93 | /** Use clusterer to display a lot of markers on the gmap **/ |
||
| 94 | protected $useClusterer = false; |
||
| 95 | protected $gridSize = 50; |
||
| 96 | protected $maxZoom = 17; |
||
| 97 | protected $clustererLibraryPath = '/mappable/javascript/google/markerclusterer.js'; |
||
| 98 | |||
| 99 | /** Enable automatic center/zoom **/ |
||
| 100 | protected $enableAutomaticCenterZoom = false; |
||
| 101 | |||
| 102 | /** maximum longitude of all markers **/ |
||
| 103 | protected $maxLng = -1000000; |
||
| 104 | |||
| 105 | /** minimum longitude of all markers **/ |
||
| 106 | protected $minLng = 1000000; |
||
| 107 | |||
| 108 | /** max latitude of all markers **/ |
||
| 109 | protected $maxLat = -1000000; |
||
| 110 | |||
| 111 | /** min latitude of all markers **/ |
||
| 112 | protected $minLat = 1000000; |
||
| 113 | |||
| 114 | /** map center latitude (horizontal), calculated automatically as markers |
||
| 115 | are added to the map **/ |
||
| 116 | protected $centerLat = null; |
||
| 117 | |||
| 118 | /** map center longitude (vertical), calculated automatically as markers |
||
| 119 | are added to the map **/ |
||
| 120 | protected $centerLng = null; |
||
| 121 | |||
| 122 | /** factor by which to fudge the boundaries so that when we zoom encompass, |
||
| 123 | the markers aren't too close to the edge **/ |
||
| 124 | protected $coordCoef = 0.01; |
||
| 125 | |||
| 126 | /* set this to true to render button to maximize / minimize a map */ |
||
| 127 | protected $allowFullScreen = null; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Class constructor. |
||
| 131 | * |
||
| 132 | * @param string $googleMapKey the googleMapKey |
||
| 133 | */ |
||
| 134 | 41 | public function __construct($googleMapKey = '') |
|
| 138 | |||
| 139 | 3 | public function setShowInlineMapDivStyle($new_show_inline_map_div_style) |
|
| 145 | |||
| 146 | 3 | public function setAdditionalCSSClasses($new_additional_css_classes) |
|
| 152 | |||
| 153 | 1 | public function setMapStyle($newStyles) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Set the useClusterer parameter (optimization to display a lot of marker). |
||
| 162 | * |
||
| 163 | * @param bool $useClusterer use cluster or not |
||
| 164 | * @param int $gridSize grid size |
||
| 165 | * @param int $maxZoom max zoom to cluster at |
||
| 166 | * |
||
| 167 | * * @return MapAPI This same object, in order to enable chaining of methods |
||
| 168 | */ |
||
| 169 | 1 | public function setClusterer( |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Set the ID of the default gmap DIV. |
||
| 185 | * |
||
| 186 | * @param string $googleMapId the google div ID |
||
| 187 | 41 | * |
|
| 188 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 189 | 41 | */ |
|
| 190 | public function setDivId($googleMapId) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Set the size of the gmap. If these values are not provided |
||
| 199 | * then CSS is used instead. |
||
| 200 | * |
||
| 201 | * @param int $width GoogleMap width |
||
| 202 | * @param int $height GoogleMap height |
||
| 203 | 41 | * |
|
| 204 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 205 | 41 | */ |
|
| 206 | 41 | public function setSize($width, $height) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Set the lang of the gmap. |
||
| 216 | * |
||
| 217 | * @param string $lang GoogleMap lang : fr,en,.. |
||
| 218 | 41 | * |
|
| 219 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 220 | 41 | */ |
|
| 221 | public function setLang($lang) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Set the zoom of the gmap. |
||
| 230 | * |
||
| 231 | * @param int $zoom GoogleMap zoom. |
||
| 232 | 32 | * |
|
| 233 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 234 | 32 | */ |
|
| 235 | public function setZoom($zoom) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Set the zoom of the infowindow. |
||
| 244 | * |
||
| 245 | * @param int $infoWindowZoom GoogleMap information window zoom. |
||
| 246 | 1 | * |
|
| 247 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 248 | 1 | */ |
|
| 249 | public function setInfoWindowZoom($infoWindowZoom) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Enable the zoom on the marker when you click on it. |
||
| 258 | * |
||
| 259 | * @param bool $enableWindowZoom info window enabled zoom. |
||
| 260 | 1 | * |
|
| 261 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 262 | 1 | */ |
|
| 263 | public function setEnableWindowZoom($enableWindowZoom) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Enable theautomatic center/zoom at the gmap load. |
||
| 272 | * |
||
| 273 | * @param bool $enableAutomaticCenterZoom enable automatic centre zoom |
||
| 274 | 41 | * |
|
| 275 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 276 | 41 | */ |
|
| 277 | public function setEnableAutomaticCenterZoom($enableAutomaticCenterZoom) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Set the center of the gmap (an address). |
||
| 286 | * |
||
| 287 | * @param string $center GoogleMap center (an address) |
||
| 288 | 41 | * |
|
| 289 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 290 | 41 | */ |
|
| 291 | public function setCenter($center) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Set the type of the gmap. Also takes into account legacy settings. |
||
| 300 | * |
||
| 301 | * FIXME - allow other valid settings in config for map type |
||
| 302 | * |
||
| 303 | * @param string $mapType Can be one of road,satellite,hybrid or terrain. Defaults to road |
||
| 304 | 41 | * |
|
| 305 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 306 | 41 | */ |
|
| 307 | public function setMapType($mapType) |
||
| 329 | |||
| 330 | /* |
||
| 331 | 41 | Set whether or not to allow the full screen tools |
|
| 332 | @return MapAPI This same object, in order to enable chaining of methods |
||
| 333 | 41 | */ |
|
| 334 | public function setAllowFullScreen($allowed) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Set the center of the gmap. |
||
| 343 | 5 | * |
|
| 344 | * @return MapAPI This same object, in order to enable chaining of methods |
||
| 345 | **/ |
||
| 346 | public function setLatLongCenter($center) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Set the defaultHideMarker. |
||
| 367 | * |
||
| 368 | * @param bool $defaultHideMarker hide all the markers on the map by default |
||
| 369 | 41 | * |
|
| 370 | * @return MapAPI |
||
| 371 | 41 | */ |
|
| 372 | public function setDefaultHideMarker($defaultHideMarker) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get the google map content. |
||
| 381 | 37 | * |
|
| 382 | * @return string the google map html code |
||
| 383 | 37 | */ |
|
| 384 | public function getGoogleMap() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get URL content using cURL. |
||
| 391 | * |
||
| 392 | * @param string $url the url |
||
| 393 | * |
||
| 394 | * @return string the html code |
||
| 395 | 1 | * |
|
| 396 | * @todo add proxy settings |
||
| 397 | 1 | */ |
|
| 398 | 1 | public function getContent($url) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Geocoding an address (address -> lat,lng). |
||
| 413 | * |
||
| 414 | * @param string $address an address |
||
| 415 | 34 | * |
|
| 416 | * @return string array with precision, lat & lng |
||
| 417 | 34 | */ |
|
| 418 | 34 | public function geocoding($address) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Add marker by his coord. |
||
| 440 | * |
||
| 441 | * @param string $lat lat |
||
| 442 | * @param string $lng lngs |
||
| 443 | * @param string $html html code display in the info window |
||
| 444 | * @param string $category marker category |
||
| 445 | * @param string $icon an icon url |
||
| 446 | 13 | * |
|
| 447 | * @return MapAPI |
||
| 448 | */ |
||
| 449 | 13 | public function addMarkerByCoords($lat, $lng, $html = '', $category = '', $icon = '') |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Add marker by his address. |
||
| 465 | * |
||
| 466 | * @param string $address an ddress |
||
| 467 | * @param string $content html code display in the info window |
||
| 468 | * @param string $category marker category |
||
| 469 | * @param string $icon an icon url |
||
| 470 | 1 | * |
|
| 471 | * @return MapAPI |
||
| 472 | 1 | */ |
|
| 473 | 1 | public function addMarkerByAddress($address, $content = '', $category = '', $icon = '') |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Add marker by an array of coord. |
||
| 485 | * |
||
| 486 | * @param array $coordtab an array of lat,lng,content |
||
| 487 | * @param string $category marker category |
||
| 488 | * @param string $icon an icon url |
||
| 489 | 1 | * |
|
| 490 | * @return MapAPI |
||
| 491 | 1 | */ |
|
| 492 | 1 | public function addArrayMarkerByCoords($coordtab, $category = '', $icon = '') |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Adds a {@link ViewableData} object that implements {@link Mappable} |
||
| 503 | * to the map. |
||
| 504 | * |
||
| 505 | 10 | * @param $infowindowtemplateparams Optional array of extra parameters to pass to the map info window |
|
| 506 | * @param ViewableData $obj |
||
| 507 | 10 | */ |
|
| 508 | 10 | public function addMarkerAsObject(ViewableData $obj, $infowindowtemplateparams = null) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Draws a line between two {@link ViewableData} objects. |
||
| 545 | * |
||
| 546 | * @param ViewableData $one The first point |
||
| 547 | 1 | * @param ViewableData $two The second point |
|
| 548 | * @param string $color The hexidecimal color of the line |
||
| 549 | 1 | */ |
|
| 550 | 1 | public function connectPoints(ViewableData $one, ViewableData $two, $color = '#FF3300') |
|
| 558 | 37 | ||
| 559 | 37 | public function forTemplate() |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Add a KML file which will be rendered on this map. Normally used for likes |
||
| 569 | * of GPS traces from activities. |
||
| 570 | * |
||
| 571 | * @param string $url url of the kml file compatible with gmap and gearth |
||
| 572 | 2 | * |
|
| 573 | * @return MapAPI |
||
| 574 | 2 | */ |
|
| 575 | public function addKML($url) |
||
| 581 | |||
| 582 | /* |
||
| 583 | 2 | Add a line to the map |
|
| 584 | |||
| 585 | */ |
||
| 586 | 2 | public function addLine($from = array(), $to = array(), $color = '#FF3300') |
|
| 600 | |||
| 601 | /* |
||
| 602 | For php 5.3 |
||
| 603 | */ |
||
| 604 | public static function jsonRemoveUnicodeSequences($struct) |
||
| 612 | |||
| 613 | /** |
||
| 614 | 37 | * Generate the gmap. |
|
| 615 | 37 | */ |
|
| 616 | 37 | public function generate() |
|
| 725 | |||
| 726 | 38 | /** |
|
| 727 | 1 | * @param string $templateName |
|
| 728 | 1 | * @param ArrayData $templateVariables |
|
| 729 | 38 | */ |
|
| 730 | 38 | public function processTemplateHTML($templateName, $templateVariables = null) |
|
| 740 | } |
||
| 741 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.