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 | * Check $_GET |
||
5 | * |
||
6 | * @example if(chk_get("s") == "a") instead of if(isset($_GET["s"]) && $_GET["s"] == "a") |
||
7 | * |
||
8 | * @param string $key Get-key... |
||
9 | * @return bool |
||
10 | */ |
||
11 | function chk_get($key) { |
||
17 | |||
18 | /** |
||
19 | * Check $_POST |
||
20 | * |
||
21 | * @example if(chk_post("s") == "a") instead of if(isset($_POST["s"]) && $_POST["s"] == "a") |
||
22 | * |
||
23 | * @param string $key Post-key... |
||
24 | * @return bool |
||
25 | */ |
||
26 | function chk_post($key) { |
||
32 | |||
33 | /** |
||
34 | * Check multiple $_GET-keys |
||
35 | * |
||
36 | * @example if(chk_get_all(array("a","b"))) instead of if(!empty($_GET["a"]) && !empty($_GET["b"])) |
||
37 | */ |
||
38 | View Code Duplication | function chk_get_all($keys) { |
|
49 | |||
50 | /** |
||
51 | * Check multiple $_POST-keys |
||
52 | * |
||
53 | * @example if(chk_post_all(array("a","b"))) instead of if(!empty($_POST["a"]) && !empty($_POST["b"])) |
||
54 | */ |
||
55 | View Code Duplication | function chk_post_all($keys) { |
|
66 | |||
67 | /** |
||
68 | * Convert timestamp to HTTP-date (RFC2616) |
||
69 | * |
||
70 | * For use in "Last-Modified" headers. |
||
71 | */ |
||
72 | function timestamp_to_http_date($timestamp) { |
||
76 | |||
77 | /** |
||
78 | * Get client ip-address |
||
79 | * |
||
80 | * @return string User ip-address |
||
81 | */ |
||
82 | function get_client_ip_address() { |
||
99 | |||
100 | /** |
||
101 | * parse url, try to correct errors and return valid url + display-url. |
||
102 | * |
||
103 | * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
||
104 | * @example gopher:/ww.example.com => gopher://www.example.com |
||
105 | * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
||
106 | * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
||
107 | * @example .example.com/ => http://example.com/ |
||
108 | * @example example.com =>http://example.com |
||
109 | * @example subdomain.example.com => http://subdomain.example.com |
||
110 | * |
||
111 | * @param string $url Any somewhat valid url. |
||
112 | * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
||
113 | */ |
||
114 | function url_parser($url) { |
||
176 | } |
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.