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 namespace BinPacking3d\Entity; |
||
11 | class Item implements EntityInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var |
||
16 | */ |
||
17 | private $width; |
||
18 | |||
19 | /** |
||
20 | * @var |
||
21 | */ |
||
22 | private $height; |
||
23 | |||
24 | /** |
||
25 | * @var |
||
26 | */ |
||
27 | private $depth; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $quantity = 1; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | private $verticalRotationLock = false; |
||
38 | |||
39 | /** |
||
40 | * @var |
||
41 | */ |
||
42 | private $itemIdentifier; |
||
43 | |||
44 | /** |
||
45 | * @var |
||
46 | */ |
||
47 | private $weight; |
||
48 | |||
49 | /** |
||
50 | * @var |
||
51 | */ |
||
52 | private $product; |
||
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | public function render() |
||
69 | |||
70 | /** |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function getWidth() |
||
77 | |||
78 | /** |
||
79 | * @param mixed $width |
||
80 | * @return Item |
||
81 | */ |
||
82 | public function setWidth($width) |
||
88 | |||
89 | /** |
||
90 | * @return mixed |
||
91 | */ |
||
92 | public function getHeight() |
||
96 | |||
97 | /** |
||
98 | * @param mixed $height |
||
99 | * @return Item |
||
100 | */ |
||
101 | public function setHeight($height) |
||
107 | |||
108 | /** |
||
109 | * @return mixed |
||
110 | */ |
||
111 | public function getDepth() |
||
115 | |||
116 | /** |
||
117 | * @param mixed $depth |
||
118 | * @return Item |
||
119 | */ |
||
120 | public function setDepth($depth) |
||
126 | |||
127 | /** |
||
128 | * @return int |
||
129 | */ |
||
130 | public function getQuantity() |
||
134 | |||
135 | /** |
||
136 | * @param int $quantity |
||
137 | * @return Item |
||
138 | */ |
||
139 | public function setQuantity($quantity) |
||
145 | |||
146 | /** |
||
147 | * @return boolean |
||
148 | */ |
||
149 | public function isVerticalRotationLock() |
||
153 | |||
154 | /** |
||
155 | * @param boolean $verticalRotationLock |
||
156 | * @return Item |
||
157 | */ |
||
158 | public function setVerticalRotationLock($verticalRotationLock) |
||
164 | |||
165 | /** |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function getItemIdentifier() |
||
172 | |||
173 | /** |
||
174 | * @param mixed $itemIdentifier |
||
175 | * @return Item |
||
176 | */ |
||
177 | public function setItemIdentifier($itemIdentifier) |
||
183 | |||
184 | /** |
||
185 | * @return mixed |
||
186 | */ |
||
187 | public function getWeight() |
||
191 | |||
192 | /** |
||
193 | * @param mixed $weight |
||
194 | * @return Item |
||
195 | */ |
||
196 | public function setWeight($weight) |
||
202 | |||
203 | /** |
||
204 | * @return bool |
||
205 | */ |
||
206 | View Code Duplication | final public function validate() |
|
217 | |||
218 | /** |
||
219 | * @return mixed |
||
220 | */ |
||
221 | public function getProduct() |
||
225 | |||
226 | /** |
||
227 | * @param mixed $product |
||
228 | * @return Item |
||
229 | */ |
||
230 | public function setProduct($product) |
||
236 | |||
237 | } |
||
238 |
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.