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 |
||
12 | class StubHelper |
||
13 | { |
||
14 | const PREFIXES = [ |
||
15 | 'static' => '::', |
||
16 | 'property' => '\\$' |
||
17 | ]; |
||
18 | |||
19 | /** |
||
20 | * http://php.net/manual/en/language.variables.basics.php |
||
21 | * http://php.net/manual/en/functions.user-defined.php |
||
22 | */ |
||
23 | const REGEX_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; |
||
24 | |||
25 | /** |
||
26 | * @param string $name |
||
27 | * @return bool |
||
28 | * |
||
29 | * @throws InvalidArgumentException |
||
30 | */ |
||
31 | 2 | View Code Duplication | public static function isStaticName(string $name): bool |
43 | |||
44 | /** |
||
45 | * @param string $name |
||
46 | * @return void |
||
47 | * |
||
48 | * @throws InvalidArgumentException |
||
49 | */ |
||
50 | 2 | public static function validateStaticName(string $name) |
|
54 | |||
55 | /** |
||
56 | * @param string $name |
||
57 | * @return bool |
||
58 | * |
||
59 | * @throws InvalidArgumentException |
||
60 | */ |
||
61 | 118 | View Code Duplication | public static function isPropertyName(string $name): bool |
73 | |||
74 | /** |
||
75 | * @param string $name |
||
76 | * @return void |
||
77 | * |
||
78 | * @throws InvalidArgumentException |
||
79 | */ |
||
80 | 114 | public static function validatePropertyName(string $name) |
|
84 | |||
85 | /** |
||
86 | * @param string $name |
||
87 | * @return bool |
||
88 | * |
||
89 | * @throws InvalidArgumentException |
||
90 | */ |
||
91 | 4 | public static function isMethodName(string $name): bool |
|
95 | |||
96 | /** |
||
97 | * @param string $name |
||
98 | * @return void |
||
99 | * |
||
100 | * @throws InvalidArgumentException |
||
101 | */ |
||
102 | 114 | public static function validateMethodName(string $name) |
|
106 | |||
107 | /** |
||
108 | * @param string $name |
||
109 | * @return string |
||
110 | * |
||
111 | * @throws InvalidArgumentException |
||
112 | */ |
||
113 | 121 | public static function stripName(string $name): string |
|
119 | |||
120 | /** |
||
121 | * @param string $name |
||
122 | * @param array|null $prefixes |
||
123 | * @return string |
||
124 | */ |
||
125 | 131 | private static function doStripName(string $name, array $prefixes = null): string |
|
139 | |||
140 | /** |
||
141 | * @param string $name |
||
142 | * @param string|null $type |
||
143 | * @return void |
||
144 | * |
||
145 | * @throws InvalidArgumentException |
||
146 | */ |
||
147 | 131 | private static function validateName(string $name, string $type = null) |
|
169 | } |
||
170 |
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.