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 |
||
17 | final class Uri implements UriInterface |
||
18 | { |
||
19 | private static $schemes = ['http' => 80, 'https' => 443]; |
||
20 | |||
21 | private static $charUnreserved = 'a-zA-Z0-9_\-\.~'; |
||
22 | |||
23 | private static $charSubDelims = '!\$&\'\(\)\*\+,;='; |
||
24 | |||
25 | /** @var string Uri scheme. */ |
||
26 | private $scheme = ''; |
||
27 | |||
28 | /** @var string Uri user info. */ |
||
29 | private $userInfo = ''; |
||
30 | |||
31 | /** @var string Uri host. */ |
||
32 | private $host = ''; |
||
33 | |||
34 | /** @var int|null Uri port. */ |
||
35 | private $port; |
||
36 | |||
37 | /** @var string Uri path. */ |
||
38 | private $path = ''; |
||
39 | |||
40 | /** @var string Uri query string. */ |
||
41 | private $query = ''; |
||
42 | |||
43 | /** @var string Uri fragment. */ |
||
44 | private $fragment = ''; |
||
45 | |||
46 | 145 | public function __construct(string $uri = '') |
|
56 | |||
57 | 65 | public function __toString(): string |
|
61 | |||
62 | 23 | public function getScheme(): string |
|
66 | |||
67 | 71 | public function getAuthority(): string |
|
84 | |||
85 | 7 | public function getUserInfo(): string |
|
89 | |||
90 | 86 | public function getHost(): string |
|
94 | |||
95 | 41 | public function getPort(): ?int |
|
99 | |||
100 | 23 | public function getPath(): string |
|
104 | |||
105 | 19 | public function getQuery(): string |
|
109 | |||
110 | 17 | public function getFragment(): string |
|
114 | |||
115 | 27 | public function withScheme($scheme): self |
|
127 | |||
128 | 5 | public function withUserInfo($user, $password = null): self |
|
144 | |||
145 | 7 | public function withHost($host): self |
|
156 | |||
157 | 9 | public function withPort($port): self |
|
168 | |||
169 | 9 | public function withPath($path): self |
|
180 | |||
181 | 6 | public function withQuery($query): self |
|
192 | |||
193 | 6 | public function withFragment($fragment): self |
|
204 | |||
205 | /** |
||
206 | * Apply parse_url parts to a URI. |
||
207 | * |
||
208 | * @param array $parts Array of parse_url parts to apply |
||
209 | */ |
||
210 | 106 | private function applyParts(array $parts): void |
|
223 | |||
224 | /** |
||
225 | * Create a URI string from its various parts. |
||
226 | */ |
||
227 | 65 | private static function createUriString(string $scheme, string $authority, string $path, string $query, string $fragment): string |
|
265 | |||
266 | /** |
||
267 | * Is a given port non-standard for the current scheme? |
||
268 | */ |
||
269 | 12 | private static function isNonStandardPort(string $scheme, int $port): bool |
|
273 | |||
274 | 74 | private function filterScheme($scheme): string |
|
282 | |||
283 | 57 | private function filterHost($host): string |
|
291 | |||
292 | 32 | private function filterPort($port): ?int |
|
305 | |||
306 | 102 | private function filterPath($path): string |
|
314 | |||
315 | 40 | private function filterQueryAndFragment($str): string |
|
323 | |||
324 | 6 | private function rawurlencodeMatchZero(array $match): string |
|
328 | } |
||
329 |