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 Inner extends Rule |
||
21 | { |
||
22 | const NOT_AN_ARRAY = 'Inner::NOT_AN_ARRAY'; |
||
23 | |||
24 | /** |
||
25 | * The message templates which can be returned by this validator. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $messageTemplates = [ |
||
30 | self::NOT_AN_ARRAY => '{{ name }} must be an array', |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @var callable |
||
35 | */ |
||
36 | protected $callback; |
||
37 | |||
38 | /** |
||
39 | * @param callable $callback |
||
40 | */ |
||
41 | 1 | public function __construct(callable $callback) |
|
45 | |||
46 | /** |
||
47 | * Validates if $value is array, validate inner array of $value, and return result. |
||
48 | * |
||
49 | * @param mixed $value |
||
50 | * @return bool |
||
51 | */ |
||
52 | 1 | public function validate($value) |
|
71 | |||
72 | /** |
||
73 | * @param ValidationResult $result |
||
74 | */ |
||
75 | View Code Duplication | protected function handleError($result) |
|
85 | } |
||
86 |
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.