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 |
||
3 | trait Misc { |
||
4 | |||
5 | /** |
||
6 | * Convert ISO 3166-1 alpha-2 code to (English) country name. |
||
7 | * |
||
8 | * @link https://gist.github.com/IngmarBoddington/5909709 Source |
||
9 | * |
||
10 | * @param string $key ISO 3166-1 alpha-2 code |
||
11 | * @return string Country name OR $key if no match. |
||
12 | */ |
||
13 | public static function iso3166ToName($key) { |
||
19 | |||
20 | /** |
||
21 | * To replace "Hallo [@var] world" with $value. |
||
22 | * |
||
23 | * @example replace_string($string, array("val1" => "foo", "val2" => "bar")) |
||
24 | * |
||
25 | * @param string $langString String containing placeholder. |
||
26 | * @param array $dynamicContent key->value array. |
||
27 | * @return string String with placeholder replaced. |
||
28 | */ |
||
29 | public static function replaceString($langString, $dynamicContent = array()) { |
||
36 | |||
37 | /** |
||
38 | * Get client ip-address |
||
39 | * |
||
40 | * @return string User ip-address |
||
41 | */ |
||
42 | public static function getClientIpAddress() { |
||
59 | |||
60 | /** |
||
61 | * Tries to auto-correct parse_url()-output. |
||
62 | * |
||
63 | * @param string $url |
||
64 | * @return string[]|false |
||
65 | */ |
||
|
|||
66 | |||
67 | private static function autoCorrectParseUrl($url) { |
||
83 | |||
84 | /** |
||
85 | * parse url, try to correct errors and return valid url + display-url. |
||
86 | * |
||
87 | * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
||
88 | * @example gopher:/ww.example.com => gopher://www.example.com |
||
89 | * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
||
90 | * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
||
91 | * @example .example.com/ => http://example.com/ |
||
92 | * @example example.com =>http://example.com |
||
93 | * @example subdomain.example.com => http://subdomain.example.com |
||
94 | * |
||
95 | * @param string $url Any somewhat valid url. |
||
96 | * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
||
97 | */ |
||
98 | public static function urlParser($url) { |
||
153 | |||
154 | /** |
||
155 | * Generate a password-suggestion. |
||
156 | * |
||
157 | * @param int $length Length of password |
||
158 | * @param string $passwordType "simple" limit character-set to first 33 characters. "long" uses 64 characters. |
||
159 | * @return string |
||
160 | */ |
||
161 | public static function generatePassword($length = 8, $passwordType = "long") { |
||
186 | |||
187 | /** |
||
188 | * Obfuscate string (url-safe and somewhat hard to guess). |
||
189 | * |
||
190 | * @param string $input The text that should be obfuscated |
||
191 | * @return string Obfuscated string |
||
192 | */ |
||
193 | public static function obfuscateString($input) { |
||
196 | |||
197 | /** |
||
198 | * Deobfuscate string |
||
199 | * |
||
200 | * @param string $input Obfuscated string |
||
201 | * @return string Deobfuscated string |
||
202 | */ |
||
203 | public static function deobfuscateString($input) { |
||
206 | |||
207 | /** |
||
208 | * Convert <textarea> to [textarea]. |
||
209 | * |
||
210 | * @param string $html |
||
211 | * @return string |
||
212 | */ |
||
213 | public static function textareaEncode($html) { |
||
216 | |||
217 | /** |
||
218 | * Convert [textarea] to <textarea>. |
||
219 | * |
||
220 | * @param string $html |
||
221 | * @return string |
||
222 | */ |
||
223 | public static function textareaDecode($html) { |
||
226 | |||
227 | /** |
||
228 | * Create range for pagination |
||
229 | * |
||
230 | * @param int $totalPages |
||
231 | * @param int $selectedPage |
||
232 | * @param int $numberOfResults |
||
233 | * @return array Array with all page-numbers limited by $number_of_results |
||
234 | */ |
||
235 | public static function generatePaginationRange($totalPages, $selectedPage = 1, $numberOfResults = 7) { |
||
269 | |||
270 | |||
271 | } |
||
272 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.