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 | |||
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() |
||
31 | { |
||
32 | $this->rect = [ |
||
33 | 'width' => 0, |
||
34 | 'height' => 0, |
||
35 | 'x' => null, |
||
36 | 'y' => null, |
||
37 | ]; |
||
38 | } |
||
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) |
||
45 | { |
||
46 | $this->originalData = [$modifier => $params]; |
||
47 | if ($this->isRelativeModifier($modifier)) { |
||
48 | $this->addRelativeModifier($modifier, $params); |
||
49 | } else { |
||
50 | $this->addAbsoluteModifier($modifier, $params); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | public function getBox() |
||
58 | { |
||
59 | foreach ($this->data as $key => $value) { |
||
60 | $this->modifier = key($value); |
||
61 | $this->index = $key; |
||
62 | $this->current = $value[$this->modifier]; |
||
63 | |||
64 | if ($this->isAnyKindOfLine()) { |
||
65 | $this->getLBox(); |
||
66 | } elseif ($this->modifier === 'Q' || $this->modifier === 'q') { |
||
67 | $this->getQBox(); |
||
68 | } elseif ($this->modifier === 'C' || $this->modifier === 'c') { |
||
69 | $this->getCBox(); |
||
70 | } |
||
71 | } |
||
72 | unset($this->modifier, $this->index, $this->current); |
||
73 | |||
74 | return $this->rect; |
||
75 | } |
||
76 | |||
77 | private function getLBox() |
||
78 | { |
||
79 | list($x1, $y1) = $this->getStartPoint(); |
||
80 | list($x2, $y2) = $this->current; |
||
81 | |||
82 | $this->union($x1, $y1, $x2, $y2); |
||
83 | } |
||
84 | |||
85 | private function getPreviousData() |
||
86 | { |
||
87 | $mod = $this->modifierAtIndex($this->index - 1); |
||
88 | |||
89 | return $this->data[$this->index - 1][$mod]; |
||
90 | } |
||
91 | |||
92 | private function modifierAtIndex($index) |
||
96 | |||
97 | private function union($x1, $y1, $x2, $y2) |
||
98 | { |
||
99 | $box = [ |
||
100 | 'width' => max($x1, $x2) - min($x1, $x2), |
||
101 | 'height' => max($y1, $y2) - min($y1, $y2), |
||
112 | |||
113 | private function getQBox() |
||
122 | |||
123 | private function getCBox() |
||
132 | |||
133 | /** |
||
134 | * @return array |
||
135 | */ |
||
136 | private function getStartPoint() |
||
143 | |||
144 | private function getNearest($axis) |
||
151 | |||
152 | /** |
||
153 | * @param string $mod |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | private function isRelativeModifier($mod) |
||
161 | |||
162 | /** |
||
163 | * @param array $data |
||
164 | * |
||
165 | * @return float|false |
||
166 | */ |
||
167 | private function getStartX($data) |
||
171 | |||
172 | /** |
||
173 | * @param array $data |
||
174 | * |
||
175 | * @return float |
||
176 | */ |
||
177 | private function getStartY($data) |
||
181 | |||
182 | /** |
||
183 | * @return array |
||
184 | */ |
||
185 | private function getLastData() |
||
193 | |||
194 | private function getLastModifier() |
||
198 | |||
199 | private function isAnyKindOfLine() |
||
204 | |||
205 | /** |
||
206 | * @param $modifier |
||
207 | * @param array $params |
||
208 | */ |
||
209 | private function addRelativeModifier($modifier, array $params) |
||
283 | |||
284 | /** |
||
285 | * @param $modifier |
||
286 | * @param array $params |
||
287 | */ |
||
288 | private function addAbsoluteModifier($modifier, array $params) |
||
335 | } |
||
336 |
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.