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 VirtualCardValidator |
||
19 | { |
||
20 | private $virtualCard; |
||
21 | |||
22 | /** |
||
23 | * VirtualCardValidator constructor. |
||
24 | * @param VirtualCard $virtualCard |
||
25 | */ |
||
26 | public function __construct(VirtualCard $virtualCard) |
||
30 | |||
31 | /** |
||
32 | * This validate the virtual card |
||
33 | */ |
||
34 | public function validate(): void |
||
45 | |||
46 | /** |
||
47 | * This validate a virtual card document |
||
48 | * |
||
49 | * @return void |
||
50 | * @throws InvalidArgumentException |
||
51 | */ |
||
52 | private function validateDocumentNumber() |
||
62 | |||
63 | /** |
||
64 | * This validates a card name |
||
65 | * |
||
66 | * @return void |
||
67 | * @throws InvalidArgumentException |
||
68 | */ |
||
69 | View Code Duplication | private function validateCardName() |
|
76 | |||
77 | /** |
||
78 | * This validate a virtual card alias |
||
79 | * |
||
80 | * @return void |
||
81 | * @throws InvalidArgumentException |
||
82 | */ |
||
83 | View Code Duplication | private function validateAlias() |
|
90 | |||
91 | /** |
||
92 | * This validate a virtual card bank agency |
||
93 | * |
||
94 | * @return void |
||
95 | * @throws InvalidArgumentException |
||
96 | */ |
||
97 | private function validateBankAgency() |
||
104 | |||
105 | /** |
||
106 | * This validate a virtual card bank account |
||
107 | * |
||
108 | * @return void |
||
109 | * @throws InvalidArgumentException |
||
110 | */ |
||
111 | private function validateBankAccount() |
||
118 | |||
119 | /** |
||
120 | * This validate a virtual card program ID |
||
121 | * |
||
122 | * @return void |
||
123 | * @throws InvalidArgumentException |
||
124 | */ |
||
125 | private function validateProgramId() |
||
132 | |||
133 | /** |
||
134 | * This validate a virtual card password |
||
135 | * |
||
136 | * @return void |
||
137 | * @throws InvalidArgumentException |
||
138 | */ |
||
139 | private function validatePassword() |
||
146 | |||
147 | /** |
||
148 | * This validate a virtual card address |
||
149 | * |
||
150 | * @return void |
||
151 | * @throws InvalidArgumentException |
||
152 | */ |
||
153 | private function validateAddress() |
||
158 | } |
||
159 |
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.