| Conditions | 23 |
| Paths | > 20000 |
| Total Lines | 126 |
| Code Lines | 72 |
| 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 |
||
| 171 | private function split_url($url, $decode = true) |
||
| 172 | { |
||
| 173 | // Character sets from RFC3986. |
||
| 174 | $xunressub = 'a-zA-Z\d\-._~\!$&\'()*+,;='; |
||
| 175 | $xpchar = $xunressub . ':@%'; |
||
| 176 | |||
| 177 | // Scheme from RFC3986. |
||
| 178 | $xscheme = '([a-zA-Z][a-zA-Z\d+\-.]*)'; |
||
| 179 | |||
| 180 | // User info (user + password) from RFC3986. |
||
| 181 | $xuserinfo = '(([' . $xunressub . '%]*)' . |
||
| 182 | '(:([' . $xunressub . ':%]*))?)'; |
||
| 183 | |||
| 184 | // IPv4 from RFC3986 (without digit constraints). |
||
| 185 | $xipv4 = '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'; |
||
| 186 | |||
| 187 | // IPv6 from RFC2732 (without digit and grouping constraints). |
||
| 188 | $xipv6 = '(\[([a-fA-F\d.:]+)\])'; |
||
| 189 | |||
| 190 | // Host name from RFC1035. Technically, must start with a letter. |
||
| 191 | // Relax that restriction to better parse URL structure, then |
||
| 192 | // leave host name validation to application. |
||
| 193 | $xhost_name = '([a-zA-Z\d\-.%]+)'; |
||
| 194 | |||
| 195 | // Authority from RFC3986. Skip IP future. |
||
| 196 | $xhost = '(' . $xhost_name . '|' . $xipv4 . '|' . $xipv6 . ')'; |
||
| 197 | $xport = '(\d*)'; |
||
| 198 | $xauthority = '((' . $xuserinfo . '@)?' . $xhost . |
||
| 199 | '?(:' . $xport . ')?)'; |
||
| 200 | |||
| 201 | // Path from RFC3986. Blend absolute & relative for efficiency. |
||
| 202 | $xslash_seg = '(/[' . $xpchar . ']*)'; |
||
| 203 | $xpath_authabs = '((//' . $xauthority . ')((/[' . $xpchar . ']*)*))'; |
||
| 204 | $xpath_rel = '([' . $xpchar . ']+' . $xslash_seg . '*)'; |
||
| 205 | $xpath_abs = '(/(' . $xpath_rel . ')?)'; |
||
| 206 | $xapath = '(' . $xpath_authabs . '|' . $xpath_abs . |
||
| 207 | '|' . $xpath_rel . ')'; |
||
| 208 | |||
| 209 | // Query and fragment from RFC3986. |
||
| 210 | $xqueryfrag = '([' . $xpchar . '/?' . ']*)'; |
||
| 211 | |||
| 212 | // URL. |
||
| 213 | $xurl = '^(' . $xscheme . ':)?' . $xapath . '?' . |
||
| 214 | '(\?' . $xqueryfrag . ')?(#' . $xqueryfrag . ')?$'; |
||
| 215 | |||
| 216 | |||
| 217 | // Split the URL into components. |
||
| 218 | if (!preg_match('!' . $xurl . '!', $url, $m)) { |
||
| 219 | return false; |
||
| 220 | } |
||
| 221 | |||
| 222 | if (!empty($m[2])) { |
||
| 223 | $parts['scheme'] = strtolower($m[2]); |
||
| 224 | } |
||
| 225 | |||
| 226 | if (!empty($m[7])) { |
||
| 227 | if (isset($m[9])) { |
||
| 228 | $parts['user'] = $m[9]; |
||
| 229 | } else { |
||
| 230 | $parts['user'] = ''; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | if (!empty($m[10])) { |
||
| 234 | $parts['pass'] = $m[11]; |
||
| 235 | } |
||
| 236 | |||
| 237 | if (!empty($m[13])) { |
||
| 238 | $h = $parts['host'] = $m[13]; |
||
| 239 | } else { |
||
| 240 | if (!empty($m[14])) { |
||
| 241 | $parts['host'] = $m[14]; |
||
| 242 | } else { |
||
| 243 | if (!empty($m[16])) { |
||
| 244 | $parts['host'] = $m[16]; |
||
| 245 | } else { |
||
| 246 | if (!empty($m[5])) { |
||
| 247 | $parts['host'] = ''; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | if (!empty($m[17])) { |
||
| 253 | $parts['port'] = $m[18]; |
||
| 254 | } |
||
| 255 | |||
| 256 | if (!empty($m[19])) { |
||
| 257 | $parts['path'] = $m[19]; |
||
| 258 | } else { |
||
| 259 | if (!empty($m[21])) { |
||
| 260 | $parts['path'] = $m[21]; |
||
| 261 | } else { |
||
| 262 | if (!empty($m[25])) { |
||
| 263 | $parts['path'] = $m[25]; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | if (!empty($m[27])) { |
||
| 269 | $parts['query'] = $m[28]; |
||
| 270 | } |
||
| 271 | if (!empty($m[29])) { |
||
| 272 | $parts['fragment'] = $m[30]; |
||
| 273 | } |
||
| 274 | |||
| 275 | if (!$decode) { |
||
| 276 | return $parts; |
||
| 277 | } |
||
| 278 | if (!empty($parts['user'])) { |
||
| 279 | $parts['user'] = rawurldecode($parts['user']); |
||
| 280 | } |
||
| 281 | if (!empty($parts['pass'])) { |
||
| 282 | $parts['pass'] = rawurldecode($parts['pass']); |
||
| 283 | } |
||
| 284 | if (!empty($parts['path'])) { |
||
| 285 | $parts['path'] = rawurldecode($parts['path']); |
||
| 286 | } |
||
| 287 | if (isset($h)) { |
||
| 288 | $parts['host'] = rawurldecode($parts['host']); |
||
| 289 | } |
||
| 290 | if (!empty($parts['query'])) { |
||
| 291 | $parts['query'] = rawurldecode($parts['query']); |
||
| 292 | } |
||
| 293 | if (!empty($parts['fragment'])) { |
||
| 294 | $parts['fragment'] = rawurldecode($parts['fragment']); |
||
| 295 | } |
||
| 296 | return $parts; |
||
| 297 | } |
||
| 298 | } |