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 |
||
18 | class PixCashoutManualValidator |
||
19 | { |
||
20 | /** @var PixCashoutManual */ |
||
21 | private $pixCashoutManual; |
||
22 | |||
23 | /** |
||
24 | * @param PixCashoutManual $pixCashoutManual |
||
25 | */ |
||
26 | public function __construct(PixCashoutManual $pixCashoutManual) |
||
30 | |||
31 | /** |
||
32 | * Validate the attributes of the PIX cashout class |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | public function validate(): void |
||
44 | |||
45 | /** |
||
46 | * This validates the amount |
||
47 | * |
||
48 | * @return void |
||
49 | * @throws InvalidArgumentException |
||
50 | */ |
||
51 | private function validateAmount() |
||
58 | |||
59 | /** |
||
60 | * This validates a description |
||
61 | * |
||
62 | * @return void |
||
63 | * @throws InvalidArgumentException |
||
64 | */ |
||
65 | private function validateDescription() |
||
72 | |||
73 | /** |
||
74 | * This validates a sender bank account |
||
75 | * |
||
76 | * @return void |
||
77 | * @throws InvalidArgumentException |
||
78 | */ |
||
79 | private function validateSender() |
||
89 | |||
90 | /** |
||
91 | * This validates a recipient bank account |
||
92 | * |
||
93 | * @return void |
||
94 | * @throws InvalidArgumentException |
||
95 | */ |
||
96 | private function validateRecipient() |
||
106 | |||
107 | /** |
||
108 | * This validates a initialization type |
||
109 | * |
||
110 | * @return void |
||
111 | * @throws InvalidArgumentException |
||
112 | */ |
||
113 | View Code Duplication | private function validateInitializationType() |
|
124 | } |
||
125 |
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.