Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
13 | class Uri implements UriInterface |
||
14 | { |
||
15 | const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; |
||
16 | const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; |
||
17 | const REPLACE_QUERY = ['=' => '%3D', '&' => '%26']; |
||
18 | const SCHEMES = [ |
||
19 | 'http' => 80, |
||
20 | 'https' => 443, |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * @var string Uri scheme. |
||
25 | */ |
||
26 | private $scheme; |
||
27 | |||
28 | /** |
||
29 | * @var string Uri user info. |
||
30 | */ |
||
31 | private $userInfo; |
||
32 | |||
33 | /** |
||
34 | * @var string Uri host. |
||
35 | */ |
||
36 | private $host; |
||
37 | |||
38 | /** |
||
39 | * @var int Uri port. |
||
40 | */ |
||
41 | private $port; |
||
42 | |||
43 | /** |
||
44 | * @var string Uri path. |
||
45 | */ |
||
46 | private $path; |
||
47 | |||
48 | /** |
||
49 | * @var string Uri query string. |
||
50 | */ |
||
51 | private $query; |
||
52 | |||
53 | /** |
||
54 | * @var string Uri fragment. |
||
55 | */ |
||
56 | private $fragment; |
||
57 | |||
58 | /** |
||
59 | * @param string $uri URI to parse and wrap. |
||
60 | */ |
||
61 | 99 | public function __construct(string $uri = '') |
|
80 | |||
81 | 59 | public function __toString() : string |
|
91 | |||
92 | /** |
||
93 | * Removes dot segments from a path and returns the new path. |
||
94 | * |
||
95 | * @param string $path |
||
96 | * |
||
97 | * @return string |
||
98 | * @link http://tools.ietf.org/html/rfc3986#section-5.2.4 |
||
99 | */ |
||
100 | 36 | public static function removeDotSegments(string $path) : string |
|
134 | |||
135 | /** |
||
136 | * Resolve a base URI with a relative URI and return a new URI. |
||
137 | * |
||
138 | * @param UriInterface $base Base URI |
||
139 | * @param string $rel Relative URI |
||
140 | * |
||
141 | * @return UriInterface |
||
142 | */ |
||
143 | 39 | public static function resolve(UriInterface $base, string $rel) : UriInterface |
|
209 | |||
210 | /** |
||
211 | * Create a new URI with a specific query string value removed. |
||
212 | * |
||
213 | * Any existing query string values that exactly match the provided key are |
||
214 | * removed. |
||
215 | * |
||
216 | * Note: this function will convert "=" to "%3D" and "&" to "%26". |
||
217 | * |
||
218 | * @param UriInterface $uri URI to use as a base. |
||
219 | * @param string $key Query string key value pair to remove. |
||
220 | * |
||
221 | * @return UriInterface |
||
222 | */ |
||
223 | 1 | public static function withoutQueryValue(UriInterface $uri, string $key) : UriInterface |
|
239 | |||
240 | /** |
||
241 | * Create a new URI with a specific query string value. |
||
242 | * |
||
243 | * Any existing query string values that exactly match the provided key are |
||
244 | * removed and replaced with the given key value pair. |
||
245 | * |
||
246 | * Note: this function will convert "=" to "%3D" and "&" to "%26". |
||
247 | * |
||
248 | * @param UriInterface $uri URI to use as a base. |
||
249 | * @param string $key Key to set. |
||
250 | * @param string $value Value to set. |
||
251 | * |
||
252 | * @return UriInterface |
||
253 | */ |
||
254 | 2 | public static function withQueryValue(UriInterface $uri, string $key, string $value = null) : UriInterface |
|
278 | |||
279 | 2 | public static function withAddedQueryValues(UriInterface $uri, array $queryValues) : UriInterface |
|
299 | |||
300 | /** |
||
301 | * Create a URI from a hash of parse_url parts. |
||
302 | * |
||
303 | * @param array $parts |
||
304 | * |
||
305 | * @return self |
||
306 | */ |
||
307 | public static function fromParts(array $parts) : self |
||
314 | |||
315 | 40 | public function getScheme() : string |
|
319 | |||
320 | 61 | public function getAuthority() : string |
|
337 | |||
338 | 2 | public function getUserInfo() : string |
|
342 | |||
343 | 29 | public function getHost() : string |
|
347 | |||
348 | 14 | public function getPort() |
|
352 | |||
353 | 63 | public function getPath() : string |
|
357 | |||
358 | 47 | public function getQuery() : string |
|
362 | |||
363 | 41 | public function getFragment() : string |
|
367 | |||
368 | 7 | public function withScheme($scheme) : self |
|
382 | |||
383 | 1 | public function withUserInfo($user, $password = null) : self |
|
399 | |||
400 | 8 | public function withHost($host) : self |
|
411 | |||
412 | 8 | public function withPort($port) : self |
|
425 | |||
426 | 10 | public function withPath($path) : self |
|
445 | |||
446 | 11 | public function withQuery($query) : self |
|
470 | |||
471 | 1 | public function withFragment($fragment) : self |
|
488 | |||
489 | /** |
||
490 | * Apply parse_url parts to a URI. |
||
491 | * |
||
492 | * @param array $parts Array of parse_url parts to apply. |
||
493 | */ |
||
494 | 97 | private function applyParts(array $parts) |
|
517 | |||
518 | /** |
||
519 | * Create a URI string from its various parts |
||
520 | * |
||
521 | * @param string $scheme |
||
522 | * @param string $authority |
||
523 | * @param string $path |
||
524 | * @param string $query |
||
525 | * @param string $fragment |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | 59 | private static function createUriString( |
|
573 | |||
574 | /** |
||
575 | * Is a given port non-standard for the current scheme? |
||
576 | * |
||
577 | * @param string $scheme |
||
578 | * @param string $host |
||
579 | * @param int $port |
||
580 | * |
||
581 | * @return bool |
||
582 | */ |
||
583 | 62 | private static function isNonStandardPort(string $scheme = null, string $host = null, int $port = null) : bool |
|
595 | |||
596 | /** |
||
597 | * @param string $scheme |
||
598 | * |
||
599 | * @return string |
||
600 | */ |
||
601 | 76 | private function filterScheme(string $scheme) : string |
|
608 | |||
609 | /** |
||
610 | * @param string $scheme |
||
611 | * @param string $host |
||
612 | * @param int $port |
||
613 | * |
||
614 | * @return int|null |
||
615 | * |
||
616 | * @throws \InvalidArgumentException If the port is invalid. |
||
617 | */ |
||
618 | 13 | private function filterPort(string $scheme, string $host, int $port = null) |
|
631 | |||
632 | /** |
||
633 | * Filters the path of a URI |
||
634 | * |
||
635 | * @param $path |
||
636 | * |
||
637 | * @return string |
||
638 | */ |
||
639 | 92 | View Code Duplication | private function filterPath(string $path) : string |
647 | |||
648 | /** |
||
649 | * Filters the query string or fragment of a URI. |
||
650 | * |
||
651 | * @param $str |
||
652 | * |
||
653 | * @return string |
||
654 | */ |
||
655 | 57 | View Code Duplication | private function filterQueryAndFragment(string $str) : string |
663 | |||
664 | 4 | private function rawurlencodeMatchZero(array $match) : string |
|
668 | } |
||
669 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: