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 | public function setShowInlineMapDivStyle($new_show_inline_map_div_style) { |
||
147 | |||
148 | public function setAdditionalCSSClasses($new_additional_css_classes) { |
||
152 | |||
153 | |||
154 | public function setMapStyle($newStyles) { |
||
158 | |||
159 | /** |
||
160 | * Set the useClusterer parameter (optimization to display a lot of marker) |
||
161 | * |
||
162 | * @param boolean $useClusterer use cluster or not |
||
163 | * @param int $gridSize grid size |
||
164 | * @param int $maxZoom max zoom to cluster at |
||
165 | * |
||
166 | * * @return MapAPI This same object, in order to enable chaining of methods |
||
167 | */ |
||
168 | |||
169 | public function setClusterer($useClusterer, $gridSize = 50, $maxZoom = 17, |
||
177 | |||
178 | /** |
||
179 | * Set the ID of the default gmap DIV |
||
180 | * |
||
181 | * @param string $googleMapId the google div ID |
||
182 | * |
||
183 | * @return MapAPI This same object, in order to enable chaining of methods |
||
184 | */ |
||
185 | |||
186 | public function setDivId($googleMapId) { |
||
190 | |||
191 | /** |
||
192 | * Set the size of the gmap. If these values are not provided |
||
193 | * then CSS is used instead |
||
194 | * |
||
195 | * @param int $width GoogleMap width |
||
196 | * @param int $height GoogleMap height |
||
197 | * |
||
198 | * @return MapAPI This same object, in order to enable chaining of methods |
||
199 | */ |
||
200 | |||
201 | public function setSize($width, $height) { |
||
206 | |||
207 | /** |
||
208 | * Set the lang of the gmap |
||
209 | * |
||
210 | * @param string $lang GoogleMap lang : fr,en,.. |
||
211 | * |
||
212 | * @return MapAPI This same object, in order to enable chaining of methods |
||
213 | */ |
||
214 | |||
215 | public function setLang($lang) { |
||
219 | |||
220 | /** |
||
221 | * Set the zoom of the gmap |
||
222 | * |
||
223 | * @param int $zoom GoogleMap zoom. |
||
224 | * |
||
225 | * @return MapAPI This same object, in order to enable chaining of methods |
||
226 | */ |
||
227 | |||
228 | public function setZoom($zoom) { |
||
232 | |||
233 | /** |
||
234 | * Set the zoom of the infowindow |
||
235 | * |
||
236 | * @param int $infoWindowZoom GoogleMap information window zoom. |
||
237 | * |
||
238 | * @return MapAPI This same object, in order to enable chaining of methods |
||
239 | */ |
||
240 | |||
241 | public function setInfoWindowZoom($infoWindowZoom) { |
||
245 | |||
246 | /** |
||
247 | * Enable the zoom on the marker when you click on it |
||
248 | * |
||
249 | * @param int $enableWindowZoom info window enabled zoom. |
||
250 | * |
||
251 | * @return MapAPI This same object, in order to enable chaining of methods |
||
252 | */ |
||
253 | |||
254 | public function setEnableWindowZoom($enableWindowZoom) { |
||
258 | |||
259 | /** |
||
260 | * Enable theautomatic center/zoom at the gmap load |
||
261 | * |
||
262 | * @param int $enableAutomaticCenterZoom enable automatic centre zoom |
||
263 | * |
||
264 | * @return MapAPI This same object, in order to enable chaining of methods |
||
265 | */ |
||
266 | |||
267 | public function setEnableAutomaticCenterZoom($enableAutomaticCenterZoom) { |
||
271 | |||
272 | /** |
||
273 | * Set the center of the gmap (an address) |
||
274 | * |
||
275 | * @param string $center GoogleMap center (an address) |
||
276 | * |
||
277 | * @return MapAPI This same object, in order to enable chaining of methods |
||
278 | */ |
||
279 | |||
280 | public function setCenter($center) { |
||
284 | |||
285 | /** |
||
286 | * Set the type of the gmap. Also takes into account legacy settings |
||
287 | * |
||
288 | * FIXME - allow other valid settings in config for map type |
||
289 | * |
||
290 | * @param string $mapType Can be one of road,satellite,hybrid or terrain. Defaults to road |
||
291 | * |
||
292 | * @return MapAPI This same object, in order to enable chaining of methods |
||
293 | */ |
||
294 | |||
295 | public function setMapType($mapType) { |
||
315 | |||
316 | /* |
||
317 | Set whether or not to allow the full screen tools |
||
318 | @return MapAPI This same object, in order to enable chaining of methods |
||
319 | */ |
||
320 | public function setAllowFullScreen($allowed) { |
||
324 | |||
325 | /** |
||
326 | * Set the center of the gmap |
||
327 | * |
||
328 | * @return MapAPI This same object, in order to enable chaining of methods |
||
329 | **/ |
||
330 | public function setLatLongCenter($center) { |
||
346 | |||
347 | /** |
||
348 | * Set the defaultHideMarker |
||
349 | * |
||
350 | * @param boolean $defaultHideMarker hide all the markers on the map by default |
||
351 | * |
||
352 | * @return MapAPI |
||
353 | */ |
||
354 | |||
355 | public function setDefaultHideMarker($defaultHideMarker) { |
||
359 | |||
360 | /** |
||
361 | * Get the google map content |
||
362 | * |
||
363 | * @return string the google map html code |
||
364 | */ |
||
365 | |||
366 | public function getGoogleMap() { |
||
369 | |||
370 | |||
371 | /** |
||
372 | * Get URL content using cURL. |
||
373 | * |
||
374 | * @param string $url the url |
||
375 | * |
||
376 | * @return string the html code |
||
377 | * |
||
378 | * @todo add proxy settings |
||
379 | */ |
||
380 | |||
381 | public function getContent($url) { |
||
391 | |||
392 | /** |
||
393 | * Geocoding an address (address -> lat,lng) |
||
394 | * |
||
395 | * @param string $address an address |
||
396 | * |
||
397 | * @return string array with precision, lat & lng |
||
398 | */ |
||
399 | |||
400 | public function geocoding($address) { |
||
418 | |||
419 | /** |
||
420 | * Add marker by his coord |
||
421 | * |
||
422 | * @param string $lat lat |
||
423 | * @param string $lng lngs |
||
424 | * @param string $html html code display in the info window |
||
425 | * @param string $category marker category |
||
426 | * @param string $icon an icon url |
||
427 | * |
||
428 | * @return MapAPI |
||
429 | */ |
||
430 | |||
431 | public function addMarkerByCoords($lat, $lng, $html = '', $category = '', $icon = '') { |
||
442 | |||
443 | |||
444 | /** |
||
445 | * Add marker by his address |
||
446 | * |
||
447 | * @param string $address an ddress |
||
448 | * @param string $content html code display in the info window |
||
449 | * @param string $category marker category |
||
450 | * @param string $icon an icon url |
||
451 | * |
||
452 | * @return MapAPI |
||
453 | */ |
||
454 | |||
455 | public function addMarkerByAddress($address, $content = '', $category = '', $icon = '') { |
||
462 | |||
463 | /** |
||
464 | * Add marker by an array of coord |
||
465 | * |
||
466 | * @param array $coordtab an array of lat,lng,content |
||
467 | * @param string $category marker category |
||
468 | * @param string $icon an icon url |
||
469 | * |
||
470 | * @return MapAPI |
||
471 | */ |
||
472 | |||
473 | public function addArrayMarkerByCoords($coordtab, $category = '', $icon = '') { |
||
479 | |||
480 | |||
481 | /** |
||
482 | * Adds a {@link ViewableData} object that implements {@link Mappable} |
||
483 | * to the map. |
||
484 | * @param $infowindowtemplateparams Optional array of extra parameters to pass to the map info window |
||
485 | * |
||
486 | * @param ViewableData $obj |
||
487 | */ |
||
488 | public function addMarkerAsObject(ViewableData $obj, $infowindowtemplateparams = null) { |
||
523 | |||
524 | |||
525 | /** |
||
526 | * Draws a line between two {@link ViewableData} objects |
||
527 | * |
||
528 | * @param ViewableData $one The first point |
||
529 | * @param ViewableData $two The second point |
||
530 | * @param string $color The hexidecimal color of the line |
||
531 | */ |
||
532 | public function connectPoints(ViewableData $one, ViewableData $two, $color = "#FF3300") { |
||
539 | |||
540 | public function forTemplate() { |
||
545 | |||
546 | /** |
||
547 | * Add a KML file which will be rendered on this map. Normally used for likes |
||
548 | * of GPS traces from activities |
||
549 | * |
||
550 | * @param string $url url of the kml file compatible with gmap and gearth |
||
551 | * |
||
552 | * @return MapAPI |
||
553 | */ |
||
554 | |||
555 | public function addKML($url) { |
||
559 | |||
560 | |||
561 | /* |
||
562 | Add a line to the map |
||
563 | |||
564 | */ |
||
565 | public function addLine($from = array(), $to = array(), $color = "#FF3300") { |
||
577 | |||
578 | |||
579 | /* |
||
580 | For php 5.3 |
||
581 | */ |
||
582 | public static function jsonRemoveUnicodeSequences($struct) { |
||
587 | |||
588 | |||
589 | /** |
||
590 | * Generate the gmap |
||
591 | * |
||
592 | * @return void |
||
593 | */ |
||
594 | |||
595 | public function generate() { |
||
705 | |||
706 | /** |
||
707 | * @param string $templateName |
||
708 | * @param ArrayData $templateVariables |
||
709 | */ |
||
710 | public function processTemplateHTML($templateName, $templateVariables = null) { |
||
718 | } |
||
719 |
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.