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 |
||
16 | class Uri |
||
17 | { |
||
18 | private static $partNames = [ |
||
19 | 'scheme', |
||
20 | 'user', |
||
21 | 'pass', |
||
22 | 'host', |
||
23 | 'port', |
||
24 | 'path', |
||
25 | 'query', |
||
26 | 'fragment', |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $raw; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $parts; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $authority; |
||
43 | |||
44 | /** |
||
45 | * @var string[] |
||
46 | */ |
||
47 | private $segments; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $primaryIdentifier; |
||
53 | |||
54 | /** |
||
55 | * Constructor. |
||
56 | * |
||
57 | * @param string $rawUri |
||
58 | */ |
||
59 | 382 | public function __construct($rawUri) |
|
63 | |||
64 | /** |
||
65 | * @return string |
||
66 | */ |
||
67 | 3 | public function getRawUri() |
|
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | 55 | public function getRawPointer() |
|
79 | |||
80 | /** |
||
81 | * @return bool |
||
82 | */ |
||
83 | 90 | public function isAbsolute() |
|
84 | { |
||
85 | 90 | return $this->parts['scheme'] !== ''; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | 63 | public function getScheme() |
|
92 | { |
||
93 | 63 | return $this->parts['scheme']; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | 63 | public function getAuthority() |
|
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | 63 | public function getPath() |
|
108 | { |
||
109 | 63 | return $this->parts['path']; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | 63 | public function getQuery() |
|
116 | { |
||
117 | 63 | return $this->parts['query']; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @return string[] |
||
122 | */ |
||
123 | 65 | public function getPointerSegments() |
|
127 | |||
128 | /** |
||
129 | * Returns the primary resource identifier part of the URI, i.e. everything |
||
130 | * excluding its fragment part. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 350 | public function getPrimaryResourceIdentifier() |
|
138 | |||
139 | /** |
||
140 | * Resolves the current (relative) URI against another (absolute) URI. |
||
141 | * Example:. |
||
142 | * |
||
143 | * Current = foo.json |
||
144 | * Other = http://localhost/bar/baz |
||
145 | * Resolved = http://localhost/bar/foo.json |
||
146 | * |
||
147 | * @param Uri $uri |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 65 | public function resolveAgainst(Uri $uri) |
|
170 | |||
171 | /** |
||
172 | * Returns whether two URIs share the same primary resource identifier, |
||
173 | * i.e. whether they point to the same document. |
||
174 | * |
||
175 | * @param Uri $uri |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | 5 | public function isSamePrimaryResource(Uri $uri) |
|
187 | |||
188 | 382 | private function buildFromRawUri($rawUri) |
|
211 | |||
212 | 381 | private function buildAuthority() |
|
231 | |||
232 | 381 | private function buildSegments() |
|
252 | |||
253 | 381 | private function buildPrimaryIdentifier() |
|
269 | |||
270 | 63 | private function buildResolvedUriAgainst(Uri $uri) |
|
294 | |||
295 | 14 | private function buildResolvedPathAgainst($againstPath) |
|
307 | |||
308 | 63 | private function appendRelativeParts($absolutePart, $query, $fragment) |
|
320 | } |
||
321 |