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 |
||
18 | View Code Duplication | class AlphaNumeric extends AbstractValidator implements ValidatorInterface |
|
|
|||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @readwrite |
||
23 | * @var array Message templates |
||
24 | */ |
||
25 | protected $messageTemplate = |
||
26 | 'The value can only contain alpha numeric characters.'; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * Returns true if and only if $value meets the validation requirements |
||
31 | * |
||
32 | * The context specified can be used in the validation process so that |
||
33 | * the same value can be valid or invalid depending on that data. |
||
34 | * |
||
35 | * @param mixed $value |
||
36 | * @param array|mixed $context |
||
37 | * |
||
38 | * @return bool |
||
39 | */ |
||
40 | 2 | public function validates($value, $context = []) |
|
48 | } |
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.