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 |
||
8 | class Uri implements UriInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $scheme = ''; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $userInfo = ''; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $host = ''; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $port; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $path = ''; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $query = ''; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $fragment = ''; |
||
44 | |||
45 | /** |
||
46 | * @var int[] Default Ports for known Schemes. |
||
47 | */ |
||
48 | private $defaultSchemePorts = [ |
||
49 | 57 | 'http' => 80, |
|
50 | 'https' => 443, |
||
51 | 57 | ]; |
|
52 | 42 | ||
53 | /** |
||
54 | * @param string|array $uri |
||
55 | 57 | * @throws InvalidArgumentException on non-string $uri argument |
|
56 | 36 | */ |
|
57 | 36 | public function __construct($uri = '') |
|
74 | 48 | ||
75 | public function __toString() |
||
79 | |||
80 | 48 | private function getUriString() |
|
100 | 51 | ||
101 | 51 | private function parseUriParts($parts) |
|
115 | |||
116 | public function hasScheme() |
||
124 | 48 | ||
125 | 21 | public function getScheme() |
|
129 | |||
130 | 27 | public function getAuthority() |
|
148 | 33 | ||
149 | 27 | public function getUserInfo() |
|
153 | |||
154 | public function hasUserInfo() |
||
162 | |||
163 | public function hasHost() |
||
171 | 33 | ||
172 | 30 | public function getHost() |
|
176 | |||
177 | public function hasPort() |
||
185 | 9 | ||
186 | 6 | public function getPort() |
|
194 | |||
195 | 48 | private function isDefaultPort() |
|
213 | |||
214 | 51 | public function hasPath() |
|
222 | |||
223 | 27 | public function getPath() |
|
228 | 6 | ||
229 | 6 | public function hasQuery() |
|
237 | 6 | ||
238 | 6 | public function getQuery() |
|
242 | |||
243 | 6 | public function hasFragment() |
|
251 | |||
252 | 6 | public function getFragment() |
|
256 | |||
257 | 6 | public function withScheme($scheme) |
|
263 | |||
264 | 6 | public function withUserInfo($user, $password = null) |
|
273 | 6 | ||
274 | 6 | public function withHost($host) |
|
280 | |||
281 | public function withPort($port) |
||
287 | |||
288 | 42 | public function withPath($path) |
|
294 | |||
295 | public function withQuery($query) |
||
301 | |||
302 | public function withFragment($fragment) |
||
308 | |||
309 | /** |
||
310 | * @param array $modifiedParts |
||
311 | * @return Uri |
||
312 | */ |
||
313 | private function withModifiedParts($modifiedParts) |
||
321 | } |
||
322 |