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 |
||
17 | class Utility |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * method to get the current http protocol |
||
22 | * |
||
23 | * @return string the current protocol |
||
24 | * @deprecated since 1.5 will be removed |
||
25 | */ |
||
26 | public static function getProtocol() |
||
30 | |||
31 | /** |
||
32 | * detect base url |
||
33 | * |
||
34 | * @return string |
||
35 | * @deprecated since 1.5 will be removed |
||
36 | */ |
||
37 | View Code Duplication | public static function getBaseUrl() |
|
48 | |||
49 | /** |
||
50 | * detect install path |
||
51 | * |
||
52 | * @return string |
||
53 | * @deprecated since 1.5 will be removed |
||
54 | */ |
||
55 | public static function getInstallPath() |
||
63 | |||
64 | /** |
||
65 | * resolve a file path by replace the mod: prefix |
||
66 | * |
||
67 | * @param $path |
||
68 | * |
||
69 | * @return string|null the full filepath or null if file does not exists |
||
70 | */ |
||
71 | public static function resolveFilePath($path) |
||
84 | |||
85 | /** |
||
86 | * load files e.g. config files |
||
87 | * |
||
88 | * @param $file |
||
89 | * |
||
90 | * @return mixed|null |
||
91 | */ |
||
92 | 32 | public static function load($file) |
|
100 | |||
101 | /** |
||
102 | * check if a plugin is loaded |
||
103 | * |
||
104 | * @param $plugin |
||
105 | * @return bool |
||
106 | * @deprecated since 1.5 will be removed |
||
107 | * @use 'plugins_loaded' event |
||
108 | */ |
||
109 | public static function isPluginLoaded($plugin) |
||
118 | |||
119 | /** |
||
120 | * static method to get files by directory and file filter |
||
121 | * |
||
122 | * @param $directory |
||
123 | * @param string $filter |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 7 | public static function getFiles($directory, $filter = '\Phile\FilterIterator\GeneralFileFilterIterator') |
|
147 | |||
148 | /** |
||
149 | * redirect to an url |
||
150 | * |
||
151 | * @param $url the url to redirect to |
||
152 | * @param int $statusCode the http status code |
||
153 | * @deprecated since 1.5 will be removed |
||
154 | */ |
||
155 | public static function redirect($url, $statusCode = 302) |
||
159 | |||
160 | /** |
||
161 | * generate secure md5 hash |
||
162 | * |
||
163 | * @param $value |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public static function getSecureMD5Hash($value) |
||
173 | |||
174 | /** |
||
175 | * method to generate a secure token |
||
176 | * code from http://stackoverflow.com/a/13733588/1372085 |
||
177 | * modified by Frank Nägler |
||
178 | * |
||
179 | * @param int $length |
||
180 | * @param bool $widthSpecialChars |
||
181 | * @param null $additionalChars |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | 1 | public static function generateSecureToken($length = 32, $widthSpecialChars = true, $additionalChars = null) |
|
203 | |||
204 | /** |
||
205 | * method to get a more secure random value |
||
206 | * code from http://stackoverflow.com/a/13733588/1372085 |
||
207 | * |
||
208 | * @param $min |
||
209 | * @param $max |
||
210 | * |
||
211 | * @return mixed |
||
212 | */ |
||
213 | // @codingStandardsIgnoreStart |
||
214 | 1 | public static function crypto_rand_secure($min, $max) |
|
232 | } |
||
233 |
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.