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 |
||
11 | class Time implements ValidateRuleInterface |
||
12 | { |
||
13 | use TimeTypeTrait; |
||
14 | |||
15 | /** |
||
16 | * Indicates if the given field's value is a MySql safe Time. |
||
17 | * |
||
18 | * @param object $subject |
||
19 | * @param string $field |
||
20 | * |
||
21 | * @return bool |
||
22 | */ |
||
23 | 36 | public function __invoke($subject, string $field): bool |
|
33 | |||
34 | /** |
||
35 | * Validates that the string can represent seconds. |
||
36 | * |
||
37 | * @param string $seconds |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | 27 | View Code Duplication | protected function validateSeconds(string $seconds): bool |
53 | |||
54 | /** |
||
55 | * Validates that the string can represent minutes. |
||
56 | * |
||
57 | * @param string $minutes |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | 27 | protected function validateMinutes(string $minutes): bool |
|
67 | |||
68 | /** |
||
69 | * Validates tha the string can represent hours. |
||
70 | * |
||
71 | * @param string $hours |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 33 | protected function validateHours(string $hours): bool |
|
81 | } |
||
82 |
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.