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 ExtendedHeaderFlag |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * Tag is an update flag |
||
19 | */ |
||
20 | const FLAG_TAG_IS_AN_UPDATE = 0; |
||
21 | |||
22 | /** |
||
23 | * CRC data present flag |
||
24 | */ |
||
25 | const FLAG_CRC_DATA_PRESENT = 1; |
||
26 | |||
27 | /** |
||
28 | * Tag restrictions flag |
||
29 | */ |
||
30 | const FLAG_TAG_RESTRICTIONS = 2; |
||
31 | |||
32 | /** |
||
33 | * Valid values |
||
34 | * |
||
35 | * @var int[] |
||
36 | */ |
||
37 | protected static $values = [ |
||
38 | self::FLAG_TAG_IS_AN_UPDATE, |
||
39 | self::FLAG_CRC_DATA_PRESENT, |
||
40 | self::FLAG_TAG_RESTRICTIONS |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Return valid values |
||
45 | * |
||
46 | * @return int[] |
||
47 | */ |
||
48 | public static function values() |
||
52 | } |
||
53 |
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.