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 |
||
| 2 | class holt45 { |
||
|
|
|||
| 3 | |||
| 4 | const DATA_URI_TRANSPARENT_GIF = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'; |
||
| 5 | const DATA_URI_TRANSPARENT_PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='; |
||
| 6 | |||
| 7 | |||
| 8 | /** |
||
| 9 | * Check $_GET |
||
| 10 | * |
||
| 11 | * @example if(chk_get("s") == "a") instead of if(isset($_GET["s"]) && $_GET["s"] == "a") |
||
| 12 | * |
||
| 13 | * @param string $key Get-key... |
||
| 14 | * @return bool |
||
| 15 | */ |
||
| 16 | public static function chk_get($key) { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Check $_POST |
||
| 25 | * |
||
| 26 | * @example if(chk_post("s") == "a") instead of if(isset($_POST["s"]) && $_POST["s"] == "a") |
||
| 27 | * |
||
| 28 | * @param string $key Post-key... |
||
| 29 | * @return bool |
||
| 30 | */ |
||
| 31 | public static function chk_post($key) { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Check multiple $_GET-keys |
||
| 40 | * |
||
| 41 | * @example if(chk_get_all(array("a","b"))) instead of if(!empty($_GET["a"]) && !empty($_GET["b"])) |
||
| 42 | * |
||
| 43 | * @param array $keys |
||
| 44 | * @return bool |
||
| 45 | */ |
||
| 46 | View Code Duplication | public static function chk_get_all($keys) { |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Check multiple $_POST-keys |
||
| 60 | * |
||
| 61 | * @example if(chk_post_all(array("a","b"))) instead of if(!empty($_POST["a"]) && !empty($_POST["b"])) |
||
| 62 | * |
||
| 63 | * @param array $keys |
||
| 64 | * @return bool |
||
| 65 | */ |
||
| 66 | View Code Duplication | public static function chk_post_all($keys) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Convert timestamp to HTTP-date (RFC2616) |
||
| 80 | * |
||
| 81 | * For use in "Last-Modified" headers. |
||
| 82 | * |
||
| 83 | * @param string $timestamp |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public static function timestamp_to_http_date($timestamp) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get client ip-address |
||
| 93 | * |
||
| 94 | * @return string User ip-address |
||
| 95 | */ |
||
| 96 | public static function get_client_ip_address() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * parse url, try to correct errors and return valid url + display-url. |
||
| 116 | * |
||
| 117 | * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
||
| 118 | * @example gopher:/ww.example.com => gopher://www.example.com |
||
| 119 | * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
||
| 120 | * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
||
| 121 | * @example .example.com/ => http://example.com/ |
||
| 122 | * @example example.com =>http://example.com |
||
| 123 | * @example subdomain.example.com => http://subdomain.example.com |
||
| 124 | * |
||
| 125 | * @param string $url Any somewhat valid url. |
||
| 126 | * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
||
| 127 | */ |
||
| 128 | public static function url_parser($url) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Generate a password-suggestion. |
||
| 193 | * |
||
| 194 | * @param int $length Length of password |
||
| 195 | * @param bool $simple Limit character-set to first 33 characters. |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public static function generate_password($length = 8, $simple = false) { |
||
| 220 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.