Complex classes like Map 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 Map, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Map implements VariableAwareInterface, StaticOptionsAwareInterface |
||
31 | { |
||
32 | use StaticOptionsAwareTrait; |
||
33 | use VariableAwareTrait; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $htmlId = 'map_canvas'; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $autoZoom = false; |
||
44 | |||
45 | /** |
||
46 | * @var Coordinate |
||
47 | */ |
||
48 | private $center; |
||
49 | |||
50 | /** |
||
51 | * @var Bound |
||
52 | */ |
||
53 | private $bound; |
||
54 | |||
55 | /** |
||
56 | * @var ControlManager |
||
57 | */ |
||
58 | private $controlManager; |
||
59 | |||
60 | /** |
||
61 | * @var EventManager |
||
62 | */ |
||
63 | private $eventManager; |
||
64 | |||
65 | /** |
||
66 | * @var LayerManager |
||
67 | */ |
||
68 | private $layerManager; |
||
69 | |||
70 | /** |
||
71 | * @var OverlayManager |
||
72 | */ |
||
73 | private $overlayManager; |
||
74 | |||
75 | /** |
||
76 | * @var string[] |
||
77 | */ |
||
78 | private $libraries = []; |
||
79 | |||
80 | /** |
||
81 | * @var mixed[] |
||
82 | */ |
||
83 | private $mapOptions = []; |
||
84 | |||
85 | /** |
||
86 | * @var string[] |
||
87 | */ |
||
88 | private $stylesheetOptions = []; |
||
89 | |||
90 | /** |
||
91 | * @var string[] |
||
92 | */ |
||
93 | private $htmlAttributes = []; |
||
94 | |||
95 | 380 | public function __construct() |
|
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | 40 | public function getHtmlId() |
|
112 | |||
113 | /** |
||
114 | * @param string $htmlId |
||
115 | */ |
||
116 | 4 | public function setHtmlId($htmlId) |
|
120 | |||
121 | /** |
||
122 | * @return bool |
||
123 | */ |
||
124 | 152 | public function isAutoZoom() |
|
128 | |||
129 | /** |
||
130 | * @param bool $autoZoom |
||
131 | */ |
||
132 | 16 | public function setAutoZoom($autoZoom) |
|
136 | |||
137 | /** |
||
138 | * @return Coordinate |
||
139 | */ |
||
140 | 44 | public function getCenter() |
|
144 | |||
145 | 380 | public function setCenter(Coordinate $center) |
|
149 | |||
150 | /** |
||
151 | * @return Bound |
||
152 | */ |
||
153 | 24 | public function getBound() |
|
157 | |||
158 | 380 | public function setBound(Bound $bound) |
|
162 | |||
163 | /** |
||
164 | * @return ControlManager |
||
165 | */ |
||
166 | 28 | public function getControlManager() |
|
170 | |||
171 | 380 | public function setControlManager(ControlManager $controlManager) |
|
175 | |||
176 | /** |
||
177 | * @return EventManager |
||
178 | */ |
||
179 | 24 | public function getEventManager() |
|
183 | |||
184 | 380 | public function setEventManager(EventManager $eventManager) |
|
188 | |||
189 | /** |
||
190 | * @return LayerManager |
||
191 | */ |
||
192 | 380 | public function getLayerManager() |
|
196 | |||
197 | 380 | public function setLayerManager(LayerManager $layerManager) |
|
205 | |||
206 | /** |
||
207 | * @return OverlayManager |
||
208 | */ |
||
209 | 416 | public function getOverlayManager() |
|
213 | |||
214 | 380 | public function setOverlayManager(OverlayManager $overlayManager) |
|
222 | |||
223 | /** |
||
224 | * @return bool |
||
225 | */ |
||
226 | 20 | public function hasLibraries() |
|
230 | |||
231 | /** |
||
232 | * @return string[] |
||
233 | */ |
||
234 | 20 | public function getLibraries() |
|
238 | |||
239 | /** |
||
240 | * @param string[] $libraries |
||
241 | */ |
||
242 | 8 | public function setLibraries(array $libraries) |
|
247 | |||
248 | /** |
||
249 | * @param string[] $libraries |
||
250 | */ |
||
251 | 8 | public function addLibraries(array $libraries) |
|
257 | |||
258 | /** |
||
259 | * @param string $library |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | 16 | public function hasLibrary($library) |
|
267 | |||
268 | /** |
||
269 | * @param string $library |
||
270 | */ |
||
271 | 16 | public function addLibrary($library) |
|
277 | |||
278 | /** |
||
279 | * @param string $library |
||
280 | */ |
||
281 | 4 | public function removeLibrary($library) |
|
286 | |||
287 | /** |
||
288 | * @return bool |
||
289 | */ |
||
290 | 20 | public function hasMapOptions() |
|
294 | |||
295 | /** |
||
296 | * @return mixed[] |
||
297 | */ |
||
298 | 36 | public function getMapOptions() |
|
302 | |||
303 | /** |
||
304 | * @param mixed[] $mapOptions |
||
305 | */ |
||
306 | 12 | public function setMapOptions(array $mapOptions) |
|
311 | |||
312 | /** |
||
313 | * @param mixed[] $mapOptions |
||
314 | */ |
||
315 | 12 | public function addMapOptions(array $mapOptions) |
|
321 | |||
322 | /** |
||
323 | * @param string $mapOption |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | 28 | public function hasMapOption($mapOption) |
|
331 | |||
332 | /** |
||
333 | * @param string $mapOption |
||
334 | * |
||
335 | * @return mixed |
||
336 | */ |
||
337 | 28 | public function getMapOption($mapOption) |
|
341 | |||
342 | /** |
||
343 | * @param string $mapOption |
||
344 | * @param mixed $value |
||
345 | */ |
||
346 | 28 | public function setMapOption($mapOption, $value) |
|
350 | |||
351 | /** |
||
352 | * @param string $mapOption |
||
353 | */ |
||
354 | 4 | public function removeMapOption($mapOption) |
|
358 | |||
359 | /** |
||
360 | * @return bool |
||
361 | */ |
||
362 | 20 | public function hasStylesheetOptions() |
|
366 | |||
367 | /** |
||
368 | * @return string[] |
||
369 | */ |
||
370 | 20 | public function getStylesheetOptions() |
|
374 | |||
375 | /** |
||
376 | * @param string[] $stylesheetOptions |
||
377 | */ |
||
378 | 8 | public function setStylesheetOptions(array $stylesheetOptions) |
|
383 | |||
384 | /** |
||
385 | * @param string[] $stylesheetOptions |
||
386 | */ |
||
387 | 8 | public function addStylesheetOptions(array $stylesheetOptions) |
|
393 | |||
394 | /** |
||
395 | * @param string $stylesheetOption |
||
396 | * |
||
397 | * @return bool |
||
398 | */ |
||
399 | 28 | public function hasStylesheetOption($stylesheetOption) |
|
403 | |||
404 | /** |
||
405 | * @param string $stylesheetOption |
||
406 | * |
||
407 | * @return string|null |
||
408 | */ |
||
409 | 16 | public function getStylesheetOption($stylesheetOption) |
|
413 | |||
414 | /** |
||
415 | * @param string $stylesheetOption |
||
416 | * @param string $value |
||
417 | */ |
||
418 | 20 | public function setStylesheetOption($stylesheetOption, $value) |
|
422 | |||
423 | /** |
||
424 | * @param string $stylesheetOption |
||
425 | */ |
||
426 | 4 | public function removeStylesheetOption($stylesheetOption) |
|
430 | |||
431 | /** |
||
432 | * @return bool |
||
433 | */ |
||
434 | 20 | public function hasHtmlAttributes() |
|
438 | |||
439 | /** |
||
440 | * @return string[] |
||
441 | */ |
||
442 | 36 | public function getHtmlAttributes() |
|
446 | |||
447 | /** |
||
448 | * @param string[] $htmlAttributes |
||
449 | */ |
||
450 | 12 | public function setHtmlAttributes(array $htmlAttributes) |
|
455 | |||
456 | /** |
||
457 | * @param string[] $htmlAttributes |
||
458 | */ |
||
459 | 12 | public function addHtmlAttributes(array $htmlAttributes) |
|
465 | |||
466 | /** |
||
467 | * @param string $htmlAttribute |
||
468 | * |
||
469 | * @return bool |
||
470 | */ |
||
471 | 12 | public function hasHtmlAttribute($htmlAttribute) |
|
475 | |||
476 | /** |
||
477 | * @param string $htmlAttribute |
||
478 | * |
||
479 | * @return string|null |
||
480 | */ |
||
481 | 12 | public function getHtmlAttribute($htmlAttribute) |
|
485 | |||
486 | /** |
||
487 | * @param string $htmlAttribute |
||
488 | * @param string $value |
||
489 | */ |
||
490 | 20 | public function setHtmlAttribute($htmlAttribute, $value) |
|
494 | |||
495 | /** |
||
496 | * @param string $htmlAttribute |
||
497 | */ |
||
498 | 4 | public function removeHtmlAttribute($htmlAttribute) |
|
502 | } |
||
503 |