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 |
||
20 | class Nested implements ValidatorInterface { |
||
21 | |||
22 | |||
23 | /** |
||
24 | * Holds the invalid values. |
||
25 | */ |
||
26 | protected $invalidDetails; |
||
27 | |||
28 | /** |
||
29 | * Checks whether the given values are of the expected nested data. |
||
30 | * |
||
31 | * @param mixed $value |
||
32 | * the potential nested values to check |
||
33 | * @param Validator $validator |
||
34 | * the validator to check with |
||
35 | * @param array $rules |
||
36 | * the rules which the nested data must fulfill |
||
37 | * |
||
38 | * @return boolean |
||
39 | * true if all the $values are a valid array, else false with the invalid details set |
||
40 | */ |
||
41 | protected function isValidNested($value, Validator $validator, array $rules) { |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | View Code Duplication | public function isValid($value, array $parameters) { |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function getInvalidDetails() { |
||
75 | } |
||
76 |
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.