Complex classes like OverlayManager 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 OverlayManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class OverlayManager |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var Map|null |
||
| 23 | */ |
||
| 24 | private $map; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var MarkerCluster |
||
| 28 | */ |
||
| 29 | private $markerCluster; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var InfoWindow[] |
||
| 33 | */ |
||
| 34 | private $infoWindows = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Polyline[] |
||
| 38 | */ |
||
| 39 | private $polylines = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var EncodedPolyline[] |
||
| 43 | */ |
||
| 44 | private $encodedPolylines = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Polygon[] |
||
| 48 | */ |
||
| 49 | private $polygons = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Rectangle[] |
||
| 53 | */ |
||
| 54 | private $rectangles = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Circle[] |
||
| 58 | */ |
||
| 59 | private $circles = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var GroundOverlay[] |
||
| 63 | */ |
||
| 64 | private $groundOverlays = []; |
||
| 65 | |||
| 66 | 960 | public function __construct() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | 504 | public function hasMap() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @return Map|null |
||
| 81 | */ |
||
| 82 | 928 | public function getMap() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @param Map $map |
||
| 89 | */ |
||
| 90 | 960 | public function setMap(Map $map) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @return MarkerCluster |
||
| 101 | */ |
||
| 102 | 960 | public function getMarkerCluster() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param MarkerCluster $markerCluster |
||
| 109 | */ |
||
| 110 | 960 | public function setMarkerCluster(MarkerCluster $markerCluster) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | 8 | public function hasMarkers() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @return Marker[] |
||
| 129 | */ |
||
| 130 | 424 | public function getMarkers() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param Marker[] $markers |
||
| 137 | */ |
||
| 138 | 4 | public function setMarkers(array $markers) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param Marker[] $markers |
||
| 145 | */ |
||
| 146 | 4 | public function addMarkers(array $markers) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param Marker $marker |
||
| 153 | * |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | 4 | public function hasMarker(Marker $marker) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param Marker $marker |
||
| 163 | */ |
||
| 164 | 100 | public function addMarker(Marker $marker) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param Marker $marker |
||
| 171 | */ |
||
| 172 | 4 | public function removeMarker(Marker $marker) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | 32 | public function hasInfoWindows() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return InfoWindow[] |
||
| 187 | */ |
||
| 188 | 320 | public function getInfoWindows() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param InfoWindow[] $infoWindows |
||
| 195 | */ |
||
| 196 | 16 | public function setInfoWindows(array $infoWindows) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @param InfoWindow[] $infoWindows |
||
| 207 | */ |
||
| 208 | 16 | public function addInfoWindows(array $infoWindows) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param InfoWindow $infoWindow |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | 72 | public function hasInfoWindow(InfoWindow $infoWindow) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param InfoWindow $infoWindow |
||
| 227 | */ |
||
| 228 | 72 | public function addInfoWindow(InfoWindow $infoWindow) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param InfoWindow $infoWindow |
||
| 239 | */ |
||
| 240 | 16 | public function removeInfoWindow(InfoWindow $infoWindow) |
|
| 241 | { |
||
| 242 | 16 | unset($this->infoWindows[array_search($infoWindow, $this->infoWindows, true)]); |
|
| 243 | 16 | $this->infoWindows = empty($this->infoWindows) ? [] : array_values($this->infoWindows); |
|
| 244 | 16 | $this->removeExtendable($infoWindow); |
|
| 245 | 16 | } |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | 32 | public function hasPolylines() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @return Polyline[] |
||
| 257 | */ |
||
| 258 | 400 | public function getPolylines() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param Polyline[] $polylines |
||
| 265 | */ |
||
| 266 | 16 | public function setPolylines(array $polylines) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @param Polyline[] $polylines |
||
| 277 | */ |
||
| 278 | 16 | public function addPolylines(array $polylines) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param Polyline $polyline |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | 80 | public function hasPolyline(Polyline $polyline) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param Polyline $polyline |
||
| 297 | */ |
||
| 298 | 80 | public function addPolyline(Polyline $polyline) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @param Polyline $polyline |
||
| 309 | */ |
||
| 310 | 16 | public function removePolyline(Polyline $polyline) |
|
| 311 | { |
||
| 312 | 16 | unset($this->polylines[array_search($polyline, $this->polylines, true)]); |
|
| 313 | 16 | $this->polylines = empty($this->polylines) ? [] : array_values($this->polylines); |
|
| 314 | 16 | $this->removeExtendable($polyline); |
|
| 315 | 16 | } |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | 32 | public function hasEncodedPolylines() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @return EncodedPolyline[] |
||
| 327 | */ |
||
| 328 | 356 | public function getEncodedPolylines() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param EncodedPolyline[] $encodedPolylines |
||
| 335 | */ |
||
| 336 | 16 | public function setEncodedPolylines(array $encodedPolylines) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * @param EncodedPolyline[] $encodedPolylines |
||
| 347 | */ |
||
| 348 | 16 | public function addEncodedPolylines(array $encodedPolylines) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param EncodedPolyline $encodedPolyline |
||
| 357 | * |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | 60 | public function hasEncodedPolyline(EncodedPolyline $encodedPolyline) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param EncodedPolyline $encodedPolyline |
||
| 367 | */ |
||
| 368 | 60 | public function addEncodedPolyline(EncodedPolyline $encodedPolyline) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param EncodedPolyline $encodedPolyline |
||
| 379 | */ |
||
| 380 | 16 | public function removeEncodedPolyline(EncodedPolyline $encodedPolyline) |
|
| 381 | { |
||
| 382 | 16 | unset($this->encodedPolylines[array_search($encodedPolyline, $this->encodedPolylines, true)]); |
|
| 383 | 16 | $this->encodedPolylines = empty($this->encodedPolylines) ? [] : array_values($this->encodedPolylines); |
|
| 384 | 16 | $this->removeExtendable($encodedPolyline); |
|
| 385 | 16 | } |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | 32 | public function hasPolygons() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * @return Polygon[] |
||
| 397 | */ |
||
| 398 | 288 | public function getPolygons() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param Polygon[] $polygons |
||
| 405 | */ |
||
| 406 | 16 | public function setPolygons(array $polygons) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @param Polygon[] $polygons |
||
| 417 | */ |
||
| 418 | 16 | public function addPolygons(array $polygons) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * @param Polygon $polygon |
||
| 427 | * |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | 48 | public function hasPolygon(Polygon $polygon) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param Polygon $polygon |
||
| 437 | */ |
||
| 438 | 48 | public function addPolygon(Polygon $polygon) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @param Polygon $polygon |
||
| 449 | */ |
||
| 450 | 16 | public function removePolygon(Polygon $polygon) |
|
| 451 | { |
||
| 452 | 16 | unset($this->polygons[array_search($polygon, $this->polygons, true)]); |
|
| 453 | 16 | $this->polygons = empty($this->polygons) ? [] : array_values($this->polygons); |
|
| 454 | 16 | $this->removeExtendable($polygon); |
|
| 455 | 16 | } |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | 32 | public function hasRectangles() |
|
| 464 | |||
| 465 | /** |
||
| 466 | * @return Rectangle[] |
||
| 467 | */ |
||
| 468 | 304 | public function getRectangles() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * @param Rectangle[] $rectangles |
||
| 475 | */ |
||
| 476 | 16 | public function setRectangles(array $rectangles) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * @param Rectangle[] $rectangles |
||
| 487 | */ |
||
| 488 | 16 | public function addRectangles(array $rectangles) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * @param Rectangle $rectangle |
||
| 497 | * |
||
| 498 | * @return bool |
||
| 499 | */ |
||
| 500 | 52 | public function hasRectangle(Rectangle $rectangle) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @param Rectangle $rectangle |
||
| 507 | */ |
||
| 508 | 52 | public function addRectangle(Rectangle $rectangle) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * @param Rectangle $rectangle |
||
| 519 | */ |
||
| 520 | 16 | public function removeRectangle(Rectangle $rectangle) |
|
| 521 | { |
||
| 522 | 16 | unset($this->rectangles[array_search($rectangle, $this->rectangles, true)]); |
|
| 523 | 16 | $this->rectangles = empty($this->rectangles) ? [] : array_values($this->rectangles); |
|
| 524 | 16 | $this->removeExtendable($rectangle); |
|
| 525 | 16 | } |
|
| 526 | |||
| 527 | /** |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | 32 | public function hasCircles() |
|
| 534 | |||
| 535 | /** |
||
| 536 | * @return Circle[] |
||
| 537 | */ |
||
| 538 | 288 | public function getCircles() |
|
| 542 | |||
| 543 | /** |
||
| 544 | * @param Circle[] $circles |
||
| 545 | */ |
||
| 546 | 16 | public function setCircles(array $circles) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * @param Circle[] $circles |
||
| 557 | */ |
||
| 558 | 16 | public function addCircles(array $circles) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * @param Circle $circle |
||
| 567 | * |
||
| 568 | * @return bool |
||
| 569 | */ |
||
| 570 | 48 | public function hasCircle(Circle $circle) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * @param Circle $circle |
||
| 577 | */ |
||
| 578 | 48 | public function addCircle(Circle $circle) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * @param Circle $circle |
||
| 589 | */ |
||
| 590 | 16 | public function removeCircle(Circle $circle) |
|
| 591 | { |
||
| 592 | 16 | unset($this->circles[array_search($circle, $this->circles, true)]); |
|
| 593 | 16 | $this->circles = empty($this->circles) ? [] : array_values($this->circles); |
|
| 594 | 16 | $this->removeExtendable($circle); |
|
| 595 | 16 | } |
|
| 596 | |||
| 597 | /** |
||
| 598 | * @return bool |
||
| 599 | */ |
||
| 600 | 32 | public function hasGroundOverlays() |
|
| 604 | |||
| 605 | /** |
||
| 606 | * @return GroundOverlay[] |
||
| 607 | */ |
||
| 608 | 304 | public function getGroundOverlays() |
|
| 612 | |||
| 613 | /** |
||
| 614 | * @param GroundOverlay[] $groundOverlays |
||
| 615 | */ |
||
| 616 | 16 | public function setGroundOverlays(array $groundOverlays) |
|
| 624 | |||
| 625 | /** |
||
| 626 | * @param GroundOverlay[] $groundOverlays |
||
| 627 | */ |
||
| 628 | 16 | public function addGroundOverlays(array $groundOverlays) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * @param GroundOverlay $groundOverlay |
||
| 637 | * |
||
| 638 | * @return bool |
||
| 639 | */ |
||
| 640 | 48 | public function hasGroundOverlay(GroundOverlay $groundOverlay) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * @param GroundOverlay $groundOverlay |
||
| 647 | */ |
||
| 648 | 48 | public function addGroundOverlay(GroundOverlay $groundOverlay) |
|
| 656 | |||
| 657 | /** |
||
| 658 | * @param GroundOverlay $groundOverlay |
||
| 659 | */ |
||
| 660 | 16 | public function removeGroundOverlay(GroundOverlay $groundOverlay) |
|
| 661 | { |
||
| 662 | 16 | unset($this->groundOverlays[array_search($groundOverlay, $this->groundOverlays, true)]); |
|
| 663 | 16 | $this->groundOverlays = empty($this->groundOverlays) ? [] : array_values($this->groundOverlays); |
|
| 664 | 16 | $this->removeExtendable($groundOverlay); |
|
| 665 | 16 | } |
|
| 666 | |||
| 667 | /** |
||
| 668 | * @param ExtendableInterface $extendable |
||
| 669 | */ |
||
| 670 | 404 | private function addExtendable(ExtendableInterface $extendable) |
|
| 676 | |||
| 677 | /** |
||
| 678 | * @param ExtendableInterface $extendable |
||
| 679 | */ |
||
| 680 | 112 | private function removeExtendable(ExtendableInterface $extendable) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * @return bool |
||
| 689 | */ |
||
| 690 | 404 | private function isAutoZoom() |
|
| 694 | } |
||
| 695 |