| Total Complexity | 44 |
| Total Lines | 204 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UriResolver 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 UriResolver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | final class UriResolver |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Removes dot segments from a path and returns the new path. |
||
| 17 | * |
||
| 18 | * @param string $path |
||
| 19 | * |
||
| 20 | * @return string |
||
| 21 | * @link http://tools.ietf.org/html/rfc3986#section-5.2.4 |
||
| 22 | */ |
||
| 23 | public static function removeDotSegments($path) |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Converts the relative URI into a new URI that is resolved against the base URI. |
||
| 55 | * |
||
| 56 | * @param UriInterface $base Base URI |
||
| 57 | * @param UriInterface $rel Relative URI |
||
| 58 | * |
||
| 59 | * @return UriInterface |
||
| 60 | * @link http://tools.ietf.org/html/rfc3986#section-5.2 |
||
| 61 | */ |
||
| 62 | public static function resolve(UriInterface $base, UriInterface $rel) |
||
| 108 | )); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the target URI as a relative reference from the base URI. |
||
| 113 | * |
||
| 114 | * This method is the counterpart to resolve(): |
||
| 115 | * |
||
| 116 | * (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target)) |
||
| 117 | * |
||
| 118 | * One use-case is to use the current request URI as base URI and then generate relative links in your documents |
||
| 119 | * to reduce the document size or offer self-contained downloadable document archives. |
||
| 120 | * |
||
| 121 | * $base = new Uri('http://example.com/a/b/'); |
||
| 122 | * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'. |
||
| 123 | * echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'. |
||
| 124 | * echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'. |
||
| 125 | * echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'. |
||
| 126 | * |
||
| 127 | * This method also accepts a target that is already relative and will try to relativize it further. Only a |
||
| 128 | * relative-path reference will be returned as-is. |
||
| 129 | * |
||
| 130 | * echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well |
||
| 131 | * |
||
| 132 | * @param UriInterface $base Base URI |
||
| 133 | * @param UriInterface $target Target URI |
||
| 134 | * |
||
| 135 | * @return UriInterface The relative URI reference |
||
| 136 | */ |
||
| 137 | public static function relativize(UriInterface $base, UriInterface $target) |
||
| 138 | { |
||
| 139 | if ($target->getScheme() !== '' && |
||
| 140 | ($base->getScheme() !== $target->getScheme() || $target->getAuthority() === '' && $base->getAuthority() !== '') |
||
| 141 | ) { |
||
| 142 | return $target; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (Uri::isRelativePathReference($target)) { |
||
| 146 | // As the target is already highly relative we return it as-is. It would be possible to resolve |
||
| 147 | // the target with `$target = self::resolve($base, $target);` and then try make it more relative |
||
| 148 | // by removing a duplicate query. But let's not do that automatically. |
||
| 149 | return $target; |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($target->getAuthority() !== '' && $base->getAuthority() !== $target->getAuthority()) { |
||
| 153 | return $target->withScheme(''); |
||
| 154 | } |
||
| 155 | |||
| 156 | // We must remove the path before removing the authority because if the path starts with two slashes, the URI |
||
| 157 | // would turn invalid. And we also cannot set a relative path before removing the authority, as that is also |
||
| 158 | // invalid. |
||
| 159 | $emptyPathUri = $target->withScheme('')->withPath('')->withUserInfo('')->withPort(null)->withHost(''); |
||
| 160 | |||
| 161 | if ($base->getPath() !== $target->getPath()) { |
||
| 162 | return $emptyPathUri->withPath(self::getRelativePath($base, $target)); |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($base->getQuery() === $target->getQuery()) { |
||
| 166 | // Only the target fragment is left. And it must be returned even if base and target fragment are the same. |
||
| 167 | return $emptyPathUri->withQuery(''); |
||
| 168 | } |
||
| 169 | |||
| 170 | // If the base URI has a query but the target has none, we cannot return an empty path reference as it would |
||
| 171 | // inherit the base query component when resolving. |
||
| 172 | if ($target->getQuery() === '') { |
||
| 173 | $segments = explode('/', $target->getPath()); |
||
| 174 | $lastSegment = end($segments); |
||
| 175 | |||
| 176 | return $emptyPathUri->withPath($lastSegment === '' ? './' : $lastSegment); |
||
| 177 | } |
||
| 178 | |||
| 179 | return $emptyPathUri; |
||
| 180 | } |
||
| 181 | |||
| 182 | private static function getRelativePath(UriInterface $base, UriInterface $target) |
||
| 213 | } |
||
| 214 | |||
| 215 | private function __construct() |
||
| 217 | // cannot be instantiated |
||
| 218 | } |
||
| 220 |