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 string $scheme; | ||
|  | |||
| 13 | |||
| 14 | /** @var string */ | ||
| 15 | private string $host; | ||
| 16 | |||
| 17 | /** @var int */ | ||
| 18 | private int $port; | ||
| 19 | |||
| 20 | /** @var string */ | ||
| 21 | private string $user; | ||
| 22 | |||
| 23 | /** @var string */ | ||
| 24 | private string $password; | ||
| 25 | |||
| 26 | /** @var string */ | ||
| 27 | private string $path; | ||
| 28 | |||
| 29 | /** @var string */ | ||
| 30 | private string $query; | ||
| 31 | |||
| 32 | /** @var string */ | ||
| 33 | private string $fragment; | ||
| 34 | |||
| 35 | public function __construct(string $uri) | ||
| 36 |     { | ||
| 37 | $scheme = parse_url($uri, PHP_URL_SCHEME); | ||
| 38 | $host = parse_url($uri, PHP_URL_HOST); | ||
| 39 | $port = parse_url($uri, PHP_URL_PORT); | ||
| 40 | $port = $port ? (int) $port : 80; | ||
| 41 | $user = parse_url($uri, PHP_URL_USER); | ||
| 42 | $password = parse_url($uri, PHP_URL_PASS); | ||
| 43 | $path = parse_url($uri, PHP_URL_PATH); | ||
| 44 | $query = parse_url($uri, PHP_URL_QUERY); | ||
| 45 | $fragment = parse_url($uri, PHP_URL_FRAGMENT); | ||
| 46 | |||
| 47 | $this->scheme = $scheme ?: ''; | ||
| 48 | $this->host = $host ?: ''; | ||
| 49 | $this->port = $port; | ||
| 50 | $this->user = $user ?: ''; | ||
| 51 | $this->path = $path ?: ''; | ||
| 52 | $this->password = $password ?: ''; | ||
| 53 | $this->query = $query ?: ''; | ||
| 54 | $this->fragment = $fragment ?: ''; | ||
| 55 | } | ||
| 56 | |||
| 57 | /** | ||
| 58 |      * {@inheritdoc} | ||
| 59 | */ | ||
| 60 | public function getScheme() | ||
| 61 |     { | ||
| 62 | return $this->scheme; | ||
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 |      * {@inheritdoc} | ||
| 67 | */ | ||
| 68 | public function getAuthority() | ||
| 69 |     { | ||
| 70 | $userInfo = $this->getUserInfo(); | ||
| 71 | $port = $this->getPort(); | ||
| 72 | $authority = ($userInfo !== '' ? $userInfo.'@' : ''); | ||
| 73 | $authority .= $this->getHost(); | ||
| 74 | $authority .= ($port !== null ? ':'.$port : ''); | ||
| 75 | |||
| 76 | return $authority; | ||
| 77 | } | ||
| 78 | |||
| 79 | /** | ||
| 80 |      * {@inheritdoc} | ||
| 81 | */ | ||
| 82 | public function getUserInfo() | ||
| 83 |     { | ||
| 84 |         if (! $this->user) { | ||
| 85 | return ''; | ||
| 86 | } | ||
| 87 | |||
| 88 |         return $this->user.($this->password ? ":{$this->password}" : ''); | ||
| 89 | } | ||
| 90 | |||
| 91 | /** | ||
| 92 |      * {@inheritdoc} | ||
| 93 | */ | ||
| 94 | public function getHost() | ||
| 95 |     { | ||
| 96 | return $this->host; | ||
| 97 | } | ||
| 98 | |||
| 99 | /** | ||
| 100 |      * {@inheritdoc} | ||
| 101 | */ | ||
| 102 | public function getPort() | ||
| 103 |     { | ||
| 104 | return $this->port; | ||
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 |      * {@inheritdoc} | ||
| 109 | */ | ||
| 110 | public function getPath() | ||
| 111 |     { | ||
| 112 | return $this->path; | ||
| 113 | } | ||
| 114 | |||
| 115 | /** | ||
| 116 |      * {@inheritdoc} | ||
| 117 | */ | ||
| 118 | public function getQuery() | ||
| 119 |     { | ||
| 120 | return $this->query; | ||
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 |      * {@inheritdoc} | ||
| 125 | */ | ||
| 126 | public function getFragment() | ||
| 127 |     { | ||
| 128 | return $this->fragment; | ||
| 129 | } | ||
| 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) | ||
| 147 |     { | ||
| 148 |         if (preg_match('/^[a-zA-Z0-9+-.]+$/', $scheme) === 0) { | ||
| 149 | throw new \InvalidArgumentException( | ||
| 150 |                 "Invalid scheme {$scheme}; please reference RFC3986 for additional details" | ||
| 151 | ); | ||
| 152 | } | ||
| 153 | |||
| 154 | $instance = clone $this; | ||
| 155 | $instance->scheme = strtolower($scheme); | ||
| 156 | |||
| 157 | return $instance; | ||
| 158 | } | ||
| 159 | |||
| 160 | /** | ||
| 161 |      * {@inheritdoc} | ||
| 162 | */ | ||
| 163 | public function withUserInfo($user, $password = null) | ||
| 164 |     { | ||
| 165 | $instance = clone $this; | ||
| 166 | $instance->user = $user; | ||
| 167 |         if ($password) { | ||
| 168 | $instance->password = $password; | ||
| 169 | } | ||
| 170 | |||
| 171 | return $instance; | ||
| 172 | } | ||
| 173 | |||
| 174 | /** | ||
| 175 |      * {@inheritdoc} | ||
| 176 | */ | ||
| 177 | public function withHost($host) | ||
| 178 |     { | ||
| 179 | /** @psalm-suppress DocblockTypeContradiction */ | ||
| 180 |         if (! is_string($host)) { | ||
| 181 |             throw new \InvalidArgumentException("Invalid host: {$host}"); | ||
| 182 | } | ||
| 183 | |||
| 184 |         if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { | ||
| 185 |             $host = "[{$host}]"; | ||
| 186 | } | ||
| 187 | |||
| 188 | $instance = clone $this; | ||
| 189 | $instance->host = $host; | ||
| 190 | |||
| 191 | return $instance; | ||
| 192 | } | ||
| 193 | |||
| 194 | /** | ||
| 195 |      * {@inheritdoc} | ||
| 196 | */ | ||
| 197 | public function withPort($port) | ||
| 198 |     { | ||
| 199 | /** @psalm-suppress DocblockTypeContradiction */ | ||
| 200 |         if (! is_int($port)) { | ||
| 201 |             throw new \InvalidArgumentException("Invalid port: {$port}"); | ||
| 202 | } | ||
| 203 | |||
| 204 | $instance = clone $this; | ||
| 205 | $instance->port = $port; | ||
| 206 | |||
| 207 | return $instance; | ||
| 208 | } | ||
| 209 | |||
| 210 | /** | ||
| 211 |      * {@inheritdoc} | ||
| 212 | */ | ||
| 213 | public function withPath($path) | ||
| 214 |     { | ||
| 215 | /** @psalm-suppress DocblockTypeContradiction */ | ||
| 216 |         if (! is_string($path)) { | ||
| 217 |             throw new \InvalidArgumentException("Invalid path: {$path}"); | ||
| 218 | } | ||
| 219 | |||
| 220 | $instance = clone $this; | ||
| 221 | $instance->path = $path; | ||
| 222 | |||
| 223 | return $instance; | ||
| 224 | } | ||
| 225 | |||
| 226 | /** | ||
| 227 |      * {@inheritdoc} | ||
| 228 | */ | ||
| 229 | public function withQuery($query) | ||
| 230 |     { | ||
| 231 | /** @psalm-suppress DocblockTypeContradiction */ | ||
| 232 |         if (! is_string($query)) { | ||
| 233 |             throw new \InvalidArgumentException("Invalid query: {$query}"); | ||
| 234 | } | ||
| 235 | |||
| 236 | $instance = clone $this; | ||
| 237 | $instance->query = ltrim($query, '?'); | ||
| 238 | |||
| 239 | return $instance; | ||
| 240 | } | ||
| 241 | |||
| 242 | /** | ||
| 243 |      * {@inheritdoc} | ||
| 244 | */ | ||
| 245 | public function withFragment($fragment) | ||
| 246 |     { | ||
| 247 | /** @psalm-suppress DocblockTypeContradiction */ | ||
| 248 |         if (! is_string($fragment)) { | ||
| 249 |             throw new \InvalidArgumentException("Invalid fragment: {$fragment}"); | ||
| 250 | } | ||
| 251 | |||
| 252 | $instance = clone $this; | ||
| 253 | $instance->fragment = ltrim($fragment, '#'); | ||
| 254 | |||
| 255 | return $instance; | ||
| 256 | } | ||
| 257 | |||
| 258 | /** | ||
| 259 |      * {@inheritdoc} | ||
| 260 | */ | ||
| 261 | public function __toString() | ||
| 262 |     { | ||
| 263 | $scheme = $this->getScheme(); | ||
| 264 | $authority = $this->getAuthority(); | ||
| 265 | $path = $this->getPath(); | ||
| 266 | $query = $this->getQuery(); | ||
| 267 | $fragment = $this->getFragment(); | ||
| 268 |         $uri = $scheme ? "{$scheme}:" : ''; | ||
| 269 |         $uri .= $authority ? "//{$authority}" : ''; | ||
| 270 | |||
| 271 |         if ($path) { | ||
| 272 |             if ($authority && strlen(ltrim($path, '/')) === 0) { | ||
| 273 | $uri .= '/'; | ||
| 274 |             } elseif (! $authority && strpos($path, '//') === 0) { | ||
| 275 | $uri .= '/'.ltrim($path, '/'); | ||
| 276 |             } else { | ||
| 277 | $uri .= $path; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | $uri .= $query ? '?'.ltrim($query, '?') : ''; | ||
| 282 | $uri .= $fragment ? '#'.ltrim($fragment, '#') : ''; | ||
| 283 | |||
| 284 | return $uri; | ||
| 285 | } | ||
| 286 | } | ||
| 287 |