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 | |||
12 | /** |
||
13 | * Uri scheme (without "://" suffix) |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $scheme = ''; |
||
18 | |||
19 | /** |
||
20 | * Uri user |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $user = ''; |
||
25 | |||
26 | /** |
||
27 | * Uri password |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $password = ''; |
||
32 | |||
33 | /** |
||
34 | * Uri host |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $host = ''; |
||
39 | |||
40 | /** |
||
41 | * Uri port number |
||
42 | * |
||
43 | * @var null|int |
||
44 | */ |
||
45 | protected $port; |
||
46 | |||
47 | /** |
||
48 | * Uri base path |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $basePath = ''; |
||
53 | |||
54 | /** |
||
55 | * Uri path |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $path = ''; |
||
60 | |||
61 | /** |
||
62 | * Uri query string (without "?" prefix) |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $query = ''; |
||
67 | |||
68 | /** |
||
69 | * Uri fragment string (without "#" prefix) |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $fragment = ''; |
||
74 | |||
75 | /** |
||
76 | * Instance new Uri. |
||
77 | * |
||
78 | * @param string $scheme Uri scheme. |
||
79 | * @param string $host Uri host. |
||
80 | * @param int $port Uri port number. |
||
81 | * @param string $path Uri path. |
||
82 | * @param string $query Uri query string. |
||
83 | * @param string $fragment Uri fragment. |
||
84 | * @param string $user Uri user. |
||
85 | * @param string $password Uri password. |
||
86 | */ |
||
87 | public function __construct( |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function getScheme() |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function getAuthority() |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function getUserInfo() |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | public function getHost() |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function getPort() |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getPath() |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function getQuery() |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function getFragment() |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function withScheme($scheme) |
||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function withUserInfo($user, $password = null) |
||
198 | |||
199 | /** |
||
200 | * {@inheritdoc} |
||
201 | */ |
||
202 | public function withHost($host) |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function withPort($port) |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function withPath($path) |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | public function withQuery($query) |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function withFragment($fragment) |
||
253 | |||
254 | /** |
||
255 | * withString function. |
||
256 | * |
||
257 | * @access protected |
||
258 | * @param mixed $str |
||
|
|||
259 | * @return Uri |
||
260 | */ |
||
261 | protected function withString($string, $name = 'query') |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function __toString() |
||
293 | |||
294 | /* |
||
295 | END OF UriInterface Implementation |
||
296 | */ |
||
297 | |||
298 | /** |
||
299 | * filter scheme given to only allow certain scheme, no file:// or ftp:// or other scheme because its http message uri interface |
||
300 | * |
||
301 | * @access protected |
||
302 | * @param string $scheme |
||
303 | * @return string $scheme |
||
304 | * @throws InvalidArgumentException if not corret scheme is present |
||
305 | */ |
||
306 | protected function filterScheme(string $scheme) |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Filter allowable port to minimize risk |
||
325 | * |
||
326 | * @access protected |
||
327 | * @param integer|null $port |
||
328 | * @return null|integer $port |
||
329 | * @throws InvalidArgumentException for incorrect port assigned |
||
330 | */ |
||
331 | protected function filterPort($port) |
||
339 | |||
340 | /** |
||
341 | * Path allowed chars filter, no weird path on uri yes?. |
||
342 | * |
||
343 | * @access protected |
||
344 | * @param string $path |
||
345 | * @return string of cleared path |
||
346 | */ |
||
347 | protected function filterPath($path) |
||
357 | |||
358 | /** |
||
359 | * replace query to clear not allowed chars |
||
360 | * |
||
361 | * @access protected |
||
362 | * @param string $query |
||
363 | * @return string of replaced query |
||
364 | */ |
||
365 | protected function filterQuery($query) |
||
375 | |||
376 | /** |
||
377 | * cek if current uri scheme use standard port |
||
378 | * |
||
379 | * @access protected |
||
380 | * @return boolean |
||
381 | */ |
||
382 | protected function hasStandardPort() |
||
386 | |||
387 | |||
388 | /** |
||
389 | * get BasePath property. |
||
390 | * |
||
391 | * @access public |
||
392 | * @return string basePath |
||
393 | */ |
||
394 | public function getBasePath() |
||
398 | |||
399 | /** |
||
400 | * Set BasePath Function to rewrite request. |
||
401 | * |
||
402 | * @access public |
||
403 | * @param string $basePath |
||
404 | * @return void |
||
405 | */ |
||
406 | public function withBasePath(string $basePath) |
||
410 | |||
411 | |||
412 | /** |
||
413 | * get Base Url |
||
414 | * |
||
415 | * @access public |
||
416 | * @return string |
||
417 | */ |
||
418 | public function getBaseUrl() |
||
432 | |||
433 | /** |
||
434 | * Create uri Instance from header $_SERVER. |
||
435 | * |
||
436 | * @access public |
||
437 | * @static |
||
438 | * @return Uri |
||
439 | */ |
||
440 | public static function createFromServer($serv) |
||
484 | |||
485 | |||
486 | /** |
||
487 | * Create Uri Instance from string http://www.example.com/url/path.html |
||
488 | * |
||
489 | * @access public |
||
490 | * @static |
||
491 | * @param string $uri |
||
492 | * @return Uri |
||
493 | */ |
||
494 | public static function createFromString(string $uri) |
||
508 | } |
||
509 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.