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 Collection implements ValidatorInterface { |
||
21 | |||
22 | |||
23 | /** |
||
24 | * Holds the invalid array values. |
||
25 | */ |
||
26 | protected $invalidDetails; |
||
27 | |||
28 | /** |
||
29 | * Checks whether the given parameters fulfil: |
||
30 | * - At two given |
||
31 | * - The first one is a Validator or a subclass of it |
||
32 | * |
||
33 | * @param array $parameters |
||
34 | * the validation parameters |
||
35 | * |
||
36 | * @throws ValidationException |
||
37 | * thrown if the amount of parameters is not equal two or the first parameter is not a Validator |
||
38 | */ |
||
39 | View Code Duplication | protected function checkParameters(array $parameters) { |
|
47 | |||
48 | /** |
||
49 | * Checks whether the given values are of the expected array. |
||
50 | * |
||
51 | * @param mixed $values |
||
52 | * the potential array values to check |
||
53 | * @param Validator $validator |
||
54 | * the validator to check with |
||
55 | * @param array $rule |
||
56 | * the rule which each element must fulfill |
||
57 | * |
||
58 | * @return boolean |
||
59 | * true if all the $values are a valid array, else false with the invalid details set |
||
60 | */ |
||
61 | protected function isValidCollection($values, Validator $validator, array $rule) { |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function isValid($value, array $parameters) { |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function getInvalidDetails() { |
||
92 | } |
||
93 |
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.