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 $lang_string String containing placeholder. |
||
| 26 | * @return string String with placeholder replaced. |
||
| 27 | */ |
||
| 28 | public static function replaceString($lang_string, $dynamic_content = array()) { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Get client ip-address |
||
| 38 | * |
||
| 39 | * @return string User ip-address |
||
| 40 | */ |
||
| 41 | public static function getClientIpAddress() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Tries to auto-correct parse_url()-output. |
||
| 61 | * |
||
| 62 | * @param string $url |
||
| 63 | * @return array |
||
| 64 | */ |
||
|
|
|||
| 65 | |||
| 66 | private static function autoCorrectParseUrl($url) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * parse url, try to correct errors and return valid url + display-url. |
||
| 85 | * |
||
| 86 | * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
||
| 87 | * @example gopher:/ww.example.com => gopher://www.example.com |
||
| 88 | * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
||
| 89 | * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
||
| 90 | * @example .example.com/ => http://example.com/ |
||
| 91 | * @example example.com =>http://example.com |
||
| 92 | * @example subdomain.example.com => http://subdomain.example.com |
||
| 93 | * |
||
| 94 | * @param string $url Any somewhat valid url. |
||
| 95 | * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
||
| 96 | */ |
||
| 97 | public static function urlParser($url) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Generate a password-suggestion. |
||
| 150 | * |
||
| 151 | * @param int $length Length of password |
||
| 152 | * @param bool $simple Limit character-set to first 33 characters. |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public static function generatePassword($length = 8, $simple = false) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Obfuscate string (url-safe and somewhat hard to guess). |
||
| 182 | * |
||
| 183 | * @param string $input The text that should be obfuscated |
||
| 184 | * @return string Obfuscated string |
||
| 185 | */ |
||
| 186 | function obfuscateString($input) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Deobfuscate string |
||
| 192 | * |
||
| 193 | * @param string $input Obfuscated string |
||
| 194 | * @return string Deobfuscated string |
||
| 195 | */ |
||
| 196 | function deobfuscateString($input) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Convert <textarea> to [textarea]. |
||
| 202 | * |
||
| 203 | * @param string $html |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public static function textareaEncode($html) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Convert [textarea] to <textarea>. |
||
| 212 | * |
||
| 213 | * @param string $html |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public static function textareaDecode($html) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Create range for pagination |
||
| 222 | * |
||
| 223 | * @param int $total_pages |
||
| 224 | * @param int $selected_page |
||
| 225 | * @param int $number_of_results |
||
| 226 | * @return array Array with all page-numbers limited by $number_of_results |
||
| 227 | */ |
||
| 228 | public static function generatePaginationRange($total_pages,$selected_page = 1,$number_of_results = 7) { |
||
| 258 | |||
| 259 | |||
| 260 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.