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 | /* array of lines to be drawn on the map */ |
||
36 | protected $lines = array(); |
||
37 | |||
38 | /* kml file to be rendered */ |
||
39 | protected $kmlFiles = array(); |
||
40 | |||
41 | /** Default zoom of the gmap **/ |
||
42 | protected $zoom = 9; |
||
43 | |||
44 | /** Enable the zoom of the Infowindow **/ |
||
45 | protected $enableWindowZoom = false; |
||
46 | |||
47 | /** Default zoom of the Infowindow **/ |
||
48 | protected $infoWindowZoom = 13; |
||
49 | |||
50 | /** Lang of the gmap **/ |
||
51 | protected $lang = 'en'; |
||
52 | |||
53 | /**Center of the gmap **/ |
||
54 | protected $center = 'Paris, France'; |
||
55 | |||
56 | /* |
||
57 | Additional CSS classes to render as a class attribute for the div of the |
||
58 | map. Use this if you want more fine grained control over your map using |
||
59 | CSS. If blank it will be ignored |
||
60 | */ |
||
61 | protected $additional_css_classes = ''; |
||
62 | |||
63 | |||
64 | /* Decided whether or not to show the inline map css style on div creation */ |
||
65 | protected $show_inline_map_div_style = true; |
||
66 | |||
67 | protected $latLongCenter = null; |
||
68 | |||
69 | protected $jsonMapStyles = '[]'; |
||
70 | |||
71 | protected $delayLoadMapFunction = false; |
||
72 | |||
73 | /** |
||
74 | * Type of the gmap, can be: |
||
75 | * 'road' (roadmap), |
||
76 | * 'satellite' (sattelite/aerial photographs) |
||
77 | * 'hybrid' (hybrid of road and satellite) |
||
78 | * 'terrain' (terrain) |
||
79 | * The JavaScript for the mapping service will convert this into a suitable mapping type |
||
80 | */ |
||
81 | |||
82 | protected $mapType = 'road'; |
||
83 | |||
84 | |||
85 | /** Content of the HTML generated **/ |
||
86 | protected $content = ''; |
||
87 | |||
88 | protected $mapService = 'google'; |
||
89 | |||
90 | /** Hide the marker by default **/ |
||
91 | protected $defaultHideMarker = false; |
||
92 | |||
93 | /** Extra content (marker, etc...) **/ |
||
94 | protected $contentMarker = ''; |
||
95 | |||
96 | // a list of markers, markers being associative arrays |
||
97 | protected $markers = array(); |
||
98 | |||
99 | /** Use clusterer to display a lot of markers on the gmap **/ |
||
100 | protected $useClusterer = false; |
||
101 | protected $gridSize = 50; |
||
102 | protected $maxZoom = 17; |
||
103 | protected $clustererLibraryPath = "/mappable/javascript/google/markerclusterer.js"; |
||
104 | |||
105 | /** Enable automatic center/zoom **/ |
||
106 | protected $enableAutomaticCenterZoom = false; |
||
107 | |||
108 | /** maximum longitude of all markers **/ |
||
109 | protected $maxLng = -1000000; |
||
110 | |||
111 | /** minimum longitude of all markers **/ |
||
112 | protected $minLng = 1000000; |
||
113 | |||
114 | /** max latitude of all markers **/ |
||
115 | protected $maxLat = -1000000; |
||
116 | |||
117 | /** min latitude of all markers **/ |
||
118 | protected $minLat = 1000000; |
||
119 | |||
120 | /** map center latitude (horizontal), calculated automatically as markers |
||
121 | are added to the map **/ |
||
122 | protected $centerLat = null; |
||
123 | |||
124 | /** map center longitude (vertical), calculated automatically as markers |
||
125 | are added to the map **/ |
||
126 | protected $centerLng = null; |
||
127 | |||
128 | /** factor by which to fudge the boundaries so that when we zoom encompass, |
||
129 | the markers aren't too close to the edge **/ |
||
130 | protected $coordCoef = 0.01; |
||
131 | |||
132 | /* set this to true to render button to maximize / minimize a map */ |
||
133 | protected $allowFullScreen = null; |
||
134 | |||
135 | /** |
||
136 | * Class constructor |
||
137 | * |
||
138 | * @param string $googleMapKey the googleMapKey |
||
139 | */ |
||
140 | |||
141 | public function __construct($googleMapKey = '') { |
||
144 | |||
145 | /** |
||
146 | * Set the key of the gmap |
||
147 | * |
||
148 | * @param string $googleMapKey the googleMapKey |
||
149 | * |
||
150 | * @return MapAPI |
||
151 | */ |
||
152 | |||
153 | public function setKey($googleMapKey) { |
||
157 | |||
158 | public function setShowInlineMapDivStyle($new_show_inline_map_div_style) { |
||
162 | |||
163 | public function setAdditionalCSSClasses($new_additional_css_classes) { |
||
167 | |||
168 | |||
169 | public function setMapStyle($newStyles) { |
||
173 | |||
174 | |||
175 | |||
176 | public function setDelayLoadMapFunction($newDelay) { |
||
180 | |||
181 | /** |
||
182 | * Set the useClusterer parameter (optimization to display a lot of marker) |
||
183 | * |
||
184 | * @param boolean $useClusterer use cluster or not |
||
185 | * @param int $gridSize grid size |
||
186 | * @param int $maxZoom max zoom to cluster at |
||
187 | * |
||
188 | * * @return MapAPI This same object, in order to enable chaining of methods |
||
189 | */ |
||
190 | |||
191 | public function setClusterer($useClusterer, $gridSize = 50, $maxZoom = 17, |
||
199 | |||
200 | /** |
||
201 | * Set the ID of the default gmap DIV |
||
202 | * |
||
203 | * @param string $googleMapId the google div ID |
||
204 | * |
||
205 | * @return MapAPI This same object, in order to enable chaining of methods |
||
206 | */ |
||
207 | |||
208 | public function setDivId($googleMapId) { |
||
212 | |||
213 | /** |
||
214 | * Set the size of the gmap. If these values are not provided |
||
215 | * then CSS is used instead |
||
216 | * |
||
217 | * @param int $width GoogleMap width |
||
218 | * @param int $height GoogleMap height |
||
219 | * |
||
220 | * @return MapAPI This same object, in order to enable chaining of methods |
||
221 | */ |
||
222 | |||
223 | public function setSize($width, $height) { |
||
228 | |||
229 | /** |
||
230 | * Set the lang of the gmap |
||
231 | * |
||
232 | * @param string $lang GoogleMap lang : fr,en,.. |
||
233 | * |
||
234 | * @return MapAPI This same object, in order to enable chaining of methods |
||
235 | */ |
||
236 | |||
237 | public function setLang($lang) { |
||
241 | |||
242 | /** |
||
243 | * Set the zoom of the gmap |
||
244 | * |
||
245 | * @param int $zoom GoogleMap zoom. |
||
246 | * |
||
247 | * @return MapAPI This same object, in order to enable chaining of methods |
||
248 | */ |
||
249 | |||
250 | public function setZoom($zoom) { |
||
254 | |||
255 | /** |
||
256 | * Set the zoom of the infowindow |
||
257 | * |
||
258 | * @param int $infoWindowZoom GoogleMap information window zoom. |
||
259 | * |
||
260 | * @return MapAPI This same object, in order to enable chaining of methods |
||
261 | */ |
||
262 | |||
263 | public function setInfoWindowZoom($infoWindowZoom) { |
||
267 | |||
268 | /** |
||
269 | * Enable the zoom on the marker when you click on it |
||
270 | * |
||
271 | * @param int $enableWindowZoom info window enabled zoom. |
||
272 | * |
||
273 | * @return MapAPI This same object, in order to enable chaining of methods |
||
274 | */ |
||
275 | |||
276 | public function setEnableWindowZoom($enableWindowZoom) { |
||
280 | |||
281 | /** |
||
282 | * Enable theautomatic center/zoom at the gmap load |
||
283 | * |
||
284 | * @param int $enableAutomaticCenterZoom enable automatic centre zoom |
||
285 | * |
||
286 | * @return MapAPI This same object, in order to enable chaining of methods |
||
287 | */ |
||
288 | |||
289 | public function setEnableAutomaticCenterZoom($enableAutomaticCenterZoom) { |
||
293 | |||
294 | /** |
||
295 | * Set the center of the gmap (an address) |
||
296 | * |
||
297 | * @param string $center GoogleMap center (an address) |
||
298 | * |
||
299 | * @return MapAPI This same object, in order to enable chaining of methods |
||
300 | */ |
||
301 | |||
302 | public function setCenter($center) { |
||
306 | |||
307 | /** |
||
308 | * Set the type of the gmap. Also takes into account legacy settings |
||
309 | * |
||
310 | * FIXME - allow other valid settings in config for map type |
||
311 | * |
||
312 | * @param string $mapType Can be one of road,satellite,hybrid or terrain. Defaults to road |
||
313 | * |
||
314 | * @return MapAPI This same object, in order to enable chaining of methods |
||
315 | */ |
||
316 | |||
317 | public function setMapType($mapType) { |
||
338 | |||
339 | /* |
||
340 | Set whether or not to allow the full screen tools |
||
341 | @return MapAPI This same object, in order to enable chaining of methods |
||
342 | */ |
||
343 | public function setAllowFullScreen($allowed) { |
||
347 | |||
348 | /** |
||
349 | * Set the center of the gmap |
||
350 | * |
||
351 | * @return MapAPI This same object, in order to enable chaining of methods |
||
352 | **/ |
||
353 | public function setLatLongCenter($center) { |
||
369 | |||
370 | /** |
||
371 | * Set the defaultHideMarker |
||
372 | * |
||
373 | * @param boolean $defaultHideMarker hide all the markers on the map by default |
||
374 | * |
||
375 | * @return MapAPI |
||
376 | */ |
||
377 | |||
378 | public function setDefaultHideMarker($defaultHideMarker) { |
||
382 | |||
383 | /** |
||
384 | * Get the google map content |
||
385 | * |
||
386 | * @return string the google map html code |
||
387 | */ |
||
388 | |||
389 | public function getGoogleMap() { |
||
392 | |||
393 | |||
394 | /** |
||
395 | * Get URL content using cURL. |
||
396 | * |
||
397 | * @param string $url the url |
||
398 | * |
||
399 | * @return string the html code |
||
400 | * |
||
401 | * @todo add proxy settings |
||
402 | */ |
||
403 | |||
404 | public function getContent($url) { |
||
414 | |||
415 | /** |
||
416 | * Geocoding an address (address -> lat,lng) |
||
417 | * |
||
418 | * @param string $address an address |
||
419 | * |
||
420 | * @return string array with precision, lat & lng |
||
421 | */ |
||
422 | |||
423 | public function geocoding($address) { |
||
439 | |||
440 | /** |
||
441 | * Add marker by his coord |
||
442 | * |
||
443 | * @param string $lat lat |
||
444 | * @param string $lng lngs |
||
445 | * @param string $html html code display in the info window |
||
446 | * @param string $category marker category |
||
447 | * @param string $icon an icon url |
||
448 | * |
||
449 | * @return MapAPI |
||
450 | */ |
||
451 | |||
452 | public function addMarkerByCoords($lat, $lng, $html = '', $category = '', $icon = '') { |
||
463 | |||
464 | |||
465 | /** |
||
466 | * Add marker by his address |
||
467 | * |
||
468 | * @param string $address an ddress |
||
469 | * @param string $content html code display in the info window |
||
470 | * @param string $category marker category |
||
471 | * @param string $icon an icon url |
||
472 | * |
||
473 | * @return MapAPI |
||
474 | */ |
||
475 | |||
476 | public function addMarkerByAddress($address, $content = '', $category = '', $icon = '') { |
||
485 | |||
486 | /** |
||
487 | * Add marker by an array of coord |
||
488 | * |
||
489 | * @param array $coordtab an array of lat,lng,content |
||
490 | * @param string $category marker category |
||
491 | * @param string $icon an icon url |
||
492 | * |
||
493 | * @return MapAPI |
||
494 | */ |
||
495 | |||
496 | public function addArrayMarkerByCoords($coordtab, $category = '', $icon = '') { |
||
502 | |||
503 | |||
504 | /** |
||
505 | * Adds a {@link ViewableData} object that implements {@link Mappable} |
||
506 | * to the map. |
||
507 | * @param $infowindowtemplateparams Optional array of extra parameters to pass to the map info window |
||
508 | * |
||
509 | * @param ViewableData $obj |
||
510 | */ |
||
511 | public function addMarkerAsObject(ViewableData $obj, $infowindowtemplateparams = null) { |
||
545 | |||
546 | |||
547 | /** |
||
548 | * Draws a line between two {@link ViewableData} objects |
||
549 | * |
||
550 | * @param ViewableData $one The first point |
||
551 | * @param ViewableData $two The second point |
||
552 | * @param string $color The hexidecimal color of the line |
||
553 | */ |
||
554 | public function connectPoints(ViewableData $one, ViewableData $two, $color = "#FF3300") { |
||
561 | |||
562 | |||
563 | public function forTemplate() { |
||
567 | |||
568 | /** |
||
569 | * Add a KML file which will be rendered on this map. Normally used for likes |
||
570 | * of GPS traces from activities |
||
571 | * |
||
572 | * @param string $url url of the kml file compatible with gmap and gearth |
||
573 | * |
||
574 | * @return MapAPI |
||
575 | */ |
||
576 | |||
577 | public function addKML($url) { |
||
581 | |||
582 | |||
583 | /* |
||
584 | Add a line to the map |
||
585 | |||
586 | */ |
||
587 | public function addLine($from = array(), $to = array(), $color = "#FF3300") { |
||
599 | |||
600 | |||
601 | /* |
||
602 | For php 5.3 |
||
603 | */ |
||
604 | private function jsonRemoveUnicodeSequences($struct) { |
||
609 | |||
610 | |||
611 | /** |
||
612 | * Generate the gmap |
||
613 | * |
||
614 | * @return void |
||
615 | */ |
||
616 | |||
617 | public function generate() { |
||
724 | |||
725 | /** |
||
726 | * @param string $templateName |
||
727 | * @param ArrayData $templateVariables |
||
728 | */ |
||
729 | function processTemplateHTML($templateName, $templateVariables = null) { |
||
737 | } |
||
738 |
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.