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 |
||
19 | class DepositBilletValidator |
||
20 | { |
||
21 | /** @var DepositBillet */ |
||
22 | private $depositBillet; |
||
23 | |||
24 | /** |
||
25 | * @param DepositBillet $depositBillet |
||
26 | */ |
||
27 | public function __construct(DepositBillet $depositBillet) |
||
31 | |||
32 | /** |
||
33 | * Validate the attributes of the deposit billet class |
||
34 | * |
||
35 | * @return void |
||
36 | */ |
||
37 | public function validate(): void |
||
48 | |||
49 | /** |
||
50 | * This validates the alias |
||
51 | * |
||
52 | * @return void |
||
53 | * @throws InvalidArgumentException |
||
54 | */ |
||
55 | View Code Duplication | private function validateAlias() |
|
62 | |||
63 | /** |
||
64 | * This validates the document number |
||
65 | * |
||
66 | * @return void |
||
67 | * @throws InvalidArgumentException |
||
68 | */ |
||
69 | private function validateDocumentNumber() |
||
76 | |||
77 | /** |
||
78 | * This validates the amount |
||
79 | * |
||
80 | * @return void |
||
81 | * @throws InvalidArgumentException |
||
82 | */ |
||
83 | private function validateAmount() |
||
90 | |||
91 | /** |
||
92 | * This validates the due date |
||
93 | * |
||
94 | * @return void |
||
95 | * @throws InvalidArgumentException |
||
96 | */ |
||
97 | private function validateDueDate() |
||
110 | |||
111 | /** |
||
112 | * This validates the emission fee |
||
113 | * |
||
114 | * @return void |
||
115 | * @throws InvalidArgumentException |
||
116 | */ |
||
117 | private function validateEmissionFee() |
||
125 | |||
126 | /** |
||
127 | * This validates a type |
||
128 | * |
||
129 | * @return void |
||
130 | * @throws InvalidArgumentException |
||
131 | */ |
||
132 | private function validateType() |
||
144 | |||
145 | /** |
||
146 | * This validates a bank account |
||
147 | * |
||
148 | * @return void |
||
149 | * @throws InvalidArgumentException |
||
150 | */ |
||
151 | private function validateBankAccount() |
||
161 | |||
162 | /** |
||
163 | * This validates a bank account |
||
164 | * |
||
165 | * @return void |
||
166 | * @throws InvalidArgumentException |
||
167 | */ |
||
168 | private function validatePayer() |
||
178 | } |
||
179 |
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.