| Conditions | 11 |
| Paths | 384 |
| Total Lines | 62 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 41 | $parse_url["scheme"] = "http"; |
||
| 42 | } |
||
| 43 | if(empty($parse_url["host"]) && !empty($parse_url["path"])) { |
||
| 44 | // Strip slash from the beginning of path |
||
| 45 | $parse_url["host"] = ltrim($parse_url["path"], '\/'); |
||
| 46 | $parse_url["path"] = ""; |
||
| 47 | } |
||
| 48 | return $parse_url; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * parse url, try to correct errors and return valid url + display-url. |
||
| 53 | * |
||
| 54 | * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
||
| 55 | * @example gopher:/ww.example.com => gopher://www.example.com |
||
| 56 | * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
||
| 57 | * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
||
| 58 | * @example .example.com/ => http://example.com/ |
||
| 59 | * @example example.com =>http://example.com |
||
| 60 | * @example subdomain.example.com => http://subdomain.example.com |
||
| 61 | * |
||
| 62 | * @param string $url Any somewhat valid url. |
||
| 63 | * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
||
| 64 | */ |
||
| 65 | public static function url_parser($url) { |
||
| 66 | |||
| 67 | $parse_url = self::auto_correct_parse_url($url); |
||
| 68 | |||
| 69 | $url_array = array("url" => "", "url_display" => ""); |
||
| 70 | |||
| 71 | // Check if scheme is correct |
||
| 72 | if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) { |
||
| 73 | $url_array["url"] .= 'http'.'://'; |
||
| 74 | } else { |
||
| 75 | $url_array["url"] .= $parse_url["scheme"].'://'; |
||
| 76 | } |
||
| 77 | |||
| 78 | // Check if the right amount of "www" is set. |
||
| 79 | $explode_host = explode(".", $parse_url["host"]); |
||
| 80 | |||
| 81 | // Remove empty entries |
||
| 82 | $explode_host = array_filter($explode_host); |
||
| 83 | // And reassign indexes |
||
| 84 | $explode_host = array_values($explode_host); |
||
| 85 | |||
| 86 | // Contains subdomain |
||
| 87 | if(count($explode_host) > 2) { |
||
| 88 | // Check if subdomain only contains the letter w(then not any other subdomain). |
||
| 89 | if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) { |
||
| 90 | // Replace with "www" to avoid "ww" or "wwww", etc. |
||
| 91 | $explode_host[0] = "www"; |
||
| 92 | |||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $url_array["url"] .= implode(".",$explode_host); |
||
| 97 | $url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash |
||
| 98 | |||
| 99 | if(!empty($parse_url["port"])) { |
||
| 100 | $url_array["url"] .= ":".$parse_url["port"]; |
||
| 101 | } |
||
| 102 | if(!empty($parse_url["path"])) { |
||
| 103 | $url_array["url"] .= $parse_url["path"]; |
||
| 168 | } |
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.