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 |
||
10 | class Uri implements UriInterface |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $scheme = 'http'; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $userInfo = ''; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $rawUserInfo = ''; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $host; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | private $port = 80; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $path = '/'; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $query = ''; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $fragment = ''; |
||
51 | |||
52 | /** |
||
53 | * @var bool |
||
54 | */ |
||
55 | private $explicitPortSpecified = false; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | */ |
||
60 | private $explicitTrailingHostSlash = false; |
||
61 | |||
62 | /** |
||
63 | * @param string $uri |
||
64 | */ |
||
65 | public function __construct($uri = null) |
||
71 | |||
72 | /** |
||
73 | * @param string $uri |
||
74 | */ |
||
75 | protected function parseUri($uri): void |
||
120 | |||
121 | /** |
||
122 | * @param string $rawUserInfo |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | protected function protectUserInfo($rawUserInfo) |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getScheme() |
||
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getUserInfo() |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getRawUserInfo() |
||
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | public function getHost() |
||
173 | |||
174 | /** |
||
175 | * @return int |
||
176 | */ |
||
177 | public function getPort() |
||
181 | |||
182 | /** |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getPath() |
||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | public function getQuery() |
||
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getFragment() |
||
205 | |||
206 | /** |
||
207 | * Uses protected user info by default as per rfc3986-3.2.1 |
||
208 | * Uri::getRawAuthority() is available if plain-text password information is desirable. |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getAuthority() |
||
223 | |||
224 | /** |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getRawAuthority() |
||
238 | |||
239 | /** |
||
240 | * @return string |
||
241 | */ |
||
242 | public function getAbsoluteUri() |
||
262 | |||
263 | /** |
||
264 | * @return string |
||
265 | */ |
||
266 | public function getRelativeUri() |
||
278 | |||
279 | /** |
||
280 | * Uses protected user info by default as per rfc3986-3.2.1 |
||
281 | * Uri::getAbsoluteUri() is available if plain-text password information is desirable. |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | public function __toString() |
||
305 | |||
306 | /** |
||
307 | * @param $path |
||
308 | */ |
||
309 | public function setPath($path): void |
||
321 | |||
322 | /** |
||
323 | * @param string $query |
||
324 | */ |
||
325 | public function setQuery($query): void |
||
329 | |||
330 | /** |
||
331 | * @param string $var |
||
332 | * @param string $val |
||
333 | */ |
||
334 | public function addToQuery($var, $val): void |
||
341 | |||
342 | /** |
||
343 | * @param string $fragment |
||
344 | */ |
||
345 | public function setFragment($fragment): void |
||
349 | |||
350 | /** |
||
351 | * @param string $scheme |
||
352 | */ |
||
353 | public function setScheme($scheme): void |
||
357 | |||
358 | /** |
||
359 | * @param string $userInfo |
||
360 | */ |
||
361 | public function setUserInfo($userInfo): void |
||
366 | |||
367 | /** |
||
368 | * @param int $port |
||
369 | */ |
||
370 | public function setPort($port): void |
||
380 | |||
381 | /** |
||
382 | * @param string $host |
||
383 | */ |
||
384 | public function setHost($host): void |
||
388 | |||
389 | /** |
||
390 | * @return bool |
||
391 | */ |
||
392 | public function hasExplicitTrailingHostSlash() |
||
396 | |||
397 | /** |
||
398 | * @return bool |
||
399 | */ |
||
400 | public function hasExplicitPortSpecified() |
||
404 | } |
||
405 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.