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 |
||
14 | class MaskBuilder extends AbstractMaskBuilder |
||
15 | { |
||
16 | const MASK_VIEW = 1; // 1 << 0 |
||
17 | const MASK_EDIT = 4; // 1 << 2 |
||
18 | const MASK_DELETE = 8; // 1 << 3 |
||
19 | const MASK_PUBLISH = 16; // 1 << 4 |
||
20 | const MASK_UNPUBLISH = 32; // 1 << 5 |
||
21 | const MASK_IDDQD = 1073741823; // 1 << 0 | 1 << 1 | ... | 1 << 30 |
||
22 | |||
23 | const CODE_VIEW = 'V'; |
||
24 | const CODE_EDIT = 'E'; |
||
25 | const CODE_DELETE = 'D'; |
||
26 | const CODE_PUBLISH = 'P'; |
||
27 | const CODE_UNPUBLISH = 'U'; |
||
28 | |||
29 | const ALL_OFF = '................................'; |
||
30 | const OFF = '.'; |
||
31 | const ON = '*'; |
||
32 | |||
33 | /** |
||
34 | * Returns a human-readable representation of the permission |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | 1 | public function getPattern() |
|
56 | |||
57 | /** |
||
58 | * Returns the code for the passed mask |
||
59 | * |
||
60 | * @param int|null $mask |
||
61 | * |
||
62 | * @throws InvalidArgumentException |
||
63 | * @throws \RuntimeException |
||
64 | * |
||
65 | * @return string |
||
66 | */ |
||
67 | 4 | public static function getCode($mask) |
|
90 | |||
91 | /** |
||
92 | * Checks if a specific permission or mask value is set in the current mask |
||
93 | * |
||
94 | * @param string|int $mask |
||
95 | * |
||
96 | * @throws InvalidArgumentException |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | 2 | public function has($mask) |
|
110 | |||
111 | /** |
||
112 | * Returns the mask for the passed code. |
||
113 | * |
||
114 | * @param mixed $code |
||
115 | * |
||
116 | * @return int |
||
117 | * |
||
118 | * @throws \InvalidArgumentException |
||
119 | */ |
||
120 | 15 | public function resolveMask($code) |
|
136 | } |
||
137 |
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.