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 PathBounds 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 PathBounds, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class PathBounds |
||
13 | { |
||
14 | private $index; |
||
15 | private $modifier; |
||
16 | /** |
||
17 | * The path points positions |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | private $data = []; |
||
22 | |||
23 | private $current; |
||
24 | |||
25 | private $rect; |
||
26 | |||
27 | public function __construct() |
||
36 | |||
37 | public function addData($type, array $params) |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getBox() |
||
84 | |||
85 | private function getLBox() |
||
92 | |||
93 | private function getLRelBox() |
||
103 | |||
104 | private function getPreviousData() |
||
110 | |||
111 | private function modifierAtIndex($index) |
||
115 | |||
116 | private function union($x1, $y1, $x2, $y2) |
||
131 | |||
132 | private function getQBox() |
||
141 | |||
142 | private function getCBox() |
||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | public function getData() |
||
159 | |||
160 | View Code Duplication | private function getHBox() |
|
168 | |||
169 | |||
170 | View Code Duplication | private function getHRelBox() |
|
180 | |||
181 | View Code Duplication | private function getVBox() |
|
189 | |||
190 | |||
191 | View Code Duplication | private function getVRelBox() |
|
201 | |||
202 | /** |
||
203 | * @return array |
||
204 | */ |
||
205 | private function getStartPoint() |
||
212 | |||
213 | private function getNearest($axis) |
||
234 | |||
235 | private function getFirst($axis) |
||
241 | |||
242 | private function getStart($axis, $data) |
||
246 | |||
247 | /** |
||
248 | * @param $index |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | private function isRelativeModifier($index) |
||
256 | |||
257 | /** |
||
258 | * @param $axis |
||
259 | * |
||
260 | * @return mixed |
||
261 | */ |
||
262 | private function vhValue($axis) |
||
276 | |||
277 | /** |
||
278 | * @param $data |
||
279 | * |
||
280 | * @return float|false |
||
281 | */ |
||
282 | private function getStartX($data) |
||
290 | |||
291 | /** |
||
292 | * @param $data |
||
293 | * |
||
294 | * @return float |
||
295 | */ |
||
296 | private function getStartY($data) |
||
303 | } |
||
304 |
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.