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 EcuadorIdentification |
||
20 | { |
||
21 | /** |
||
22 | * Error encapsulator variable |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | private $error; |
||
27 | |||
28 | /** |
||
29 | * Set Error |
||
30 | * |
||
31 | * @param string|null $error |
||
32 | */ |
||
33 | protected function setError($error): void |
||
37 | |||
38 | /** |
||
39 | * Get Error |
||
40 | * |
||
41 | * @return string|null |
||
42 | */ |
||
43 | public function getError() |
||
47 | |||
48 | /** |
||
49 | * Validates the Ecuadorian Final Consumer |
||
50 | * |
||
51 | * @param string $identification_number Final Consumer Identification |
||
52 | * @return string|null |
||
53 | */ |
||
54 | View Code Duplication | public function validateFinalConsumer(string $identification_number) |
|
66 | |||
67 | /** |
||
68 | * Validates the Ecuadorian Identification Card |
||
69 | * |
||
70 | * @param string $identification_number Number of Identification Card |
||
71 | * @return string|null |
||
72 | */ |
||
73 | View Code Duplication | public function validatePersonalIdentification(string $identification_number) |
|
85 | |||
86 | /** |
||
87 | * Validates the Ecuadorian RUC of Natural Person |
||
88 | * |
||
89 | * @param string $identification_number Number of RUC Natural Person |
||
90 | * @return string|null |
||
91 | */ |
||
92 | View Code Duplication | public function validateNaturalRuc(string $identification_number) |
|
104 | |||
105 | /** |
||
106 | * Validates the Ecuadorian RUC of Public Companies |
||
107 | * |
||
108 | * @param string $identification_number Number of RUC Public Companies |
||
109 | * @return string|null |
||
110 | */ |
||
111 | View Code Duplication | public function validatePublicRuc(string $identification_number) |
|
123 | |||
124 | /** |
||
125 | * Validates the Ecuadorian RUC of Private Companies |
||
126 | * |
||
127 | * @param string $identification_number Number of RUC Private Companies |
||
128 | * @return string|null |
||
129 | */ |
||
130 | View Code Duplication | public function validatePrivateRuc(string $identification_number) |
|
142 | |||
143 | /** |
||
144 | * Validates the Ecuadorian Ruc's |
||
145 | * |
||
146 | * @param string $identification_number Number of RUC |
||
147 | * @return string|null |
||
148 | */ |
||
149 | public function validateRuc(string $identification_number) |
||
161 | |||
162 | /** |
||
163 | * Validate that the number belongs to natural persons. |
||
164 | * |
||
165 | * @param string $identification_number Number of identification |
||
166 | * @return string|null |
||
167 | */ |
||
168 | public function validateIsNaturalPersons(string $identification_number) |
||
172 | |||
173 | /** |
||
174 | * Validate that the number belongs to juridical persons. |
||
175 | * |
||
176 | * @param string $identification_number Number of identification |
||
177 | * @return string|null |
||
178 | */ |
||
179 | public function validateIsJuridicalPersons(string $identification_number) |
||
183 | |||
184 | /** |
||
185 | * Validate the number with all types of documents. |
||
186 | * |
||
187 | * @param string $identification_number Number of identification |
||
188 | * @return string|null |
||
189 | */ |
||
190 | public function validateAllTypeIdentification(string $identification_number) |
||
202 | } |
||
203 |
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.