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:
Complex classes like Markers 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 Markers, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Markers extends Object |
||
22 | 1 | { |
|
23 | |||
24 | const DROP = 'DROP', BOUNCE = 'BOUNCE'; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $markers = array(); |
||
30 | |||
31 | /** |
||
32 | * @var |
||
33 | */ |
||
34 | private $iconDefaultPath; |
||
35 | |||
36 | /** |
||
37 | * @var bool |
||
38 | */ |
||
39 | private $bound = FALSE; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | private $markerClusterer = FALSE; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $clusterOptions = array(); |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @param array $markers |
||
54 | */ |
||
55 | public function addMarkers(array $markers) |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @param array $position |
||
69 | * @param bool $animation |
||
70 | * @param null|string $title |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function addMarker(array $position, $animation = false, $title = null) |
||
92 | |||
93 | |||
94 | /** |
||
95 | * @return array |
||
96 | */ |
||
97 | public function getMarker() |
||
101 | |||
102 | |||
103 | /** |
||
104 | * @return array |
||
105 | */ |
||
106 | public function getMarkers() |
||
110 | |||
111 | |||
112 | public function deleteMarkers() |
||
116 | |||
117 | |||
118 | /** |
||
119 | * @param $message |
||
120 | * @param bool $autoOpen |
||
121 | * @return $this |
||
122 | * @throws LogicException |
||
123 | */ |
||
124 | public function setMessage($message, $autoOpen = false) |
||
136 | |||
137 | |||
138 | /** |
||
139 | * @param bool $cluster |
||
140 | * @return $this |
||
141 | * @throws InvalidArgumentException |
||
142 | */ |
||
143 | View Code Duplication | public function isMarkerClusterer($cluster = true) |
|
153 | |||
154 | |||
155 | /** |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function getMarkerClusterer() |
||
162 | |||
163 | |||
164 | /** |
||
165 | * @param array $options |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function setClusterOptions($options = array()) |
||
173 | |||
174 | |||
175 | /** |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getClusterOptions() |
||
182 | |||
183 | |||
184 | /** |
||
185 | * @param bool $bound Show all of markers |
||
186 | * @return $this |
||
187 | * @throws InvalidArgumentException |
||
188 | */ |
||
189 | View Code Duplication | public function fitBounds($bound = true) |
|
199 | |||
200 | |||
201 | /** |
||
202 | * @return Boolean |
||
203 | */ |
||
204 | public function getBound() |
||
208 | |||
209 | |||
210 | /** |
||
211 | * @param string|Icon $icon |
||
212 | * @return $this |
||
213 | * @throws LogicException |
||
214 | */ |
||
215 | public function setIcon($icon) |
||
235 | |||
236 | |||
237 | /** |
||
238 | * @param string $defaultPath |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function setDefaultIconPath($defaultPath) |
||
252 | |||
253 | |||
254 | /** |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getDefaultIconPath() |
||
261 | |||
262 | |||
263 | /** |
||
264 | * @param string $color Color can be 24-bit color or: green, purple, yellow, blue, orange, red |
||
265 | * @return $this |
||
266 | */ |
||
267 | public function setColor($color) |
||
284 | |||
285 | |||
286 | /** |
||
287 | * @param array $marker |
||
288 | */ |
||
289 | private function createMarker(array $marker) |
||
345 | } |
||
346 |
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.