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 |
||
114 | function url_parser($url) { |
||
115 | |||
116 | // multiple /// messes up parse_url, replace 3 or more with 2 |
||
117 | $url = preg_replace('/(\/{2,})/','//',$url); |
||
118 | |||
119 | $parse_url = parse_url($url); |
||
120 | |||
121 | if(empty($parse_url["scheme"])) { |
||
122 | $parse_url["scheme"] = "http"; |
||
123 | } |
||
124 | if(empty($parse_url["host"]) && !empty($parse_url["path"])) { |
||
125 | // Strip slash from the beginning of path |
||
126 | $parse_url["host"] = ltrim($parse_url["path"], '\/'); |
||
127 | $parse_url["path"] = ""; |
||
128 | } |
||
129 | |||
130 | $url_array = array("url" => "", "url_display" => ""); |
||
131 | |||
132 | // Check if scheme is correct |
||
133 | if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) { |
||
134 | $url_array["url"] .= 'http'.'://'; |
||
135 | } else { |
||
136 | $url_array["url"] .= $parse_url["scheme"].'://'; |
||
137 | } |
||
138 | |||
139 | // Check if the right amount of "www" is set. |
||
140 | $explode_host = explode(".", $parse_url["host"]); |
||
141 | |||
142 | // Remove empty entries |
||
143 | $explode_host = array_filter($explode_host); |
||
144 | // And reassign indexes |
||
145 | $explode_host = array_values($explode_host); |
||
146 | |||
147 | // Contains subdomain |
||
148 | if(count($explode_host) > 2) { |
||
149 | // Check if subdomain only contains the letter w(then not any other subdomain). |
||
150 | if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) { |
||
151 | // Replace with "www" to avoid "ww" or "wwww", etc. |
||
152 | $explode_host[0] = "www"; |
||
153 | |||
154 | } |
||
155 | } |
||
156 | |||
157 | $url_array["url"] .= implode(".",$explode_host); |
||
158 | $url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash |
||
159 | |||
160 | if(!empty($parse_url["port"])) { |
||
161 | $url_array["url"] .= ":".$parse_url["port"]; |
||
162 | } |
||
163 | if(!empty($parse_url["path"])) { |
||
164 | $url_array["url"] .= $parse_url["path"]; |
||
165 | } |
||
166 | if(!empty($parse_url["query"])) { |
||
167 | $url_array["url"] .= '?'.$parse_url["query"]; |
||
168 | } |
||
169 | if(!empty($parse_url["fragment"])) { |
||
170 | $url_array["url"] .= '#'.$parse_url["fragment"]; |
||
171 | } |
||
172 | |||
173 | |||
174 | return $url_array; |
||
175 | } |
||
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.