Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
26 | class DefaultInfoWindowSubscriber extends AbstractInfoWindowSubscriber |
||
27 | { |
||
28 | /** |
||
29 | * @var DefaultInfoWindowRenderer |
||
30 | */ |
||
31 | private $infoWindowRenderer; |
||
32 | |||
33 | /** |
||
34 | * @param Formatter $formatter |
||
35 | * @param DefaultInfoWindowCollector $infoWindowCollector |
||
36 | * @param DefaultInfoWindowRenderer $infoWindowRenderer |
||
37 | */ |
||
38 | 4 | public function __construct( |
|
47 | |||
48 | /** |
||
49 | * @return DefaultInfoWindowRenderer |
||
50 | */ |
||
51 | public function getInfoWindowRenderer() |
||
55 | |||
56 | /** |
||
57 | * @param DefaultInfoWindowRenderer $infoWindowRenderer |
||
58 | */ |
||
59 | 4 | public function setInfoWindowRenderer(DefaultInfoWindowRenderer $infoWindowRenderer) |
|
63 | |||
64 | /** |
||
65 | * @param MapEvent $event |
||
66 | */ |
||
67 | View Code Duplication | public function handleMap(MapEvent $event) |
|
|
|||
68 | { |
||
69 | $map = $event->getMap(); |
||
70 | $collector = $this->getInfoWindowCollector(); |
||
71 | |||
72 | foreach ($collector->collect($map, [], InfoWindowCollector::STRATEGY_MAP) as $infoWindow) { |
||
73 | $event->addCode($this->renderInfoWindow($map, $infoWindow)); |
||
74 | } |
||
75 | |||
76 | foreach ($collector->collect($map, [], InfoWindowCollector::STRATEGY_MARKER) as $infoWindow) { |
||
77 | $event->addCode($this->renderInfoWindow($map, $infoWindow, false)); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 4 | public static function getSubscribedEvents() |
|
88 | |||
89 | /** |
||
90 | * @param Map $map |
||
91 | * @param InfoWindow $infoWindow |
||
92 | * @param bool $position |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | private function renderInfoWindow(Map $map, InfoWindow $infoWindow, $position = true) |
||
105 | } |
||
106 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.