| Conditions | 11 |
| Paths | 384 |
| Total Lines | 62 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 128 | public static function url_parser($url) { |
||
| 129 | |||
| 130 | // multiple /// messes up parse_url, replace 3 or more with 2 |
||
| 131 | $url = preg_replace('/(\/{2,})/','//',$url); |
||
| 132 | |||
| 133 | $parse_url = parse_url($url); |
||
| 134 | |||
| 135 | if(empty($parse_url["scheme"])) { |
||
| 136 | $parse_url["scheme"] = "http"; |
||
| 137 | } |
||
| 138 | if(empty($parse_url["host"]) && !empty($parse_url["path"])) { |
||
| 139 | // Strip slash from the beginning of path |
||
| 140 | $parse_url["host"] = ltrim($parse_url["path"], '\/'); |
||
| 141 | $parse_url["path"] = ""; |
||
| 142 | } |
||
| 143 | |||
| 144 | $url_array = array("url" => "", "url_display" => ""); |
||
| 145 | |||
| 146 | // Check if scheme is correct |
||
| 147 | if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) { |
||
| 148 | $url_array["url"] .= 'http'.'://'; |
||
| 149 | } else { |
||
| 150 | $url_array["url"] .= $parse_url["scheme"].'://'; |
||
| 151 | } |
||
| 152 | |||
| 153 | // Check if the right amount of "www" is set. |
||
| 154 | $explode_host = explode(".", $parse_url["host"]); |
||
| 155 | |||
| 156 | // Remove empty entries |
||
| 157 | $explode_host = array_filter($explode_host); |
||
| 158 | // And reassign indexes |
||
| 159 | $explode_host = array_values($explode_host); |
||
| 160 | |||
| 161 | // Contains subdomain |
||
| 162 | if(count($explode_host) > 2) { |
||
| 163 | // Check if subdomain only contains the letter w(then not any other subdomain). |
||
| 164 | if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) { |
||
| 165 | // Replace with "www" to avoid "ww" or "wwww", etc. |
||
| 166 | $explode_host[0] = "www"; |
||
| 167 | |||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | $url_array["url"] .= implode(".",$explode_host); |
||
| 172 | $url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash |
||
| 173 | |||
| 174 | if(!empty($parse_url["port"])) { |
||
| 175 | $url_array["url"] .= ":".$parse_url["port"]; |
||
| 176 | } |
||
| 177 | if(!empty($parse_url["path"])) { |
||
| 178 | $url_array["url"] .= $parse_url["path"]; |
||
| 179 | } |
||
| 180 | if(!empty($parse_url["query"])) { |
||
| 181 | $url_array["url"] .= '?'.$parse_url["query"]; |
||
| 182 | } |
||
| 183 | if(!empty($parse_url["fragment"])) { |
||
| 184 | $url_array["url"] .= '#'.$parse_url["fragment"]; |
||
| 185 | } |
||
| 186 | |||
| 187 | |||
| 188 | return $url_array; |
||
| 189 | } |
||
| 190 | |||
| 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.