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) |
||
214 | |||
215 | /** |
||
216 | * @return OverlayManager |
||
217 | */ |
||
218 | public function getOverlayManager() |
||
222 | |||
223 | /** |
||
224 | * @param OverlayManager $overlayManager |
||
225 | */ |
||
226 | public function setOverlayManager(OverlayManager $overlayManager) |
||
234 | |||
235 | /** |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function hasLibraries() |
||
242 | |||
243 | /** |
||
244 | * @return string[] |
||
245 | */ |
||
246 | public function getLibraries() |
||
250 | |||
251 | /** |
||
252 | * @param string[] $libraries |
||
253 | */ |
||
254 | public function setLibraries(array $libraries) |
||
259 | |||
260 | /** |
||
261 | * @param string[] $libraries |
||
262 | */ |
||
263 | public function addLibraries(array $libraries) |
||
269 | |||
270 | /** |
||
271 | * @param string $library |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function hasLibrary($library) |
||
279 | |||
280 | /** |
||
281 | * @param string $library |
||
282 | */ |
||
283 | public function addLibrary($library) |
||
289 | |||
290 | /** |
||
291 | * @param string $library |
||
292 | */ |
||
293 | public function removeLibrary($library) |
||
297 | |||
298 | /** |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function hasMapOptions() |
||
305 | |||
306 | /** |
||
307 | * @return mixed[] |
||
308 | */ |
||
309 | public function getMapOptions() |
||
313 | |||
314 | /** |
||
315 | * @param mixed[] $mapOptions |
||
316 | */ |
||
317 | public function setMapOptions(array $mapOptions) |
||
322 | |||
323 | /** |
||
324 | * @param mixed[] $mapOptions |
||
325 | */ |
||
326 | public function addMapOptions(array $mapOptions) |
||
332 | |||
333 | /** |
||
334 | * @param string $mapOption |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | public function hasMapOption($mapOption) |
||
342 | |||
343 | /** |
||
344 | * @param string $mapOption |
||
345 | * |
||
346 | * @return mixed |
||
347 | */ |
||
348 | public function getMapOption($mapOption) |
||
352 | |||
353 | /** |
||
354 | * @param string $mapOption |
||
355 | * @param mixed $value |
||
356 | */ |
||
357 | public function setMapOption($mapOption, $value) |
||
361 | |||
362 | /** |
||
363 | * @param string $mapOption |
||
364 | */ |
||
365 | public function removeMapOption($mapOption) |
||
369 | |||
370 | /** |
||
371 | * @return bool |
||
372 | */ |
||
373 | public function hasStylesheetOptions() |
||
377 | |||
378 | /** |
||
379 | * @return string[] |
||
380 | */ |
||
381 | public function getStylesheetOptions() |
||
385 | |||
386 | /** |
||
387 | * @param string[] $stylesheetOptions |
||
388 | */ |
||
389 | public function setStylesheetOptions(array $stylesheetOptions) |
||
394 | |||
395 | /** |
||
396 | * @param string[] $stylesheetOptions |
||
397 | */ |
||
398 | public function addStylesheetOptions(array $stylesheetOptions) |
||
404 | |||
405 | /** |
||
406 | * @param string $stylesheetOption |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | public function hasStylesheetOption($stylesheetOption) |
||
414 | |||
415 | /** |
||
416 | * @param string $stylesheetOption |
||
417 | * |
||
418 | * @return string|null |
||
419 | */ |
||
420 | public function getStylesheetOption($stylesheetOption) |
||
424 | |||
425 | /** |
||
426 | * @param string $stylesheetOption |
||
427 | * @param string $value |
||
428 | */ |
||
429 | public function setStylesheetOption($stylesheetOption, $value) |
||
433 | |||
434 | /** |
||
435 | * @param string $stylesheetOption |
||
436 | */ |
||
437 | public function removeStylesheetOption($stylesheetOption) |
||
441 | |||
442 | /** |
||
443 | * @return bool |
||
444 | */ |
||
445 | public function hasHtmlAttributes() |
||
449 | |||
450 | /** |
||
451 | * @return string[] |
||
452 | */ |
||
453 | public function getHtmlAttributes() |
||
457 | |||
458 | /** |
||
459 | * @param string[] $htmlAttributes |
||
460 | */ |
||
461 | public function setHtmlAttributes(array $htmlAttributes) |
||
466 | |||
467 | /** |
||
468 | * @param string[] $htmlAttributes |
||
469 | */ |
||
470 | public function addHtmlAttributes(array $htmlAttributes) |
||
476 | |||
477 | /** |
||
478 | * @param string $htmlAttribute |
||
479 | * |
||
480 | * @return bool |
||
481 | */ |
||
482 | public function hasHtmlAttribute($htmlAttribute) |
||
486 | |||
487 | /** |
||
488 | * @param string $htmlAttribute |
||
489 | * |
||
490 | * @return string|null |
||
491 | */ |
||
492 | public function getHtmlAttribute($htmlAttribute) |
||
496 | |||
497 | /** |
||
498 | * @param string $htmlAttribute |
||
499 | * @param string $value |
||
500 | */ |
||
501 | public function setHtmlAttribute($htmlAttribute, $value) |
||
505 | |||
506 | /** |
||
507 | * @param string $htmlAttribute |
||
508 | */ |
||
509 | public function removeHtmlAttribute($htmlAttribute) |
||
513 | } |
||
514 |