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 |
||
10 | class CreateFlightBillCommandHandler |
||
11 | { |
||
12 | /** |
||
13 | * @var \DoliDB |
||
14 | */ |
||
15 | protected $db; |
||
16 | |||
17 | /** |
||
18 | * @var stdClass |
||
19 | */ |
||
20 | private $conf; |
||
21 | |||
22 | /** |
||
23 | * @var User |
||
24 | */ |
||
25 | protected $user; |
||
26 | |||
27 | /** |
||
28 | * @var Translate |
||
29 | */ |
||
30 | private $langs; |
||
31 | |||
32 | /** |
||
33 | * @param DoliDB $db |
||
34 | * @param stdClass $conf |
||
35 | * @param User $user |
||
36 | * @param Translate $langs |
||
37 | */ |
||
38 | View Code Duplication | public function __construct($db, $conf, $user, $langs) |
|
45 | |||
46 | /** |
||
47 | * @param CreateFlightBillCommand $command |
||
48 | */ |
||
49 | public function handle(CreateFlightBillCommand $command) |
||
89 | |||
90 | /** |
||
91 | * @return Product |
||
92 | */ |
||
93 | View Code Duplication | private function getProduct() |
|
103 | |||
104 | /** |
||
105 | * @param Bbcvols $flight |
||
106 | * |
||
107 | * @return Client |
||
108 | * |
||
109 | * @throws CustomerNotFoundException |
||
110 | */ |
||
111 | private function getCustomer(Bbcvols $flight) |
||
125 | |||
126 | /** |
||
127 | * @param Bbcvols $flight |
||
128 | * |
||
129 | * @return Client |
||
130 | */ |
||
131 | private function fetchCustomerFromFlight($flight) |
||
152 | |||
153 | /** |
||
154 | * @param int $flightId |
||
155 | * |
||
156 | * @return Bbcvols |
||
157 | * |
||
158 | * @throws FlightNotFoundException |
||
159 | */ |
||
160 | private function getFlight($flightId) |
||
170 | |||
171 | /** |
||
172 | * @param Facture $object |
||
173 | * @param Bbcvols $flight |
||
174 | * |
||
175 | * @throws ContactNotAddedException |
||
176 | */ |
||
177 | private function addContacts($object, $flight) |
||
183 | |||
184 | /** |
||
185 | * @param Facture $bill |
||
186 | * @param int $contactId |
||
187 | * @param string $contactType |
||
188 | * |
||
189 | * @throws ContactNotAddedException |
||
190 | */ |
||
191 | private function addContactOnBill(Facture $bill, $contactId, $contactType) |
||
197 | |||
198 | /** |
||
199 | * @param Facture $object |
||
200 | * @param Bbcvols $flight |
||
201 | */ |
||
202 | private function addLinks($object, $flight) |
||
206 | |||
207 | /** |
||
208 | * @param Facture $object |
||
209 | * @param int $id |
||
210 | */ |
||
211 | private function validates($object, $id) |
||
216 | |||
217 | /** |
||
218 | * @return int |
||
219 | */ |
||
220 | private function isReferenceHidden() |
||
224 | |||
225 | /** |
||
226 | * @return int |
||
227 | */ |
||
228 | private function isDescriptionHidden() |
||
232 | |||
233 | /** |
||
234 | * @return int |
||
235 | */ |
||
236 | private function isDetailHidden() |
||
240 | |||
241 | /** |
||
242 | * @param CreateFlightBillCommand $command |
||
243 | * @param Facture $object |
||
244 | * @param int $id |
||
245 | */ |
||
246 | private function generateBillDocument(CreateFlightBillCommand $command, $object, $id) |
||
257 | |||
258 | /** |
||
259 | * @param $flightProduct |
||
260 | * @param $flight |
||
261 | * |
||
262 | * @return float|int |
||
263 | */ |
||
264 | private function computeDiscounts($flightProduct, $flight) |
||
268 | |||
269 | /** |
||
270 | * @param Facture $object |
||
271 | * @param Product $flightProduct |
||
272 | * @param Bbcvols $flight |
||
273 | */ |
||
274 | private function addOrderLine($object, $flightProduct, $flight, $nbrPax) |
||
321 | |||
322 | /** |
||
323 | * @param Bbcvols $flight |
||
324 | */ |
||
325 | private function flagFlightAsBilled($flight) |
||
330 | } |
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.