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 |
||
15 | class CreateOrderCommandHandler implements CommandHandlerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var DoliDB |
||
19 | */ |
||
20 | private $db; |
||
21 | |||
22 | /** |
||
23 | * @var stdClass |
||
24 | */ |
||
25 | private $conf; |
||
26 | |||
27 | /** |
||
28 | * @var User |
||
29 | */ |
||
30 | private $user; |
||
31 | |||
32 | /** |
||
33 | * @var Translate |
||
34 | */ |
||
35 | private $langs; |
||
36 | |||
37 | /** |
||
38 | * @var ModeleThirdPartyCode |
||
39 | */ |
||
40 | private $codeFournisseurGenerator; |
||
41 | |||
42 | /** |
||
43 | * @var ModeleThirdPartyCode |
||
44 | */ |
||
45 | private $codeClientGenerator; |
||
46 | |||
47 | /** |
||
48 | * @var Societe |
||
49 | */ |
||
50 | private $societe; |
||
51 | |||
52 | /** |
||
53 | * @var Commande |
||
54 | */ |
||
55 | private $order; |
||
56 | |||
57 | /** |
||
58 | * @param DoliDB $db |
||
59 | * @param stdClass $conf |
||
60 | * @param User $user |
||
61 | * @param Translate $langs |
||
62 | * @param ModeleThirdPartyCode $codeClientGenerator |
||
63 | * @param ModeleThirdPartyCode $codeFounrisseurGenerator |
||
64 | */ |
||
65 | public function __construct( |
||
80 | |||
81 | /** |
||
82 | * @param CreateOrderCommand|CommandInterface $command |
||
83 | * |
||
84 | * @throws Exception |
||
85 | */ |
||
86 | public function handle(CommandInterface $command) |
||
97 | |||
98 | /** |
||
99 | * @return Product |
||
100 | */ |
||
101 | View Code Duplication | private function getProduct() |
|
111 | |||
112 | /** |
||
113 | * @param Product $flightProduct |
||
114 | * @param float|int $pricePerPax |
||
115 | * |
||
116 | * @return float|int |
||
117 | */ |
||
118 | private function computeDiscounts($flightProduct, $pricePerPax = 0) |
||
122 | |||
123 | /** |
||
124 | * @param CommandInterface|CreateOrderCommand $command |
||
125 | * |
||
126 | * @return Societe |
||
127 | * @throws Exception |
||
128 | */ |
||
129 | private function createCustomer(CommandInterface $command) |
||
171 | |||
172 | /** |
||
173 | * @param CommandInterface|CreateOrderCommand $command |
||
174 | * |
||
175 | * @return CreateOrderCommandHandler |
||
176 | */ |
||
177 | private function addLine(CommandInterface $command) |
||
195 | |||
196 | /** |
||
197 | * @param CommandInterface|CreateOrderCommand $command |
||
198 | * @param int $customerId |
||
199 | * |
||
200 | * @return $this |
||
201 | * @throws Exception |
||
202 | */ |
||
203 | private function createOrder(CommandInterface $command, $customerId) |
||
221 | |||
222 | /** |
||
223 | * Validate the order |
||
224 | * |
||
225 | * @throws Exception |
||
226 | */ |
||
227 | private function validateOrder() |
||
235 | |||
236 | /** |
||
237 | * Add sales contact |
||
238 | */ |
||
239 | private function addContacts() |
||
244 | |||
245 | /** |
||
246 | * @return Societe |
||
247 | */ |
||
248 | public function getCustomer() |
||
252 | |||
253 | /** |
||
254 | * @return Commande |
||
255 | */ |
||
256 | public function getOrder() |
||
260 | |||
261 | } |
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.