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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
31 | final class UriResolver |
||
32 | { |
||
33 | /** |
||
34 | * @var array<string,int> |
||
35 | */ |
||
36 | const DOT_SEGMENTS = ['.' => 1, '..' => 1]; |
||
37 | |||
38 | /** |
||
39 | * @codeCoverageIgnore |
||
40 | */ |
||
41 | private function __construct() |
||
44 | |||
45 | /** |
||
46 | * Resolve an URI against a base URI using RFC3986 rules. |
||
47 | * |
||
48 | * If the first argument is a UriInterface the method returns a UriInterface object |
||
49 | * If the first argument is a Psr7UriInterface the method returns a Psr7UriInterface object |
||
50 | * |
||
51 | * @param Psr7UriInterface|UriInterface $uri |
||
52 | * @param Psr7UriInterface|UriInterface $base_uri |
||
53 | * |
||
54 | * @return Psr7UriInterface|UriInterface |
||
55 | */ |
||
56 | 98 | public static function resolve($uri, $base_uri) |
|
91 | |||
92 | /** |
||
93 | * Filter the URI object. |
||
94 | * |
||
95 | * @param mixed $uri an URI object |
||
96 | * |
||
97 | * @throws \TypeError if the URI object does not implements the supported interfaces. |
||
98 | */ |
||
99 | 200 | private static function filterUri($uri): void |
|
105 | |||
106 | /** |
||
107 | * Remove dot segments from the URI path. |
||
108 | */ |
||
109 | 96 | private static function removeDotSegments(string $path): string |
|
130 | |||
131 | /** |
||
132 | * Remove dot segments. |
||
133 | * |
||
134 | * @return array<int, string> |
||
135 | */ |
||
136 | 48 | private static function reducer(array $carry, string $segment): array |
|
150 | |||
151 | /** |
||
152 | * Resolve an URI path and query component. |
||
153 | * |
||
154 | * @param Psr7UriInterface|UriInterface $uri |
||
155 | * @param Psr7UriInterface|UriInterface $base_uri |
||
156 | * |
||
157 | * @return array{0:string, 1:string|null} |
||
158 | */ |
||
159 | 72 | private static function resolvePathAndQuery($uri, $base_uri): array |
|
201 | |||
202 | /** |
||
203 | * Relativize an URI according to a base URI. |
||
204 | * |
||
205 | * This method MUST retain the state of the submitted URI instance, and return |
||
206 | * an URI instance of the same type that contains the applied modifications. |
||
207 | * |
||
208 | * This method MUST be transparent when dealing with error and exceptions. |
||
209 | * It MUST not alter of silence them apart from validating its own parameters. |
||
210 | * |
||
211 | * @param Psr7UriInterface|UriInterface $uri |
||
212 | * @param Psr7UriInterface|UriInterface $base_uri |
||
213 | * |
||
214 | * @return Psr7UriInterface|UriInterface |
||
215 | */ |
||
216 | 102 | public static function relativize($uri, $base_uri) |
|
243 | |||
244 | /** |
||
245 | * Tells whether the component value from both URI object equals. |
||
246 | * |
||
247 | * @param Psr7UriInterface|UriInterface $uri |
||
248 | * @param Psr7UriInterface|UriInterface $base_uri |
||
249 | */ |
||
250 | 62 | private static function componentEquals(string $method, $uri, $base_uri): bool |
|
254 | |||
255 | /** |
||
256 | * Returns the component value from the submitted URI object. |
||
257 | * |
||
258 | * @param Psr7UriInterface|UriInterface $uri |
||
259 | */ |
||
260 | 62 | private static function getComponent(string $method, $uri): ?string |
|
269 | |||
270 | /** |
||
271 | * Filter the URI object. |
||
272 | * |
||
273 | * @param null|mixed $uri |
||
274 | * |
||
275 | * @throws \TypeError if the URI object does not implements the supported interfaces. |
||
276 | * |
||
277 | * @return Psr7UriInterface|UriInterface |
||
278 | */ |
||
279 | 100 | private static function formatHost($uri) |
|
292 | |||
293 | /** |
||
294 | * Tell whether the submitted URI object can be relativize. |
||
295 | * |
||
296 | * @param Psr7UriInterface|UriInterface $uri |
||
297 | * @param Psr7UriInterface|UriInterface $base_uri |
||
298 | */ |
||
299 | 100 | private static function isRelativizable($uri, $base_uri): bool |
|
305 | |||
306 | /** |
||
307 | * Relative the URI for a authority-less target URI. |
||
308 | */ |
||
309 | 40 | private static function relativizePath(string $path, string $basepath): string |
|
328 | |||
329 | /** |
||
330 | * returns the path segments. |
||
331 | * |
||
332 | * @return string[] |
||
333 | */ |
||
334 | 44 | private static function getSegments(string $path): array |
|
342 | |||
343 | /** |
||
344 | * Formatting the path to keep a valid URI. |
||
345 | */ |
||
346 | 40 | private static function formatPath(string $path, string $basepath): string |
|
363 | |||
364 | /** |
||
365 | * Formatting the path to keep a resolvable URI. |
||
366 | */ |
||
367 | 4 | private static function formatPathWithEmptyBaseQuery(string $path): string |
|
375 | } |
||
376 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.