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:
Complex classes like holt45 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use holt45, and based on these observations, apply Extract Interface, too.
| 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 | * Assign value from $_GET |
||
| 39 | * |
||
| 40 | * @example $var = assign_from_get("a") instead of $var = ((!empty($_GET["s"])) ? $_GET["s"] : ""); |
||
| 41 | */ |
||
| 42 | public static function assign_from_get($key) { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Assign value from $_POST |
||
| 48 | * |
||
| 49 | * @example $var = assign_from_post("a") instead of $var = ((!empty($_POST["s"])) ? $_POST["s"] : ""); |
||
| 50 | */ |
||
| 51 | public static function assign_from_post($key) { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Check multiple $_GET-keys |
||
| 57 | * |
||
| 58 | * @example if(chk_get_all(array("a","b"))) instead of if(!empty($_GET["a"]) && !empty($_GET["b"])) |
||
| 59 | * |
||
| 60 | * @param array $keys |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | View Code Duplication | public static function chk_get_all($keys) { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Check multiple $_POST-keys |
||
| 77 | * |
||
| 78 | * @example if(chk_post_all(array("a","b"))) instead of if(!empty($_POST["a"]) && !empty($_POST["b"])) |
||
| 79 | * |
||
| 80 | * @param array $keys |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | View Code Duplication | public static function chk_post_all($keys) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Handle sessions - set session |
||
| 97 | * |
||
| 98 | * @param string $name Session name |
||
| 99 | * @param string $content Session content |
||
| 100 | * @param int $expires How many seconds before the session should expire. |
||
| 101 | */ |
||
| 102 | function session_set($name, $content, $expires = 86400) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Handle sessions - check if session is set and not expired. |
||
| 108 | * |
||
| 109 | * @param string $name Session name |
||
| 110 | * @return bool Session status |
||
| 111 | */ |
||
| 112 | function session_isset($name) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Handle sessions - Get session content |
||
| 129 | * |
||
| 130 | * @param string $name Session name |
||
| 131 | * @return string Session content |
||
| 132 | */ |
||
| 133 | function session_read($name) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Handle sessions - Delete session |
||
| 139 | * |
||
| 140 | * @param string $name Session name |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | function session_delete($name) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Convert timestamp to HTTP-date (RFC2616) |
||
| 149 | * |
||
| 150 | * For use in "Last-Modified" headers. |
||
| 151 | * |
||
| 152 | * @param string $timestamp |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public static function timestamp_to_http_date($timestamp) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get client ip-address |
||
| 162 | * |
||
| 163 | * @return string User ip-address |
||
| 164 | */ |
||
| 165 | public static function get_client_ip_address() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * parse url, try to correct errors and return valid url + display-url. |
||
| 185 | * |
||
| 186 | * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
||
| 187 | * @example gopher:/ww.example.com => gopher://www.example.com |
||
| 188 | * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
||
| 189 | * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
||
| 190 | * @example .example.com/ => http://example.com/ |
||
| 191 | * @example example.com =>http://example.com |
||
| 192 | * @example subdomain.example.com => http://subdomain.example.com |
||
| 193 | * |
||
| 194 | * @param string $url Any somewhat valid url. |
||
| 195 | * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
||
| 196 | */ |
||
| 197 | public static function url_parser($url) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Generate a password-suggestion. |
||
| 262 | * |
||
| 263 | * @param int $length Length of password |
||
| 264 | * @param bool $simple Limit character-set to first 33 characters. |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public static function generate_password($length = 8, $simple = false) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Convert <textarea> to [textarea]. |
||
| 292 | * |
||
| 293 | * @param string $html |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public static function textarea_encode($html) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Convert [textarea] to <textarea>. |
||
| 302 | * |
||
| 303 | * @param string $html |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public static function textarea_decode($html) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Convert timestamp to "x unit" |
||
| 312 | * |
||
| 313 | * @param string $timestamp |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public static function time_elapsed($timestamp) { |
||
| 341 | } |
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.