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 |
||
13 | class CreatePilotMonthBillCommandHandler |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var \DoliDB |
||
18 | */ |
||
19 | protected $db; |
||
20 | |||
21 | /** |
||
22 | * @var stdClass |
||
23 | */ |
||
24 | private $conf; |
||
25 | |||
26 | /** |
||
27 | * @var User |
||
28 | */ |
||
29 | protected $user; |
||
30 | |||
31 | /** |
||
32 | * @var Translate |
||
33 | */ |
||
34 | private $langs; |
||
35 | |||
36 | /** |
||
37 | * @param DoliDB $db |
||
38 | * @param stdClass $conf |
||
39 | * @param User $user |
||
40 | * @param Translate $langs |
||
41 | */ |
||
42 | View Code Duplication | public function __construct($db, $conf, $user, $langs) |
|
49 | |||
50 | /** |
||
51 | * @param CreatePilotMonthBillCommand $command |
||
52 | */ |
||
53 | View Code Duplication | public function handle($command) |
|
91 | |||
92 | /** |
||
93 | * @return Product |
||
94 | */ |
||
95 | View Code Duplication | private function getProduct() |
|
105 | |||
106 | /** |
||
107 | * @param int $receiverId |
||
108 | * |
||
109 | * @return Client |
||
110 | * @throws CustomerNotFoundException |
||
111 | */ |
||
112 | private function getCustomer($receiverId) |
||
133 | |||
134 | /** |
||
135 | * @param Facture $object |
||
136 | * @param Bbcvols $flight |
||
137 | * |
||
138 | * @throws ContactNotAddedException |
||
139 | */ |
||
140 | private function addContacts($object, $flight) |
||
146 | |||
147 | /** |
||
148 | * @param Facture $bill |
||
149 | * @param int $contactId |
||
150 | * @param string $contactType |
||
151 | * |
||
152 | * @throws ContactNotAddedException |
||
153 | */ |
||
154 | private function addContactOnBill(Facture $bill, $contactId, $contactType) |
||
160 | |||
161 | /** |
||
162 | * @param Facture $object |
||
163 | * @param Bbcvols $flight |
||
164 | */ |
||
165 | private function addLinks($object, $flight) |
||
169 | |||
170 | /** |
||
171 | * @param Facture $object |
||
172 | * @param int $id |
||
173 | */ |
||
174 | private function validates($object, $id) |
||
179 | |||
180 | /** |
||
181 | * @return int |
||
182 | */ |
||
183 | private function isReferenceHidden() |
||
187 | |||
188 | /** |
||
189 | * @return int |
||
190 | */ |
||
191 | private function isDescriptionHidden() |
||
195 | |||
196 | /** |
||
197 | * @return int |
||
198 | */ |
||
199 | private function isDetailHidden() |
||
203 | |||
204 | /** |
||
205 | * @param CreatePilotMonthBillCommand $command |
||
206 | * @param Facture $object |
||
207 | * @param int $id |
||
208 | */ |
||
209 | View Code Duplication | private function generateBillDocument(CreatePilotMonthBillCommand $command, $object, $id) |
|
220 | |||
221 | /** |
||
222 | * @param Product $flightProduct |
||
223 | * @param Bbcvols $flight |
||
224 | * |
||
225 | * @return float|int |
||
226 | */ |
||
227 | private function computeDiscounts($flightProduct, $flight) |
||
231 | |||
232 | /** |
||
233 | * @param Facture $facture |
||
234 | * @param Product $flightProduct |
||
235 | * @param Bbcvols $flight |
||
236 | */ |
||
237 | private function addOrderLine($facture, $flightProduct, $flight) |
||
288 | |||
289 | /** |
||
290 | * @param Bbcvols $flight |
||
291 | */ |
||
292 | private function flagFlightAsBilled($flight) |
||
297 | |||
298 | /** |
||
299 | * @return int |
||
300 | */ |
||
301 | private function getBankAccount() |
||
305 | |||
306 | /** |
||
307 | * @param int $year |
||
308 | * @param int $month |
||
309 | * |
||
310 | * @return int |
||
311 | */ |
||
312 | private function generateBillDate($year, $month) |
||
318 | } |
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.