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 | /** GoogleMap Direction ID for the HTML DIV **/ |
||
27 | protected $googleMapDirectionId = 'route'; |
||
28 | |||
29 | /* Additional CSS classes to use when rendering the map */ |
||
30 | protected $set_additional_css_classes = ''; |
||
31 | |||
32 | /** Width of the gmap **/ |
||
33 | protected $width = 800; |
||
34 | |||
35 | /** Height of the gmap **/ |
||
36 | protected $height = 600; |
||
37 | |||
38 | /** Icon width of the gmarker **/ |
||
39 | protected $iconWidth = 20; |
||
40 | |||
41 | /** Icon height of the gmarker **/ |
||
42 | protected $iconHeight = 34; |
||
43 | |||
44 | /* array of lines to be drawn on the map */ |
||
45 | protected $lines = array(); |
||
46 | |||
47 | /* kml file to be rendered */ |
||
48 | protected $kmlFiles = array(); |
||
49 | |||
50 | /** Default zoom of the gmap **/ |
||
51 | protected $zoom = 9; |
||
52 | |||
53 | /** Enable the zoom of the Infowindow **/ |
||
54 | protected $enableWindowZoom = false; |
||
55 | |||
56 | /** Default zoom of the Infowindow **/ |
||
57 | protected $infoWindowZoom = 13; |
||
58 | |||
59 | /** Lang of the gmap **/ |
||
60 | protected $lang = 'en'; |
||
61 | |||
62 | /**Center of the gmap **/ |
||
63 | protected $center = 'Paris, France'; |
||
64 | |||
65 | /* |
||
66 | Additional CSS classes to render as a class attribute for the div of the |
||
67 | map. Use this if you want more fine grained control over your map using |
||
68 | CSS. If blank it will be ignored |
||
69 | */ |
||
70 | protected $additional_css_classes = ''; |
||
71 | |||
72 | |||
73 | /* Decided whether or not to show the inline map css style on div creation */ |
||
74 | protected $show_inline_map_div_style = true; |
||
75 | |||
76 | protected $latLongCenter = null; |
||
77 | |||
78 | protected $jsonMapStyles = '[]'; |
||
79 | |||
80 | protected $delayLoadMapFunction = false; |
||
81 | |||
82 | /** |
||
83 | * Type of the gmap, can be: |
||
84 | * 'road' (roadmap), |
||
85 | * 'satellite' (sattelite/aerial photographs) |
||
86 | * 'hybrid' (hybrid of road and satellite) |
||
87 | * 'terrain' (terrain) |
||
88 | * The JavaScript for the mapping service will convert this into a suitable mapping type |
||
89 | */ |
||
90 | |||
91 | protected $mapType = 'road'; |
||
92 | |||
93 | |||
94 | /** Content of the HTML generated **/ |
||
95 | protected $content = ''; |
||
96 | |||
97 | protected $mapService = 'google'; |
||
98 | |||
99 | /** Add the direction button to the infowindow **/ |
||
100 | protected $displayDirectionFields = false; |
||
101 | |||
102 | /** Hide the marker by default **/ |
||
103 | protected $defaultHideMarker = false; |
||
104 | |||
105 | /** Extra content (marker, etc...) **/ |
||
106 | protected $contentMarker = ''; |
||
107 | |||
108 | // a list of markers, markers being associative arrays |
||
109 | protected $markers = array(); |
||
110 | |||
111 | /** Use clusterer to display a lot of markers on the gmap **/ |
||
112 | protected $useClusterer = false; |
||
113 | protected $gridSize = 50; |
||
114 | protected $maxZoom = 17; |
||
115 | protected $clustererLibraryPath = "/mappable/javascript/google/markerclusterer.js"; |
||
116 | |||
117 | /** Enable automatic center/zoom **/ |
||
118 | protected $enableAutomaticCenterZoom = false; |
||
119 | |||
120 | /** maximum longitude of all markers **/ |
||
121 | protected $maxLng = -1000000; |
||
122 | |||
123 | /** minimum longitude of all markers **/ |
||
124 | protected $minLng = 1000000; |
||
125 | |||
126 | /** max latitude of all markers **/ |
||
127 | protected $maxLat = -1000000; |
||
128 | |||
129 | /** min latitude of all markers **/ |
||
130 | protected $minLat = 1000000; |
||
131 | |||
132 | /** map center latitude (horizontal), calculated automatically as markers |
||
133 | are added to the map **/ |
||
134 | protected $centerLat = null; |
||
135 | |||
136 | /** map center longitude (vertical), calculated automatically as markers |
||
137 | are added to the map **/ |
||
138 | protected $centerLng = null; |
||
139 | |||
140 | /** factor by which to fudge the boundaries so that when we zoom encompass, |
||
141 | the markers aren't too close to the edge **/ |
||
142 | protected $coordCoef = 0.01; |
||
143 | |||
144 | /* set this to true to render button to maximize / minimize a map */ |
||
145 | protected $allowFullScreen = null; |
||
146 | |||
147 | |||
148 | protected static $include_download_javascript = false; |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Class constructor |
||
153 | * |
||
154 | * @param string $googleMapKey the googleMapKey |
||
155 | */ |
||
156 | |||
157 | public function __construct($googleMapKey = '') { |
||
160 | |||
161 | /** |
||
162 | * Set the key of the gmap |
||
163 | * |
||
164 | * @param string $googleMapKey the googleMapKey |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | |||
169 | public function setKey($googleMapKey) { |
||
173 | |||
174 | public function setIncludeDownloadJavascript($inclusion) { |
||
178 | |||
179 | |||
180 | public function setShowInlineMapDivStyle($new_show_inline_map_div_style) { |
||
184 | |||
185 | public function setAdditionalCSSClasses($new_additional_css_classes) { |
||
189 | |||
190 | |||
191 | public function setMapStyle($newStyles) { |
||
195 | |||
196 | |||
197 | |||
198 | public function setDelayLoadMapFunction($newDelay) { |
||
202 | |||
203 | /** |
||
204 | * Set the useClusterer parameter (optimization to display a lot of marker) |
||
205 | * |
||
206 | * @param boolean $useClusterer use cluster or not |
||
207 | * @param int $gridSize grid size |
||
208 | * @param int $maxZoom max zoom to cluster at |
||
209 | * |
||
210 | * * @return MapAPI This same object, in order to enable chaining of methods |
||
211 | */ |
||
212 | |||
213 | public function setClusterer($useClusterer, $gridSize = 50, $maxZoom = 17, |
||
221 | |||
222 | /** |
||
223 | * Set the ID of the default gmap DIV |
||
224 | * |
||
225 | * @param string $googleMapId the google div ID |
||
226 | * |
||
227 | * @return MapAPI This same object, in order to enable chaining of methods |
||
228 | */ |
||
229 | |||
230 | public function setDivId($googleMapId) { |
||
234 | |||
235 | /** |
||
236 | * Set the ID of the default gmap direction DIV |
||
237 | * |
||
238 | * @param string $googleMapDirectionId GoogleMap Direction ID for the HTML DIV |
||
239 | * |
||
240 | * @return MapAPI This same object, in order to enable chaining of methods |
||
241 | */ |
||
242 | |||
243 | public function setDirectionDivId($googleMapDirectionId) { |
||
247 | |||
248 | /** |
||
249 | * Set the size of the gmap. If these values are not provided |
||
250 | * then CSS is used instead |
||
251 | * |
||
252 | * @param int $width GoogleMap width |
||
253 | * @param int $height GoogleMap height |
||
254 | * |
||
255 | * @return MapAPI This same object, in order to enable chaining of methods |
||
256 | */ |
||
257 | |||
258 | public function setSize($width, $height) { |
||
263 | |||
264 | |||
265 | /** |
||
266 | * Set the size of the icon markers |
||
267 | * |
||
268 | * @param int $iconWidth GoogleMap marker icon width |
||
269 | * @param int $iconHeight GoogleMap marker icon height |
||
270 | * |
||
271 | * @return MapAPI This same object, in order to enable chaining of methods |
||
272 | */ |
||
273 | |||
274 | public function setIconSize($iconWidth, $iconHeight) { |
||
279 | |||
280 | /** |
||
281 | * Set the lang of the gmap |
||
282 | * |
||
283 | * @param string $lang GoogleMap lang : fr,en,.. |
||
284 | * |
||
285 | * @return MapAPI This same object, in order to enable chaining of methods |
||
286 | */ |
||
287 | |||
288 | public function setLang($lang) { |
||
292 | |||
293 | /** |
||
294 | * Set the zoom of the gmap |
||
295 | * |
||
296 | * @param int $zoom GoogleMap zoom. |
||
297 | * |
||
298 | * @return MapAPI This same object, in order to enable chaining of methods |
||
299 | */ |
||
300 | |||
301 | public function setZoom($zoom) { |
||
305 | |||
306 | /** |
||
307 | * Set the zoom of the infowindow |
||
308 | * |
||
309 | * @param int $infoWindowZoom GoogleMap information window zoom. |
||
310 | * |
||
311 | * @return MapAPI This same object, in order to enable chaining of methods |
||
312 | */ |
||
313 | |||
314 | public function setInfoWindowZoom($infoWindowZoom) { |
||
318 | |||
319 | /** |
||
320 | * Enable the zoom on the marker when you click on it |
||
321 | * |
||
322 | * @param int $enableWindowZoom info window enabled zoom. |
||
323 | * |
||
324 | * @return MapAPI This same object, in order to enable chaining of methods |
||
325 | */ |
||
326 | |||
327 | public function setEnableWindowZoom($enableWindowZoom) { |
||
331 | |||
332 | /** |
||
333 | * Enable theautomatic center/zoom at the gmap load |
||
334 | * |
||
335 | * @param int $enableAutomaticCenterZoom enable automatic centre zoom |
||
336 | * |
||
337 | * @return MapAPI This same object, in order to enable chaining of methods |
||
338 | */ |
||
339 | |||
340 | public function setEnableAutomaticCenterZoom($enableAutomaticCenterZoom) { |
||
344 | |||
345 | /** |
||
346 | * Set the center of the gmap (an address) |
||
347 | * |
||
348 | * @param string $center GoogleMap center (an address) |
||
349 | * |
||
350 | * @return MapAPI This same object, in order to enable chaining of methods |
||
351 | */ |
||
352 | |||
353 | public function setCenter($center) { |
||
357 | |||
358 | /** |
||
359 | * Set the type of the gmap. Also takes into account legacy settings |
||
360 | * |
||
361 | * FIXME - allow other valid settings in config for map type |
||
362 | * |
||
363 | * @param string $mapType Can be one of road,satellite,hybrid or terrain. Defaults to road |
||
364 | * |
||
365 | * @return MapAPI This same object, in order to enable chaining of methods |
||
366 | */ |
||
367 | |||
368 | public function setMapType($mapType) { |
||
389 | |||
390 | /* |
||
391 | Set whether or not to allow the full screen tools |
||
392 | @return MapAPI This same object, in order to enable chaining of methods |
||
393 | */ |
||
394 | public function setAllowFullScreen($allowed) { |
||
398 | |||
399 | /** |
||
400 | * Set the center of the gmap |
||
401 | * |
||
402 | * @return MapAPI This same object, in order to enable chaining of methods |
||
403 | **/ |
||
404 | public function setLatLongCenter($center) { |
||
408 | |||
409 | /** |
||
410 | * Decide whether or not to show direction controls |
||
411 | * |
||
412 | * @param boolean $displayDirectionFields display directions or not in the info window |
||
413 | * |
||
414 | * @return MapAPI This same object, in order to enable chaining of methods |
||
415 | */ |
||
416 | |||
417 | public function setDisplayDirectionFields($displayDirectionFields) { |
||
421 | |||
422 | /** |
||
423 | * Set the defaultHideMarker |
||
424 | * |
||
425 | * @param boolean $defaultHideMarker hide all the markers on the map by default |
||
426 | * |
||
427 | * @return void |
||
428 | */ |
||
429 | |||
430 | public function setDefaultHideMarker($defaultHideMarker) { |
||
434 | |||
435 | /** |
||
436 | * Get the google map content |
||
437 | * |
||
438 | * @return string the google map html code |
||
439 | */ |
||
440 | |||
441 | public function getGoogleMap() { |
||
444 | |||
445 | |||
446 | /** |
||
447 | * Get URL content using cURL. |
||
448 | * |
||
449 | * @param string $url the url |
||
450 | * |
||
451 | * @return string the html code |
||
452 | * |
||
453 | * @todo add proxy settings |
||
454 | */ |
||
455 | |||
456 | public function getContent($url) { |
||
466 | |||
467 | /** |
||
468 | * Geocoding an address (address -> lat,lng) |
||
469 | * |
||
470 | * @param string $address an address |
||
471 | * |
||
472 | * @return array array with precision, lat & lng |
||
473 | */ |
||
474 | |||
475 | public function geocoding($address) { |
||
491 | |||
492 | /** |
||
493 | * Add marker by his coord |
||
494 | * |
||
495 | * @param string $lat lat |
||
496 | * @param string $lng lngs |
||
497 | * @param string $html html code display in the info window |
||
498 | * @param string $category marker category |
||
499 | * @param string $icon an icon url |
||
500 | * |
||
501 | * @return void |
||
502 | */ |
||
503 | |||
504 | public function addMarkerByCoords($lat, $lng, $html = '', $category = '', $icon = '') { |
||
515 | |||
516 | |||
517 | /** |
||
518 | * Add marker by his address |
||
519 | * |
||
520 | * @param string $address an ddress |
||
521 | * @param string $content html code display in the info window |
||
522 | * @param string $category marker category |
||
523 | * @param string $icon an icon url |
||
524 | * |
||
525 | * @return void |
||
526 | */ |
||
527 | |||
528 | public function addMarkerByAddress($address, $content = '', $category = '', $icon = '') { |
||
535 | |||
536 | /** |
||
537 | * Add marker by an array of coord |
||
538 | * |
||
539 | * @param array $coordtab an array of lat,lng,content |
||
540 | * @param string $category marker category |
||
541 | * @param string $icon an icon url |
||
542 | * |
||
543 | * @return void |
||
544 | */ |
||
545 | |||
546 | public function addArrayMarkerByCoords($coordtab, $category = '', $icon = '') { |
||
552 | |||
553 | |||
554 | /** |
||
555 | * Adds a {@link ViewableData} object that implements {@link Mappable} |
||
556 | * to the map. |
||
557 | * @param $infowindowtemplateparams Optional array of extra parameters to pass to the map info window |
||
558 | * |
||
559 | * @param ViewableData $obj |
||
560 | */ |
||
561 | public function addMarkerAsObject(ViewableData $obj, $infowindowtemplateparams = null) { |
||
595 | |||
596 | |||
597 | /** |
||
598 | * Draws a line between two {@link ViewableData} objects |
||
599 | * |
||
600 | * @param ViewableData $one The first point |
||
601 | * @param ViewableData $two The second point |
||
602 | * @param string $color The hexidecimal color of the line |
||
603 | */ |
||
604 | public function connectPoints(ViewableData $one, ViewableData $two, $color = "#FF3300") { |
||
611 | |||
612 | |||
613 | public function forTemplate() { |
||
617 | |||
618 | |||
619 | /** |
||
620 | * Add marker by an array of address |
||
621 | * |
||
622 | * @param array $coordtab an array of address |
||
623 | * @param string $category marker category |
||
624 | * @param string $icon an icon url |
||
625 | * |
||
626 | * @return void |
||
627 | */ |
||
628 | |||
629 | public function addArrayMarkerByAddress($coordtab, $category = '', $icon = '') { |
||
635 | |||
636 | /** |
||
637 | * Set a direction between 2 addresss and set a text panel |
||
638 | * |
||
639 | * @param string $from an address |
||
640 | * @param string $to an address |
||
641 | * @param string $idpanel id of the div panel |
||
642 | * |
||
643 | * @return void |
||
644 | */ |
||
645 | |||
646 | public function addDirection($from, $to, $idpanel = '') { |
||
649 | |||
650 | /** |
||
651 | * Parse a KML file and add markers to a category |
||
652 | * |
||
653 | * @param string $url url of the kml file compatible with gmap and gearth |
||
654 | * |
||
655 | * @return void |
||
656 | */ |
||
657 | |||
658 | public function addKML($url) { |
||
662 | |||
663 | |||
664 | /* |
||
665 | Add a line to the map |
||
666 | |||
667 | */ |
||
668 | public function addLine($from = array(), $to = array(), $color = "#FF3300") { |
||
680 | |||
681 | |||
682 | /* |
||
683 | For php 5.3 |
||
684 | */ |
||
685 | private function jsonRemoveUnicodeSequences($struct) { |
||
690 | |||
691 | |||
692 | /** |
||
693 | * Generate the gmap |
||
694 | * |
||
695 | * @return void |
||
696 | */ |
||
697 | |||
698 | public function generate() { |
||
802 | |||
803 | function processTemplateJS($templateName, $templateVariables = null) { |
||
811 | |||
812 | function processTemplateHTML($templateName, $templateVariables = null) { |
||
820 | } |
||
821 |
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.