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 |
||
28 | class Map implements VariableAwareInterface |
||
29 | { |
||
30 | use VariableAwareTrait; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $htmlId = 'map_canvas'; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | private $autoZoom = false; |
||
41 | |||
42 | /** |
||
43 | * @var Coordinate |
||
44 | */ |
||
45 | private $center; |
||
46 | |||
47 | /** |
||
48 | * @var Bound |
||
49 | */ |
||
50 | private $bound; |
||
51 | |||
52 | /** |
||
53 | * @var ControlManager |
||
54 | */ |
||
55 | private $controlManager; |
||
56 | |||
57 | /** |
||
58 | * @var EventManager |
||
59 | */ |
||
60 | private $eventManager; |
||
61 | |||
62 | /** |
||
63 | * @var LayerManager |
||
64 | */ |
||
65 | private $layerManager; |
||
66 | |||
67 | /** |
||
68 | * @var OverlayManager |
||
69 | */ |
||
70 | private $overlayManager; |
||
71 | |||
72 | /** |
||
73 | * @var string[] |
||
74 | */ |
||
75 | private $libraries = []; |
||
76 | |||
77 | /** |
||
78 | * @var mixed[] |
||
79 | */ |
||
80 | private $mapOptions = []; |
||
81 | |||
82 | /** |
||
83 | * @var string[] |
||
84 | */ |
||
85 | private $stylesheetOptions = []; |
||
86 | |||
87 | /** |
||
88 | * @var string[] |
||
89 | */ |
||
90 | private $htmlAttributes = []; |
||
91 | |||
92 | public function __construct() |
||
93 | { |
||
94 | $this->setVariablePrefix('map'); |
||
95 | $this->setCenter(new Coordinate()); |
||
96 | $this->setBound(new Bound()); |
||
97 | $this->setControlManager(new ControlManager()); |
||
98 | $this->setEventManager(new EventManager()); |
||
99 | $this->setOverlayManager(new OverlayManager()); |
||
100 | $this->setLayerManager(new LayerManager()); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getHtmlId() |
||
110 | |||
111 | /** |
||
112 | * @param string $htmlId |
||
113 | */ |
||
114 | public function setHtmlId($htmlId) |
||
118 | |||
119 | /** |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function isAutoZoom() |
||
123 | { |
||
124 | return $this->autoZoom; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param bool $autoZoom |
||
129 | */ |
||
130 | public function setAutoZoom($autoZoom) |
||
131 | { |
||
132 | $this->autoZoom = $autoZoom; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return Coordinate |
||
137 | */ |
||
138 | public function getCenter() |
||
139 | { |
||
140 | return $this->center; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param Coordinate $center |
||
145 | */ |
||
146 | public function setCenter(Coordinate $center) |
||
147 | { |
||
148 | $this->center = $center; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return Bound |
||
153 | */ |
||
154 | public function getBound() |
||
155 | { |
||
156 | return $this->bound; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param Bound $bound |
||
161 | */ |
||
162 | public function setBound(Bound $bound) |
||
163 | { |
||
164 | $this->bound = $bound; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @return ControlManager |
||
169 | */ |
||
170 | public function getControlManager() |
||
174 | |||
175 | /** |
||
176 | * @param ControlManager $controlManager |
||
177 | */ |
||
178 | public function setControlManager(ControlManager $controlManager) |
||
179 | { |
||
180 | $this->controlManager = $controlManager; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return EventManager |
||
185 | */ |
||
186 | public function getEventManager() |
||
190 | |||
191 | /** |
||
192 | * @param EventManager $eventManager |
||
193 | */ |
||
194 | public function setEventManager(EventManager $eventManager) |
||
198 | |||
199 | /** |
||
200 | * @return LayerManager |
||
201 | */ |
||
202 | public function getLayerManager() |
||
206 | |||
207 | /** |
||
208 | * @param LayerManager $layerManager |
||
209 | */ |
||
210 | public function setLayerManager(LayerManager $layerManager) |
||
218 | |||
219 | /** |
||
220 | * @return OverlayManager |
||
221 | */ |
||
222 | public function getOverlayManager() |
||
226 | |||
227 | /** |
||
228 | * @param OverlayManager $overlayManager |
||
229 | */ |
||
230 | public function setOverlayManager(OverlayManager $overlayManager) |
||
238 | |||
239 | /** |
||
240 | * @return bool |
||
241 | */ |
||
242 | public function hasLibraries() |
||
246 | |||
247 | /** |
||
248 | * @return string[] |
||
249 | */ |
||
250 | public function getLibraries() |
||
254 | |||
255 | /** |
||
256 | * @param string[] $libraries |
||
257 | */ |
||
258 | public function setLibraries(array $libraries) |
||
263 | |||
264 | /** |
||
265 | * @param string[] $libraries |
||
266 | */ |
||
267 | public function addLibraries(array $libraries) |
||
273 | |||
274 | /** |
||
275 | * @param string $library |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function hasLibrary($library) |
||
283 | |||
284 | /** |
||
285 | * @param string $library |
||
286 | */ |
||
287 | public function addLibrary($library) |
||
293 | |||
294 | /** |
||
295 | * @param string $library |
||
296 | */ |
||
297 | public function removeLibrary($library) |
||
301 | |||
302 | /** |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function hasMapOptions() |
||
309 | |||
310 | /** |
||
311 | * @return mixed[] |
||
312 | */ |
||
313 | public function getMapOptions() |
||
317 | |||
318 | /** |
||
319 | * @param mixed[] $mapOptions |
||
320 | */ |
||
321 | public function setMapOptions(array $mapOptions) |
||
326 | |||
327 | /** |
||
328 | * @param mixed[] $mapOptions |
||
329 | */ |
||
330 | public function addMapOptions(array $mapOptions) |
||
336 | |||
337 | /** |
||
338 | * @param string $mapOption |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | public function hasMapOption($mapOption) |
||
346 | |||
347 | /** |
||
348 | * @param string $mapOption |
||
349 | * |
||
350 | * @return mixed |
||
351 | */ |
||
352 | public function getMapOption($mapOption) |
||
356 | |||
357 | /** |
||
358 | * @param string $mapOption |
||
359 | * @param mixed $value |
||
360 | */ |
||
361 | public function setMapOption($mapOption, $value) |
||
365 | |||
366 | /** |
||
367 | * @param string $mapOption |
||
368 | */ |
||
369 | public function removeMapOption($mapOption) |
||
373 | |||
374 | /** |
||
375 | * @return bool |
||
376 | */ |
||
377 | public function hasStylesheetOptions() |
||
381 | |||
382 | /** |
||
383 | * @return string[] |
||
384 | */ |
||
385 | public function getStylesheetOptions() |
||
389 | |||
390 | /** |
||
391 | * @param string[] $stylesheetOptions |
||
392 | */ |
||
393 | public function setStylesheetOptions(array $stylesheetOptions) |
||
398 | |||
399 | /** |
||
400 | * @param string[] $stylesheetOptions |
||
401 | */ |
||
402 | public function addStylesheetOptions(array $stylesheetOptions) |
||
408 | |||
409 | /** |
||
410 | * @param string $stylesheetOption |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | public function hasStylesheetOption($stylesheetOption) |
||
418 | |||
419 | /** |
||
420 | * @param string $stylesheetOption |
||
421 | * |
||
422 | * @return string|null |
||
423 | */ |
||
424 | public function getStylesheetOption($stylesheetOption) |
||
428 | |||
429 | /** |
||
430 | * @param string $stylesheetOption |
||
431 | * @param string $value |
||
432 | */ |
||
433 | public function setStylesheetOption($stylesheetOption, $value) |
||
437 | |||
438 | /** |
||
439 | * @param string $stylesheetOption |
||
440 | */ |
||
441 | public function removeStylesheetOption($stylesheetOption) |
||
445 | |||
446 | /** |
||
447 | * @return bool |
||
448 | */ |
||
449 | public function hasHtmlAttributes() |
||
453 | |||
454 | /** |
||
455 | * @return string[] |
||
456 | */ |
||
457 | public function getHtmlAttributes() |
||
461 | |||
462 | /** |
||
463 | * @param string[] $htmlAttributes |
||
464 | */ |
||
465 | public function setHtmlAttributes(array $htmlAttributes) |
||
470 | |||
471 | /** |
||
472 | * @param string[] $htmlAttributes |
||
473 | */ |
||
474 | public function addHtmlAttributes(array $htmlAttributes) |
||
480 | |||
481 | /** |
||
482 | * @param string $htmlAttribute |
||
483 | * |
||
484 | * @return bool |
||
485 | */ |
||
486 | public function hasHtmlAttribute($htmlAttribute) |
||
490 | |||
491 | /** |
||
492 | * @param string $htmlAttribute |
||
493 | * |
||
494 | * @return string|null |
||
495 | */ |
||
496 | public function getHtmlAttribute($htmlAttribute) |
||
500 | |||
501 | /** |
||
502 | * @param string $htmlAttribute |
||
503 | * @param string $value |
||
504 | */ |
||
505 | public function setHtmlAttribute($htmlAttribute, $value) |
||
509 | |||
510 | /** |
||
511 | * @param string $htmlAttribute |
||
512 | */ |
||
513 | public function removeHtmlAttribute($htmlAttribute) |
||
517 | } |
||
518 |