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 |
||
| 17 | class Uri implements UriInterface |
||
| 18 | { |
||
| 19 | private static $schemes = [ |
||
| 20 | 'http' => 80, |
||
| 21 | 'https' => 443, |
||
| 22 | ]; |
||
| 23 | |||
| 24 | private static $charUnreserved = 'a-zA-Z0-9_\-\.~'; |
||
| 25 | private static $charSubDelims = '!\$&\'\(\)\*\+,;='; |
||
| 26 | |||
| 27 | /** @var string Uri scheme. */ |
||
| 28 | private $scheme = ''; |
||
| 29 | |||
| 30 | /** @var string Uri user info. */ |
||
| 31 | private $userInfo = ''; |
||
| 32 | |||
| 33 | /** @var string Uri host. */ |
||
| 34 | private $host = ''; |
||
| 35 | |||
| 36 | /** @var int|null Uri port. */ |
||
| 37 | private $port; |
||
| 38 | |||
| 39 | /** @var string Uri path. */ |
||
| 40 | private $path = ''; |
||
| 41 | |||
| 42 | /** @var string Uri query string. */ |
||
| 43 | private $query = ''; |
||
| 44 | |||
| 45 | /** @var string Uri fragment. */ |
||
| 46 | private $fragment = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $uri |
||
| 50 | */ |
||
| 51 | 108 | public function __construct($uri = '') |
|
| 62 | |||
| 63 | 63 | public function __toString(): string |
|
| 73 | |||
| 74 | 15 | public function getScheme(): string |
|
| 78 | |||
| 79 | 69 | public function getAuthority(): string |
|
| 96 | |||
| 97 | 7 | public function getUserInfo(): string |
|
| 101 | |||
| 102 | 54 | public function getHost(): string |
|
| 106 | |||
| 107 | 42 | public function getPort() |
|
| 111 | |||
| 112 | 17 | public function getPath(): string |
|
| 116 | |||
| 117 | 14 | public function getQuery(): string |
|
| 121 | |||
| 122 | 13 | public function getFragment(): string |
|
| 126 | |||
| 127 | 14 | public function withScheme($scheme): self |
|
| 141 | |||
| 142 | 4 | public function withUserInfo($user, $password = null): self |
|
| 158 | |||
| 159 | 13 | public function withHost($host): self |
|
| 172 | |||
| 173 | 8 | public function withPort($port): self |
|
| 186 | |||
| 187 | 15 | public function withPath($path): self |
|
| 200 | |||
| 201 | 12 | public function withQuery($query): self |
|
| 214 | |||
| 215 | 5 | public function withFragment($fragment): self |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Apply parse_url parts to a URI. |
||
| 231 | * |
||
| 232 | * @param array $parts Array of parse_url parts to apply |
||
| 233 | */ |
||
| 234 | 78 | private function applyParts(array $parts) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Create a URI string from its various parts. |
||
| 250 | * |
||
| 251 | * @param string $scheme |
||
| 252 | * @param string $authority |
||
| 253 | * @param string $path |
||
| 254 | * @param string $query |
||
| 255 | * @param string $fragment |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | 63 | private static function createUriString($scheme, $authority, $path, $query, $fragment): string |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Is a given port non-standard for the current scheme? |
||
| 301 | * |
||
| 302 | * @param string $scheme |
||
| 303 | * @param int $port |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | 11 | private static function isNonStandardPort($scheme, $port): bool |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $scheme |
||
| 314 | * |
||
| 315 | * @throws \InvalidArgumentException If the scheme is invalid |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | 55 | private function filterScheme($scheme): string |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $host |
||
| 330 | * |
||
| 331 | * @throws \InvalidArgumentException If the host is invalid |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | 57 | private function filterHost($host): string |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param int|null $port |
||
| 346 | * |
||
| 347 | * @throws \InvalidArgumentException If the port is invalid |
||
| 348 | * |
||
| 349 | * @return int|null |
||
| 350 | */ |
||
| 351 | 23 | private function filterPort($port) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Filters the path of a URI. |
||
| 367 | * |
||
| 368 | * @param string $path |
||
| 369 | * |
||
| 370 | * @throws \InvalidArgumentException If the path is invalid |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | 80 | View Code Duplication | private function filterPath($path): string |
| 375 | { |
||
| 376 | 80 | if (!is_string($path)) { |
|
| 377 | 1 | throw new \InvalidArgumentException('Path must be a string'); |
|
| 378 | } |
||
| 379 | |||
| 380 | 79 | return preg_replace_callback( |
|
| 381 | 79 | '/(?:[^'.self::$charUnreserved.self::$charSubDelims.'%:@\/]++|%(?![A-Fa-f0-9]{2}))/', |
|
| 382 | 79 | [$this, 'rawurlencodeMatchZero'], |
|
| 383 | 79 | $path |
|
| 384 | ); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Filters the query string or fragment of a URI. |
||
| 389 | * |
||
| 390 | * @param string $str |
||
| 391 | * |
||
| 392 | * @throws \InvalidArgumentException If the query or fragment is invalid |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | 39 | View Code Duplication | private function filterQueryAndFragment($str): string |
| 397 | { |
||
| 398 | 39 | if (!is_string($str)) { |
|
| 399 | 2 | throw new \InvalidArgumentException('Query and fragment must be a string'); |
|
| 400 | } |
||
| 401 | |||
| 402 | 37 | return preg_replace_callback( |
|
| 403 | 37 | '/(?:[^'.self::$charUnreserved.self::$charSubDelims.'%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', |
|
| 404 | 37 | [$this, 'rawurlencodeMatchZero'], |
|
| 405 | 37 | $str |
|
| 406 | ); |
||
| 407 | } |
||
| 408 | |||
| 409 | 6 | private function rawurlencodeMatchZero(array $match): string |
|
| 413 | } |
||
| 414 |