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 |
||
22 | View Code Duplication | class NumericMin extends AbstractRule |
|
|
|||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Contains the rule failure message |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $message = 'The field is not equal to or greater than the specified value.'; |
||
31 | |||
32 | /** |
||
33 | * @param mixed $value Value to validate |
||
34 | * @param string $field Unused by this rule |
||
35 | * @param array $allFields Unused by this rule |
||
36 | * |
||
37 | * @return bool |
||
38 | * |
||
39 | * @since 2.0 |
||
40 | */ |
||
41 | 15 | public function validate($value, $field = null, $allFields = null) |
|
52 | |||
53 | /** |
||
54 | * Sets the value to check against |
||
55 | * |
||
56 | * @param string $params If an array the first value will be used |
||
57 | * |
||
58 | * @return $this |
||
59 | * |
||
60 | * @since 2.0 |
||
61 | */ |
||
62 | 18 | public function setParameter($params) |
|
72 | |||
73 | /** |
||
74 | * Returns |
||
75 | * |
||
76 | * array( |
||
77 | * 'minValue' => <target value> |
||
78 | * ); |
||
79 | * |
||
80 | * @return string[] |
||
81 | */ |
||
82 | 1 | public function getMessageParameters() |
|
88 | |||
89 | } |
||
90 |
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.