Complex classes like Uri 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 Uri, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | final class Uri implements UriInterface |
||
19 | { |
||
20 | use LowercaseTrait; |
||
21 | |||
22 | private const SCHEMES = ['http' => 80, 'https' => 443]; |
||
23 | |||
24 | private const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; |
||
25 | |||
26 | private const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; |
||
27 | |||
28 | /** @var string Uri scheme. */ |
||
29 | private $scheme = ''; |
||
30 | |||
31 | /** @var string Uri user info. */ |
||
32 | private $userInfo = ''; |
||
33 | |||
34 | /** @var string Uri host. */ |
||
35 | private $host = ''; |
||
36 | |||
37 | /** @var int|null Uri port. */ |
||
38 | private $port; |
||
39 | |||
40 | /** @var string Uri path. */ |
||
41 | private $path = ''; |
||
42 | |||
43 | /** @var string Uri query string. */ |
||
44 | private $query = ''; |
||
45 | |||
46 | /** @var string Uri fragment. */ |
||
47 | private $fragment = ''; |
||
48 | |||
49 | 151 | public function __construct(string $uri = '') |
|
50 | { |
||
51 | 151 | if ('' !== $uri) { |
|
52 | 131 | if (false === $parts = \parse_url($uri)) { |
|
53 | 4 | throw new \InvalidArgumentException("Unable to parse URI: $uri"); |
|
54 | } |
||
55 | |||
56 | // Apply parse_url parts to a URI. |
||
57 | 127 | $this->scheme = isset($parts['scheme']) ? self::lowercase($parts['scheme']) : ''; |
|
58 | 127 | $this->userInfo = $parts['user'] ?? ''; |
|
59 | 127 | $this->host = isset($parts['host']) ? self::lowercase($parts['host']) : ''; |
|
60 | 127 | $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null; |
|
61 | 127 | $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; |
|
62 | 127 | $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
|
63 | 127 | $this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
|
64 | 127 | if (isset($parts['pass'])) { |
|
65 | 5 | $this->userInfo .= ':' . $parts['pass']; |
|
66 | } |
||
67 | } |
||
68 | 147 | } |
|
69 | |||
70 | 68 | public function __toString(): string |
|
71 | { |
||
72 | 68 | return self::createUriString($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment); |
|
73 | } |
||
74 | |||
75 | 8 | public function getScheme(): string |
|
79 | |||
80 | 74 | public function getAuthority(): string |
|
97 | |||
98 | 7 | public function getUserInfo(): string |
|
102 | |||
103 | 91 | public function getHost(): string |
|
107 | |||
108 | 42 | public function getPort(): ?int |
|
112 | |||
113 | 23 | public function getPath(): string |
|
117 | |||
118 | 19 | public function getQuery(): string |
|
122 | |||
123 | 17 | public function getFragment(): string |
|
127 | |||
128 | 12 | public function withScheme($scheme): self |
|
129 | { |
||
144 | |||
145 | 5 | public function withUserInfo($user, $password = null): self |
|
161 | |||
162 | 8 | public function withHost($host): self |
|
177 | |||
178 | 9 | public function withPort($port): self |
|
189 | |||
190 | 9 | public function withPath($path): self |
|
201 | |||
202 | 6 | public function withQuery($query): self |
|
213 | |||
214 | 6 | public function withFragment($fragment): self |
|
225 | |||
226 | /** |
||
227 | * Create a URI string from its various parts. |
||
228 | */ |
||
229 | 68 | private static function createUriString(string $scheme, string $authority, string $path, string $query, string $fragment): string |
|
267 | |||
268 | /** |
||
269 | * Is a given port non-standard for the current scheme? |
||
270 | */ |
||
271 | 12 | private static function isNonStandardPort(string $scheme, int $port): bool |
|
275 | |||
276 | 17 | private function filterPort($port): ?int |
|
289 | |||
290 | 122 | private function filterPath($path): string |
|
298 | |||
299 | 40 | private function filterQueryAndFragment($str): string |
|
307 | |||
308 | 6 | private static function rawurlencodeMatchZero(array $match): string |
|
312 | } |
||
313 |