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 Bin 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 Bin, and based on these observations, apply Extract Interface, too.
1 | <?php namespace BinPacking3d\Entity; |
||
12 | class Bin implements EntityInterface |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var |
||
17 | */ |
||
18 | private $width; |
||
19 | |||
20 | /** |
||
21 | * @var |
||
22 | */ |
||
23 | private $height; |
||
24 | |||
25 | /** |
||
26 | * @var |
||
27 | */ |
||
28 | private $depth; |
||
29 | |||
30 | /** |
||
31 | * @var |
||
32 | */ |
||
33 | private $identifier; |
||
34 | |||
35 | /** |
||
36 | * @var |
||
37 | */ |
||
38 | private $internalIdentifier; |
||
39 | |||
40 | /** |
||
41 | * @var |
||
42 | */ |
||
43 | private $maxWeight; |
||
44 | |||
45 | /** |
||
46 | * @var |
||
47 | */ |
||
48 | private $outerWidth; |
||
49 | |||
50 | /** |
||
51 | * @var |
||
52 | */ |
||
53 | private $outerHeight; |
||
54 | |||
55 | /** |
||
56 | * @var |
||
57 | */ |
||
58 | private $outerDepth; |
||
59 | |||
60 | /** |
||
61 | * @var |
||
62 | */ |
||
63 | private $weight; |
||
64 | |||
65 | /** |
||
66 | * @var |
||
67 | */ |
||
68 | private $items; |
||
69 | |||
70 | /** |
||
71 | * @var |
||
72 | */ |
||
73 | private $usedSpace; |
||
74 | |||
75 | /** |
||
76 | * @var |
||
77 | */ |
||
78 | private $usedWeight; |
||
79 | |||
80 | /** |
||
81 | * @var |
||
82 | */ |
||
83 | private $image; |
||
84 | |||
85 | /** |
||
86 | * @return array |
||
87 | */ |
||
88 | public function render() |
||
98 | |||
99 | /** |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public function getWidth() |
||
106 | |||
107 | /** |
||
108 | * @param mixed $width |
||
109 | * @return Bin |
||
110 | */ |
||
111 | public function setWidth($width) |
||
117 | |||
118 | /** |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function getHeight() |
||
125 | |||
126 | /** |
||
127 | * @param mixed $height |
||
128 | * @return Bin |
||
129 | */ |
||
130 | public function setHeight($height) |
||
136 | |||
137 | /** |
||
138 | * @return mixed |
||
139 | */ |
||
140 | public function getDepth() |
||
144 | |||
145 | /** |
||
146 | * @param mixed $depth |
||
147 | * @return Bin |
||
148 | */ |
||
149 | public function setDepth($depth) |
||
155 | |||
156 | /** |
||
157 | * @return mixed |
||
158 | */ |
||
159 | public function getIdentifier() |
||
163 | |||
164 | /** |
||
165 | * @param mixed $identifier |
||
166 | * @return Bin |
||
167 | */ |
||
168 | public function setIdentifier($identifier) |
||
174 | |||
175 | /** |
||
176 | * @return mixed |
||
177 | */ |
||
178 | public function getMaxWeight() |
||
182 | |||
183 | /** |
||
184 | * @param mixed $maxWeight |
||
185 | * @return Bin |
||
186 | */ |
||
187 | public function setMaxWeight($maxWeight) |
||
193 | |||
194 | /** |
||
195 | * @return bool |
||
196 | * @throws \Exception |
||
197 | */ |
||
198 | View Code Duplication | final public function validate() |
|
211 | |||
212 | /** |
||
213 | * @return mixed |
||
214 | */ |
||
215 | public function getInternalIdentifier() |
||
219 | |||
220 | /** |
||
221 | * @param mixed $internalIdentifier |
||
222 | * @return Bin |
||
223 | */ |
||
224 | public function setInternalIdentifier($internalIdentifier) |
||
230 | |||
231 | /** |
||
232 | * @param $path |
||
233 | * @return bool|int |
||
234 | */ |
||
235 | public function saveImage($path) |
||
243 | |||
244 | /** |
||
245 | * @return mixed |
||
246 | */ |
||
247 | public function getImage() |
||
251 | |||
252 | /** |
||
253 | * @param mixed $image |
||
254 | * @return Bin |
||
255 | */ |
||
256 | public function setImage($image) |
||
262 | |||
263 | /** |
||
264 | * @return mixed |
||
265 | */ |
||
266 | public function getUsedSpace() |
||
270 | |||
271 | /** |
||
272 | * @param mixed $usedSpace |
||
273 | * @return Bin |
||
274 | */ |
||
275 | public function setUsedSpace($usedSpace) |
||
281 | |||
282 | /** |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function getUsedWeight() |
||
289 | |||
290 | /** |
||
291 | * @param mixed $usedWeight |
||
292 | * @return Bin |
||
293 | */ |
||
294 | public function setUsedWeight($usedWeight) |
||
300 | |||
301 | /** |
||
302 | * @param Item $item |
||
303 | */ |
||
304 | public function addItem(Item $item) |
||
308 | |||
309 | /** |
||
310 | * @return mixed |
||
311 | */ |
||
312 | public function getItems() |
||
316 | |||
317 | /** |
||
318 | * @return \Generator |
||
319 | */ |
||
320 | public function yieldItems() |
||
326 | |||
327 | /** |
||
328 | * @return mixed |
||
329 | */ |
||
330 | public function getOuterWidth() |
||
334 | |||
335 | /** |
||
336 | * @param mixed $outerWidth |
||
337 | * @return Bin |
||
338 | */ |
||
339 | public function setOuterWidth($outerWidth) |
||
351 | |||
352 | /** |
||
353 | * @return mixed |
||
354 | */ |
||
355 | public function getOuterHeight() |
||
359 | |||
360 | /** |
||
361 | * @param mixed $outerHeight |
||
362 | * @return Bin |
||
363 | */ |
||
364 | public function setOuterHeight($outerHeight) |
||
376 | |||
377 | /** |
||
378 | * @return mixed |
||
379 | */ |
||
380 | public function getOuterDepth() |
||
384 | |||
385 | /** |
||
386 | * @param mixed $outerDepth |
||
387 | * @return Bin |
||
388 | */ |
||
389 | public function setOuterDepth($outerDepth) |
||
401 | |||
402 | /** |
||
403 | * @return mixed |
||
404 | */ |
||
405 | public function getWeight() |
||
409 | |||
410 | /** |
||
411 | * @param mixed $weight |
||
412 | * @return Bin |
||
413 | */ |
||
414 | public function setWeight($weight) |
||
420 | |||
421 | } |
||
422 |
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.