| Total Complexity | 70 |
| Total Lines | 290 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UrlConverter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UrlConverter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class UrlConverter |
||
| 8 | { |
||
| 9 | |||
| 10 | public function relativeToAbsolute(string $baseUrl, string $relativeUrl) |
||
| 11 | { |
||
| 12 | // If relative URL has a scheme, clean path and return. |
||
| 13 | $r = $this->split_url($relativeUrl); |
||
| 14 | if ($r === false) { |
||
|
|
|||
| 15 | return false; |
||
| 16 | } |
||
| 17 | if (!empty($r['scheme'])) { |
||
| 18 | if (!empty($r['path']) && $r['path'][0] == '/') { |
||
| 19 | $r['path'] = $this->url_remove_dot_segments($r['path']); |
||
| 20 | } |
||
| 21 | return $this->join_url($r); |
||
| 22 | } |
||
| 23 | |||
| 24 | // Make sure the base URL is absolute. |
||
| 25 | $b = $this->split_url($baseUrl); |
||
| 26 | if ($b === false || empty($b['scheme']) || empty($b['host'])) { |
||
| 27 | return false; |
||
| 28 | } |
||
| 29 | $r['scheme'] = $b['scheme']; |
||
| 30 | |||
| 31 | // If relative URL has an authority, clean path and return. |
||
| 32 | if (isset($r['host'])) { |
||
| 33 | if (!empty($r['path'])) { |
||
| 34 | $r['path'] = $this->url_remove_dot_segments($r['path']); |
||
| 35 | } |
||
| 36 | return $this->join_url($r); |
||
| 37 | } |
||
| 38 | unset($r['port']); |
||
| 39 | unset($r['user']); |
||
| 40 | unset($r['pass']); |
||
| 41 | |||
| 42 | // Copy base authority. |
||
| 43 | $r['host'] = $b['host']; |
||
| 44 | if (isset($b['port'])) { |
||
| 45 | $r['port'] = $b['port']; |
||
| 46 | } |
||
| 47 | if (isset($b['user'])) { |
||
| 48 | $r['user'] = $b['user']; |
||
| 49 | } |
||
| 50 | if (isset($b['pass'])) { |
||
| 51 | $r['pass'] = $b['pass']; |
||
| 52 | } |
||
| 53 | |||
| 54 | // If relative URL has no path, use base path |
||
| 55 | if (empty($r['path'])) { |
||
| 56 | if (!empty($b['path'])) { |
||
| 57 | $r['path'] = $b['path']; |
||
| 58 | } |
||
| 59 | if (!isset($r['query']) && isset($b['query'])) { |
||
| 60 | $r['query'] = $b['query']; |
||
| 61 | } |
||
| 62 | return $this->join_url($r); |
||
| 63 | } |
||
| 64 | |||
| 65 | // If relative URL path doesn't start with /, merge with base path |
||
| 66 | if ($r['path'][0] != '/') { |
||
| 67 | $base = \mb_strrchr($b['path'], '/', true, 'UTF-8'); |
||
| 68 | if ($base === false) { |
||
| 69 | $base = ''; |
||
| 70 | } |
||
| 71 | $r['path'] = $base . '/' . $r['path']; |
||
| 72 | } |
||
| 73 | $r['path'] = $this->url_remove_dot_segments($r['path']); |
||
| 74 | return $this->join_url($r); |
||
| 75 | } |
||
| 76 | |||
| 77 | |||
| 78 | private function url_remove_dot_segments($path) |
||
| 79 | { |
||
| 80 | // multi-byte character explode |
||
| 81 | $inSegs = preg_split('!/!u', $path); |
||
| 82 | $outSegs = array(); |
||
| 83 | foreach ($inSegs as $seg) { |
||
| 84 | if ($seg == '' || $seg == '.') { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | if ($seg == '..') { |
||
| 88 | array_pop($outSegs); |
||
| 89 | } else { |
||
| 90 | array_push($outSegs, $seg); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | $outPath = implode('/', $outSegs); |
||
| 94 | if ($path[0] == '/') { |
||
| 95 | $outPath = '/' . $outPath; |
||
| 96 | } |
||
| 97 | // compare last multi-byte character against '/' |
||
| 98 | if ($outPath != '/' && |
||
| 99 | (\mb_strlen($path) - 1) == \mb_strrpos($path, '/')) { |
||
| 100 | $outPath .= '/'; |
||
| 101 | } |
||
| 102 | return $outPath; |
||
| 103 | } |
||
| 104 | |||
| 105 | private function join_url($parts, $encode = true) |
||
| 169 | } |
||
| 170 | |||
| 171 | private function split_url($url, $decode = true) |
||
| 297 | } |
||
| 298 | } |