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 |
||
12 | class PathBounds |
||
13 | { |
||
14 | private $index; |
||
15 | |||
16 | private $modifier; |
||
17 | /** |
||
18 | * The path points positions |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | private $data = []; |
||
23 | |||
24 | private $current; |
||
25 | |||
26 | private $rect; |
||
27 | |||
28 | private $originalData; |
||
29 | |||
30 | public function __construct() |
||
39 | |||
40 | /** |
||
41 | * @param string $modifier Path modifier e.g. V, L, C, M |
||
42 | * @param array $params |
||
43 | */ |
||
44 | public function addData($modifier, array $params) |
||
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | public function getBox() |
||
99 | |||
100 | private function getLBox() |
||
107 | |||
108 | private function getPreviousData() |
||
114 | |||
115 | private function modifierAtIndex($index) |
||
119 | |||
120 | private function union($x1, $y1, $x2, $y2) |
||
135 | |||
136 | private function getQBox() |
||
145 | |||
146 | private function getCBox() |
||
155 | |||
156 | /** |
||
157 | * @return array |
||
158 | */ |
||
159 | public function getData() |
||
163 | |||
164 | /** |
||
165 | * @return array |
||
166 | */ |
||
167 | private function getStartPoint() |
||
174 | |||
175 | private function getNearest($axis) |
||
182 | |||
183 | /** |
||
184 | * @param string $mod |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | private function isRelativeModifier($mod) |
||
192 | |||
193 | /** |
||
194 | * @param array $data |
||
195 | * |
||
196 | * @return float|false |
||
197 | */ |
||
198 | private function getStartX($data) |
||
202 | |||
203 | /** |
||
204 | * @param array $data |
||
205 | * |
||
206 | * @return float |
||
207 | */ |
||
208 | private function getStartY($data) |
||
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | private function getLastData() |
||
224 | |||
225 | private function isAnyKindOfLine() |
||
230 | } |
||
231 |
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.