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 Culture extends AbstractValidator |
|
|
|||
23 | { |
||
24 | const FILENAME = '../../resource/cultures.php'; |
||
25 | /** |
||
26 | * Invalid syntax |
||
27 | * |
||
28 | * @const INVALID |
||
29 | */ |
||
30 | const INVALID_CULTURE = 'invalidCulture'; |
||
31 | |||
32 | /** |
||
33 | * Message templates |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $messageTemplates = [ |
||
38 | self::INVALID_CULTURE => '', // added dynamically |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Returns true, if value is valid. False otherwise. |
||
43 | * |
||
44 | * @param mixed $value |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | 3 | public function isValid($value) |
|
70 | } |
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.