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 |
||
| 9 | class Uri implements UriInterface |
||
| 10 | { |
||
| 11 | /** @var string */ |
||
| 12 | private $scheme; |
||
| 13 | |||
| 14 | /** @var string */ |
||
| 15 | private $host; |
||
| 16 | |||
| 17 | /** @var int */ |
||
| 18 | private $port; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | private $user; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | private $password; |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | private $path; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | private $query; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | private $fragment; |
||
| 34 | |||
| 35 | public function __construct(string $uri) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function getScheme() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function getAuthority() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function getUserInfo() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function getHost() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function getPort() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | public function getPath() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function getQuery() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function getFragment() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Return an instance with the specified scheme. |
||
| 133 | * |
||
| 134 | * This method MUST retain the state of the current instance, and return |
||
| 135 | * an instance that contains the specified scheme. |
||
| 136 | * |
||
| 137 | * Implementations MUST support the schemes "http" and "https" case |
||
| 138 | * insensitively, and MAY accommodate other schemes if required. |
||
| 139 | * |
||
| 140 | * An empty scheme is equivalent to removing the scheme. |
||
| 141 | * |
||
| 142 | * @param string $scheme The scheme to use with the new instance. |
||
| 143 | * @return static A new instance with the specified scheme. |
||
| 144 | * @throws \InvalidArgumentException for invalid or unsupported schemes. |
||
| 145 | */ |
||
| 146 | public function withScheme($scheme) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | public function withUserInfo($user, $password = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function withHost($host) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | public function withPort($port) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function withPath($path) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function withQuery($query) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | View Code Duplication | public function withFragment($fragment) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function __toString() |
||
| 281 | } |
||
| 282 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: