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 |
||
27 | class Bill implements BillInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var int|string |
||
31 | */ |
||
32 | protected $id; |
||
33 | |||
34 | /** |
||
35 | * @var TypeInterface |
||
36 | */ |
||
37 | protected $type; |
||
38 | |||
39 | /** |
||
40 | * @var DateTimeImmutable |
||
41 | */ |
||
42 | protected $time; |
||
43 | |||
44 | /** |
||
45 | * @var Money |
||
46 | */ |
||
47 | protected $sum; |
||
48 | |||
49 | /** |
||
50 | * @var QuantityInterface |
||
51 | */ |
||
52 | protected $quantity; |
||
53 | |||
54 | /** |
||
55 | * @var CustomerInterface |
||
56 | */ |
||
57 | protected $customer; |
||
58 | |||
59 | /** |
||
60 | * @var TargetInterface |
||
61 | */ |
||
62 | protected $target; |
||
63 | |||
64 | /** |
||
65 | * @var PlanInterface |
||
66 | */ |
||
67 | protected $plan; |
||
68 | |||
69 | /** |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $isFinished = false; |
||
73 | |||
74 | /** |
||
75 | * @var ChargeInterface[] |
||
76 | */ |
||
77 | protected $charges = []; |
||
78 | |||
79 | 1 | public function __construct( |
|
102 | |||
103 | 1 | public function getUniqueString() |
|
116 | |||
117 | public function calculatePrice() |
||
123 | |||
124 | /** |
||
125 | * @return int|string |
||
126 | */ |
||
127 | public function getId() |
||
131 | |||
132 | View Code Duplication | public function setId($id) |
|
142 | |||
143 | /** |
||
144 | * @return TypeInterface |
||
145 | */ |
||
146 | 1 | public function getType() |
|
150 | |||
151 | /** |
||
152 | * @return DateTimeImmutable |
||
153 | */ |
||
154 | 1 | public function getTime() |
|
158 | |||
159 | /** |
||
160 | * @return TargetInterface |
||
161 | */ |
||
162 | 1 | public function getTarget() |
|
166 | |||
167 | /** |
||
168 | * @return CustomerInterface |
||
169 | */ |
||
170 | 1 | public function getCustomer() |
|
174 | |||
175 | /** |
||
176 | * @return QuantityInterface |
||
177 | */ |
||
178 | 1 | public function getQuantity() |
|
182 | |||
183 | /** |
||
184 | * @return Money |
||
185 | */ |
||
186 | 1 | public function getSum() |
|
190 | |||
191 | /** |
||
192 | * @return PlanInterface |
||
193 | */ |
||
194 | 1 | public function getPlan() |
|
198 | |||
199 | /** |
||
200 | * @return ChargeInterface[] |
||
201 | */ |
||
202 | 1 | public function getCharges() |
|
206 | |||
207 | /** |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function getIsFinished() |
||
214 | |||
215 | public function jsonSerialize() |
||
219 | } |
||
220 |
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.