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 |
||
23 | View Code Duplication | class MaxLength extends AbstractRule |
|
|
|||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Default failure message |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $message = 'The field is longer than the allowed maximum length.'; |
||
32 | |||
33 | /** |
||
34 | * @param mixed $value |
||
35 | * @param string $field |
||
36 | * @param array $allFields |
||
37 | * |
||
38 | * @return bool |
||
39 | * |
||
40 | * @since 2.0 |
||
41 | */ |
||
42 | 22 | public function validate($value, $field = null, $allFields = null) |
|
53 | |||
54 | /** |
||
55 | * Sets the value to check against |
||
56 | * |
||
57 | * @param string $params If an array the first value will be used |
||
58 | * |
||
59 | * @return $this |
||
60 | * |
||
61 | * @since 2.0 |
||
62 | */ |
||
63 | 25 | public function setParameter($params) |
|
73 | |||
74 | /** |
||
75 | * Returns |
||
76 | * |
||
77 | * array( |
||
78 | * 'maxLength' => <target length> |
||
79 | * ); |
||
80 | * |
||
81 | * @return string[] |
||
82 | */ |
||
83 | 2 | public function getMessageParameters() |
|
89 | |||
90 | } |
||
91 |
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.