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 | /** |
||
72 | * Type of the gmap, can be: |
||
73 | * 'road' (roadmap), |
||
74 | * 'satellite' (sattelite/aerial photographs) |
||
75 | * 'hybrid' (hybrid of road and satellite) |
||
76 | * 'terrain' (terrain) |
||
77 | * The JavaScript for the mapping service will convert this into a suitable mapping type |
||
78 | */ |
||
79 | |||
80 | protected $mapType = 'road'; |
||
81 | |||
82 | |||
83 | /** Content of the HTML generated **/ |
||
84 | protected $content = ''; |
||
85 | |||
86 | protected $mapService = 'google'; |
||
87 | |||
88 | /** Hide the marker by default **/ |
||
89 | protected $defaultHideMarker = false; |
||
90 | |||
91 | /** Extra content (marker, etc...) **/ |
||
92 | protected $contentMarker = ''; |
||
93 | |||
94 | // a list of markers, markers being associative arrays |
||
95 | protected $markers = array(); |
||
96 | |||
97 | /** Use clusterer to display a lot of markers on the gmap **/ |
||
98 | protected $useClusterer = false; |
||
99 | protected $gridSize = 50; |
||
100 | protected $maxZoom = 17; |
||
101 | protected $clustererLibraryPath = "/mappable/javascript/google/markerclusterer.js"; |
||
102 | |||
103 | /** Enable automatic center/zoom **/ |
||
104 | protected $enableAutomaticCenterZoom = false; |
||
105 | |||
106 | /** maximum longitude of all markers **/ |
||
107 | protected $maxLng = -1000000; |
||
108 | |||
109 | /** minimum longitude of all markers **/ |
||
110 | protected $minLng = 1000000; |
||
111 | |||
112 | /** max latitude of all markers **/ |
||
113 | protected $maxLat = -1000000; |
||
114 | |||
115 | /** min latitude of all markers **/ |
||
116 | protected $minLat = 1000000; |
||
117 | |||
118 | /** map center latitude (horizontal), calculated automatically as markers |
||
119 | are added to the map **/ |
||
120 | protected $centerLat = null; |
||
121 | |||
122 | /** map center longitude (vertical), calculated automatically as markers |
||
123 | are added to the map **/ |
||
124 | protected $centerLng = null; |
||
125 | |||
126 | /** factor by which to fudge the boundaries so that when we zoom encompass, |
||
127 | the markers aren't too close to the edge **/ |
||
128 | protected $coordCoef = 0.01; |
||
129 | |||
130 | /* set this to true to render button to maximize / minimize a map */ |
||
131 | protected $allowFullScreen = null; |
||
132 | |||
133 | /** |
||
134 | * Class constructor |
||
135 | * |
||
136 | * @param string $googleMapKey the googleMapKey |
||
137 | */ |
||
138 | |||
139 | public function __construct($googleMapKey = '') { |
||
142 | |||
143 | /** |
||
144 | * Set the key of the gmap |
||
145 | * |
||
146 | * @param string $googleMapKey the googleMapKey |
||
147 | * |
||
148 | * @return MapAPI |
||
149 | */ |
||
150 | |||
151 | public function setKey($googleMapKey) { |
||
155 | |||
156 | public function setShowInlineMapDivStyle($new_show_inline_map_div_style) { |
||
160 | |||
161 | public function setAdditionalCSSClasses($new_additional_css_classes) { |
||
165 | |||
166 | |||
167 | public function setMapStyle($newStyles) { |
||
171 | |||
172 | /** |
||
173 | * Set the useClusterer parameter (optimization to display a lot of marker) |
||
174 | * |
||
175 | * @param boolean $useClusterer use cluster or not |
||
176 | * @param int $gridSize grid size |
||
177 | * @param int $maxZoom max zoom to cluster at |
||
178 | * |
||
179 | * * @return MapAPI This same object, in order to enable chaining of methods |
||
180 | */ |
||
181 | |||
182 | public function setClusterer($useClusterer, $gridSize = 50, $maxZoom = 17, |
||
190 | |||
191 | /** |
||
192 | * Set the ID of the default gmap DIV |
||
193 | * |
||
194 | * @param string $googleMapId the google div ID |
||
195 | * |
||
196 | * @return MapAPI This same object, in order to enable chaining of methods |
||
197 | */ |
||
198 | |||
199 | public function setDivId($googleMapId) { |
||
203 | |||
204 | /** |
||
205 | * Set the size of the gmap. If these values are not provided |
||
206 | * then CSS is used instead |
||
207 | * |
||
208 | * @param int $width GoogleMap width |
||
209 | * @param int $height GoogleMap height |
||
210 | * |
||
211 | * @return MapAPI This same object, in order to enable chaining of methods |
||
212 | */ |
||
213 | |||
214 | public function setSize($width, $height) { |
||
219 | |||
220 | /** |
||
221 | * Set the lang of the gmap |
||
222 | * |
||
223 | * @param string $lang GoogleMap lang : fr,en,.. |
||
224 | * |
||
225 | * @return MapAPI This same object, in order to enable chaining of methods |
||
226 | */ |
||
227 | |||
228 | public function setLang($lang) { |
||
232 | |||
233 | /** |
||
234 | * Set the zoom of the gmap |
||
235 | * |
||
236 | * @param int $zoom GoogleMap zoom. |
||
237 | * |
||
238 | * @return MapAPI This same object, in order to enable chaining of methods |
||
239 | */ |
||
240 | |||
241 | public function setZoom($zoom) { |
||
245 | |||
246 | /** |
||
247 | * Set the zoom of the infowindow |
||
248 | * |
||
249 | * @param int $infoWindowZoom GoogleMap information window zoom. |
||
250 | * |
||
251 | * @return MapAPI This same object, in order to enable chaining of methods |
||
252 | */ |
||
253 | |||
254 | public function setInfoWindowZoom($infoWindowZoom) { |
||
258 | |||
259 | /** |
||
260 | * Enable the zoom on the marker when you click on it |
||
261 | * |
||
262 | * @param int $enableWindowZoom info window enabled zoom. |
||
263 | * |
||
264 | * @return MapAPI This same object, in order to enable chaining of methods |
||
265 | */ |
||
266 | |||
267 | public function setEnableWindowZoom($enableWindowZoom) { |
||
271 | |||
272 | /** |
||
273 | * Enable theautomatic center/zoom at the gmap load |
||
274 | * |
||
275 | * @param int $enableAutomaticCenterZoom enable automatic centre zoom |
||
276 | * |
||
277 | * @return MapAPI This same object, in order to enable chaining of methods |
||
278 | */ |
||
279 | |||
280 | public function setEnableAutomaticCenterZoom($enableAutomaticCenterZoom) { |
||
284 | |||
285 | /** |
||
286 | * Set the center of the gmap (an address) |
||
287 | * |
||
288 | * @param string $center GoogleMap center (an address) |
||
289 | * |
||
290 | * @return MapAPI This same object, in order to enable chaining of methods |
||
291 | */ |
||
292 | |||
293 | 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 | * |
||
305 | * @return MapAPI This same object, in order to enable chaining of methods |
||
306 | */ |
||
307 | |||
308 | public function setMapType($mapType) { |
||
328 | |||
329 | /* |
||
330 | Set whether or not to allow the full screen tools |
||
331 | @return MapAPI This same object, in order to enable chaining of methods |
||
332 | */ |
||
333 | public function setAllowFullScreen($allowed) { |
||
337 | |||
338 | /** |
||
339 | * Set the center of the gmap |
||
340 | * |
||
341 | * @return MapAPI This same object, in order to enable chaining of methods |
||
342 | **/ |
||
343 | public function setLatLongCenter($center) { |
||
359 | |||
360 | /** |
||
361 | * Set the defaultHideMarker |
||
362 | * |
||
363 | * @param boolean $defaultHideMarker hide all the markers on the map by default |
||
364 | * |
||
365 | * @return MapAPI |
||
366 | */ |
||
367 | |||
368 | public function setDefaultHideMarker($defaultHideMarker) { |
||
372 | |||
373 | /** |
||
374 | * Get the google map content |
||
375 | * |
||
376 | * @return string the google map html code |
||
377 | */ |
||
378 | |||
379 | public function getGoogleMap() { |
||
382 | |||
383 | |||
384 | /** |
||
385 | * Get URL content using cURL. |
||
386 | * |
||
387 | * @param string $url the url |
||
388 | * |
||
389 | * @return string the html code |
||
390 | * |
||
391 | * @todo add proxy settings |
||
392 | */ |
||
393 | |||
394 | public function getContent($url) { |
||
404 | |||
405 | /** |
||
406 | * Geocoding an address (address -> lat,lng) |
||
407 | * |
||
408 | * @param string $address an address |
||
409 | * |
||
410 | * @return string array with precision, lat & lng |
||
411 | */ |
||
412 | |||
413 | public function geocoding($address) { |
||
431 | |||
432 | /** |
||
433 | * Add marker by his coord |
||
434 | * |
||
435 | * @param string $lat lat |
||
436 | * @param string $lng lngs |
||
437 | * @param string $html html code display in the info window |
||
438 | * @param string $category marker category |
||
439 | * @param string $icon an icon url |
||
440 | * |
||
441 | * @return MapAPI |
||
442 | */ |
||
443 | |||
444 | public function addMarkerByCoords($lat, $lng, $html = '', $category = '', $icon = '') { |
||
455 | |||
456 | |||
457 | /** |
||
458 | * Add marker by his address |
||
459 | * |
||
460 | * @param string $address an ddress |
||
461 | * @param string $content html code display in the info window |
||
462 | * @param string $category marker category |
||
463 | * @param string $icon an icon url |
||
464 | * |
||
465 | * @return MapAPI |
||
466 | */ |
||
467 | |||
468 | public function addMarkerByAddress($address, $content = '', $category = '', $icon = '') { |
||
475 | |||
476 | /** |
||
477 | * Add marker by an array of coord |
||
478 | * |
||
479 | * @param array $coordtab an array of lat,lng,content |
||
480 | * @param string $category marker category |
||
481 | * @param string $icon an icon url |
||
482 | * |
||
483 | * @return MapAPI |
||
484 | */ |
||
485 | |||
486 | public function addArrayMarkerByCoords($coordtab, $category = '', $icon = '') { |
||
492 | |||
493 | |||
494 | /** |
||
495 | * Adds a {@link ViewableData} object that implements {@link Mappable} |
||
496 | * to the map. |
||
497 | * @param $infowindowtemplateparams Optional array of extra parameters to pass to the map info window |
||
498 | * |
||
499 | * @param ViewableData $obj |
||
500 | */ |
||
501 | public function addMarkerAsObject(ViewableData $obj, $infowindowtemplateparams = null) { |
||
536 | |||
537 | |||
538 | /** |
||
539 | * Draws a line between two {@link ViewableData} objects |
||
540 | * |
||
541 | * @param ViewableData $one The first point |
||
542 | * @param ViewableData $two The second point |
||
543 | * @param string $color The hexidecimal color of the line |
||
544 | */ |
||
545 | public function connectPoints(ViewableData $one, ViewableData $two, $color = "#FF3300") { |
||
552 | |||
553 | |||
554 | public function forTemplate() { |
||
558 | |||
559 | /** |
||
560 | * Add a KML file which will be rendered on this map. Normally used for likes |
||
561 | * of GPS traces from activities |
||
562 | * |
||
563 | * @param string $url url of the kml file compatible with gmap and gearth |
||
564 | * |
||
565 | * @return MapAPI |
||
566 | */ |
||
567 | |||
568 | public function addKML($url) { |
||
572 | |||
573 | |||
574 | /* |
||
575 | Add a line to the map |
||
576 | |||
577 | */ |
||
578 | public function addLine($from = array(), $to = array(), $color = "#FF3300") { |
||
590 | |||
591 | |||
592 | /* |
||
593 | For php 5.3 |
||
594 | */ |
||
595 | public static function jsonRemoveUnicodeSequences($struct) { |
||
600 | |||
601 | |||
602 | /** |
||
603 | * Generate the gmap |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | |||
608 | public function generate() { |
||
712 | |||
713 | /** |
||
714 | * @param string $templateName |
||
715 | * @param ArrayData $templateVariables |
||
716 | */ |
||
717 | public function processTemplateHTML($templateName, $templateVariables = null) { |
||
725 | } |
||
726 |
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.