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 | abstract class AbstractBillCommandHandler implements CommandHandlerInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var stdClass |
||
14 | */ |
||
15 | protected $conf; |
||
16 | /** |
||
17 | * @var Translate |
||
18 | */ |
||
19 | protected $langs; |
||
20 | /** |
||
21 | * @var User |
||
22 | */ |
||
23 | protected $user; |
||
24 | /** |
||
25 | * @var \DoliDB |
||
26 | */ |
||
27 | protected $db; |
||
28 | |||
29 | /** |
||
30 | * @param DoliDB $db |
||
31 | * @param stdClass $conf |
||
32 | * @param User $user |
||
33 | * @param Translate $langs |
||
34 | */ |
||
35 | View Code Duplication | public function __construct($db, $conf, $user, $langs) |
|
42 | |||
43 | /** |
||
44 | * @return Product |
||
45 | */ |
||
46 | View Code Duplication | protected function getProduct() |
|
56 | |||
57 | /** |
||
58 | * @param int $receiverId |
||
59 | * |
||
60 | * @return Client |
||
61 | * @throws CustomerNotFoundException |
||
62 | */ |
||
63 | protected function getCustomer($receiverId) |
||
84 | |||
85 | /** |
||
86 | * @param Facture $object |
||
87 | * @param Bbcvols $flight |
||
88 | */ |
||
89 | protected function addLinks($object, $flight) |
||
93 | |||
94 | /** |
||
95 | * @param Facture $object |
||
96 | * @param int $id |
||
97 | */ |
||
98 | protected function validates($object, $id) |
||
103 | |||
104 | /** |
||
105 | * @return int |
||
106 | */ |
||
107 | protected function isReferenceHidden() |
||
111 | |||
112 | /** |
||
113 | * @return int |
||
114 | */ |
||
115 | protected function isDescriptionHidden() |
||
119 | |||
120 | /** |
||
121 | * @return int |
||
122 | */ |
||
123 | protected function isDetailHidden() |
||
127 | |||
128 | /** |
||
129 | * @param CreateReceiverMonthBillCommand $command |
||
130 | * @param Facture $object |
||
131 | * @param int $id |
||
132 | */ |
||
133 | View Code Duplication | protected function generateBillDocument(CreateReceiverMonthBillCommand $command, $object, $id) |
|
134 | { |
||
135 | $object->fetch($id); |
||
136 | $object->generateDocument( |
||
137 | $command->getModelDocument(), |
||
138 | $this->langs, |
||
139 | $this->isDetailHidden(), |
||
140 | $this->isDescriptionHidden(), |
||
141 | $this->isReferenceHidden() |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param Product $flightProduct |
||
147 | * @param Bbcvols $flight |
||
148 | * |
||
149 | * @return float|int |
||
150 | */ |
||
151 | protected function computeDiscounts($flightProduct, $flight) |
||
155 | |||
156 | /** |
||
157 | * @param Facture $facture |
||
158 | * @param Product $flightProduct |
||
159 | * @param Bbcvols $flight |
||
160 | */ |
||
161 | protected function addOrderLine($facture, $flightProduct, $flight) |
||
212 | |||
213 | /** |
||
214 | * @param Bbcvols $flight |
||
215 | */ |
||
216 | protected function flagFlightAsBilled($flight) |
||
221 | } |
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.