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 |
||
17 | class BillPaymentValidator |
||
18 | { |
||
19 | private $billPayment; |
||
20 | |||
21 | /** |
||
22 | * BillPaymentValidator constructor. |
||
23 | * @param BillPayment $billPayment |
||
24 | */ |
||
25 | public function __construct(BillPayment $billPayment) |
||
29 | |||
30 | /** |
||
31 | * This validate the bank account |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function validate(): void |
||
42 | |||
43 | /** |
||
44 | * This validate a bank branch |
||
45 | * |
||
46 | * @return void |
||
47 | * @throws InvalidArgumentException |
||
48 | */ |
||
49 | View Code Duplication | private function validateBankBranch() |
|
56 | |||
57 | /** |
||
58 | * This validate a bank account |
||
59 | * |
||
60 | * @return void |
||
61 | * @throws InvalidArgumentException |
||
62 | */ |
||
63 | View Code Duplication | private function validateBankAccount() |
|
70 | |||
71 | /** |
||
72 | * This validates a amount |
||
73 | * |
||
74 | * @return void |
||
75 | * @throws InvalidArgumentException |
||
76 | */ |
||
77 | private function validateAmount() |
||
84 | |||
85 | /** |
||
86 | * This validates a given ID |
||
87 | * |
||
88 | * @return void |
||
89 | * @throws InvalidArgumentException |
||
90 | */ |
||
91 | View Code Duplication | private function validateId() |
|
98 | } |
||
99 |
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.