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 |
||
25 | View Code Duplication | class Page extends AbstractValidator |
|
|
|||
26 | { |
||
27 | /** |
||
28 | * Minimum page number |
||
29 | * |
||
30 | * @const MIN |
||
31 | */ |
||
32 | const MIN = 1; |
||
33 | /** |
||
34 | * Invalid type |
||
35 | * |
||
36 | * @const INVALID_TYPE |
||
37 | */ |
||
38 | const INVALID_TYPE = 'invalidType'; |
||
39 | /** |
||
40 | * Invalid page |
||
41 | * |
||
42 | * @const INVALID_INTEGER |
||
43 | */ |
||
44 | const INVALID_INTEGER = 'invalidInteger'; |
||
45 | |||
46 | /** |
||
47 | * Message templates |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $messageTemplates = [ |
||
52 | self::INVALID_TYPE => "'%value%' must be an integer", |
||
53 | self::INVALID_INTEGER => "'%value%' contains an invalid page number", |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Returns true, if value is valid. False otherwise. |
||
58 | * |
||
59 | * @param mixed $value |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function isValid($value) |
||
88 | } |
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.