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 |
||
15 | View Code Duplication | class Restriction |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * Tag size restriction. |
||
19 | */ |
||
20 | const RESTRICTION_TAG_SIZE = 0; |
||
21 | |||
22 | /** |
||
23 | * Text encoding restriction. |
||
24 | */ |
||
25 | const RESTRICTION_TEXT_ENCODING = 1; |
||
26 | |||
27 | /** |
||
28 | * Text fields size restriction. |
||
29 | */ |
||
30 | const RESTRICTION_TEXT_FIELDS_SIZE = 2; |
||
31 | |||
32 | /** |
||
33 | * Image encoding restriction. |
||
34 | */ |
||
35 | const RESTRICTION_IMAGE_ENCODING = 3; |
||
36 | |||
37 | /** |
||
38 | * Image size restriction. |
||
39 | */ |
||
40 | const RESTRICTION_IMAGE_SIZE = 4; |
||
41 | |||
42 | /** |
||
43 | * Valid values. |
||
44 | * |
||
45 | * @var int[] |
||
46 | */ |
||
47 | protected static $values = [ |
||
48 | self::RESTRICTION_TAG_SIZE, |
||
49 | self::RESTRICTION_TEXT_ENCODING, |
||
50 | self::RESTRICTION_TEXT_FIELDS_SIZE, |
||
51 | self::RESTRICTION_IMAGE_ENCODING, |
||
52 | self::RESTRICTION_IMAGE_SIZE |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * Return valid values. |
||
57 | * |
||
58 | * @return int[] |
||
59 | */ |
||
60 | public static function values() |
||
64 | } |
||
65 |
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.