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 |
||
| 18 | final class Uri implements UriInterface{ |
||
| 19 | |||
| 20 | private const DEFAULT_PORTS = [ |
||
| 21 | 'http' => 80, |
||
| 22 | 'https' => 443, |
||
| 23 | 'ftp' => 21, |
||
| 24 | 'gopher' => 70, |
||
| 25 | 'nntp' => 119, |
||
| 26 | 'news' => 119, |
||
| 27 | 'telnet' => 23, |
||
| 28 | 'tn3270' => 23, |
||
| 29 | 'imap' => 143, |
||
| 30 | 'pop' => 110, |
||
| 31 | 'ldap' => 389, |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $scheme = ''; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $user = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $pass = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $host = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | private $port = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $path = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $query = ''; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $fragment = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Uri constructor. |
||
| 76 | * |
||
| 77 | * @param string|null $uri |
||
| 78 | * |
||
| 79 | * @throws \InvalidArgumentException |
||
| 80 | */ |
||
| 81 | public function __construct(string $uri = null){ |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @inheritdoc |
||
| 97 | */ |
||
| 98 | public function __toString(){ |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Scheme |
||
| 127 | */ |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param mixed $scheme |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | * @throws \InvalidArgumentException |
||
| 134 | */ |
||
| 135 | private function filterScheme($scheme):string{ |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @inheritdoc |
||
| 146 | */ |
||
| 147 | public function getScheme():string{ |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @inheritdoc |
||
| 153 | */ |
||
| 154 | public function withScheme($scheme):UriInterface{ |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Authority |
||
| 172 | */ |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param mixed $user |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | * @throws \InvalidArgumentException |
||
| 179 | */ |
||
| 180 | private function filterUser($user):string{ |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param mixed $pass |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | * @throws \InvalidArgumentException |
||
| 194 | */ |
||
| 195 | private function filterPass($pass):string{ |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritdoc |
||
| 206 | */ |
||
| 207 | public function getAuthority():string{ |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @inheritdoc |
||
| 224 | */ |
||
| 225 | public function getUserInfo():string{ |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @inheritdoc |
||
| 231 | */ |
||
| 232 | public function withUserInfo($user, $password = null):UriInterface{ |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Host |
||
| 254 | */ |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param mixed $host |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | * @throws \InvalidArgumentException |
||
| 261 | */ |
||
| 262 | private function filterHost($host):string{ |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritdoc |
||
| 277 | */ |
||
| 278 | public function getHost():string{ |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @inheritdoc |
||
| 284 | */ |
||
| 285 | public function withHost($host):UriInterface{ |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Port |
||
| 302 | */ |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param mixed $port |
||
| 306 | * |
||
| 307 | * @return int|null |
||
| 308 | * @throws \InvalidArgumentException |
||
| 309 | */ |
||
| 310 | private function filterPort($port):?int{ |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @inheritdoc |
||
| 327 | */ |
||
| 328 | public function getPort():?int{ |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @inheritdoc |
||
| 334 | */ |
||
| 335 | public function withPort($port):UriInterface{ |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Path |
||
| 353 | */ |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param mixed $path |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | * @throws \InvalidArgumentException |
||
| 360 | */ |
||
| 361 | private function filterPath($path):string{ |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @inheritdoc |
||
| 372 | */ |
||
| 373 | public function getPath():string{ |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @inheritdoc |
||
| 379 | */ |
||
| 380 | public function withPath($path):UriInterface{ |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Query |
||
| 397 | */ |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param $query |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | * @throws \InvalidArgumentException |
||
| 404 | */ |
||
| 405 | private function filterQuery($query):string{ |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @inheritdoc |
||
| 416 | */ |
||
| 417 | public function getQuery():string{ |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @inheritdoc |
||
| 423 | */ |
||
| 424 | public function withQuery($query):UriInterface{ |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Fragment |
||
| 441 | */ |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param $fragment |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | private function filterFragment($fragment):string{ |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @inheritdoc |
||
| 454 | */ |
||
| 455 | public function getFragment():string{ |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @inheritdoc |
||
| 461 | */ |
||
| 462 | public function withFragment($fragment):UriInterface{ |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param array $parts |
||
| 479 | * |
||
| 480 | * @return void |
||
| 481 | */ |
||
| 482 | private function parseUriParts(array $parts):void{ |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param string $str |
||
| 498 | * @param bool|null $query |
||
| 499 | * |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | private function replaceChars(string $str, bool $query = null):string{ |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return void |
||
| 519 | */ |
||
| 520 | private function removeDefaultPort():void{ |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @return void |
||
| 530 | */ |
||
| 531 | private function validateState():void{ |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Additional methods |
||
| 560 | */ |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @see \parse_url() |
||
| 564 | * |
||
| 565 | * @param array $parts |
||
| 566 | * |
||
| 567 | * @return \Psr\Http\Message\UriInterface|\chillerlan\HTTP\Psr7\Uri |
||
| 568 | */ |
||
| 569 | public static function fromParts(array $parts):UriInterface{ |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Whether the URI is absolute, i.e. it has a scheme. |
||
| 580 | * |
||
| 581 | * An instance of UriInterface can either be an absolute URI or a relative reference. This method returns true |
||
| 582 | * if it is the former. An absolute URI has a scheme. A relative reference is used to express a URI relative |
||
| 583 | * to another URI, the base URI. Relative references can be divided into several forms: |
||
| 584 | * - network-path references, e.g. '//example.com/path' |
||
| 585 | * - absolute-path references, e.g. '/path' |
||
| 586 | * - relative-path references, e.g. 'subpath' |
||
| 587 | * |
||
| 588 | * @return bool |
||
| 589 | * @see Uri::isNetworkPathReference |
||
| 590 | * @see Uri::isAbsolutePathReference |
||
| 591 | * @see Uri::isRelativePathReference |
||
| 592 | * @link https://tools.ietf.org/html/rfc3986#section-4 |
||
| 593 | */ |
||
| 594 | public function isAbsolute():bool{ |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Whether the URI is a network-path reference. |
||
| 600 | * |
||
| 601 | * A relative reference that begins with two slash characters is termed an network-path reference. |
||
| 602 | * |
||
| 603 | * @return bool |
||
| 604 | * @link https://tools.ietf.org/html/rfc3986#section-4.2 |
||
| 605 | */ |
||
| 606 | public function isNetworkPathReference():bool{ |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Whether the URI is a absolute-path reference. |
||
| 612 | * |
||
| 613 | * A relative reference that begins with a single slash character is termed an absolute-path reference. |
||
| 614 | * |
||
| 615 | * @return bool |
||
| 616 | * @link https://tools.ietf.org/html/rfc3986#section-4.2 |
||
| 617 | */ |
||
| 618 | public function isAbsolutePathReference():bool{ |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Whether the URI is a relative-path reference. |
||
| 624 | * |
||
| 625 | * A relative reference that does not begin with a slash character is termed a relative-path reference. |
||
| 626 | * |
||
| 627 | * @return bool |
||
| 628 | * @link https://tools.ietf.org/html/rfc3986#section-4.2 |
||
| 629 | */ |
||
| 630 | public function isRelativePathReference():bool{ |
||
| 633 | |||
| 634 | /** |
||
| 635 | * removes a specific query string value. |
||
| 636 | * |
||
| 637 | * Any existing query string values that exactly match the provided key are |
||
| 638 | * removed. |
||
| 639 | * |
||
| 640 | * @param string $key Query string key to remove. |
||
| 641 | * |
||
| 642 | * @return \Psr\Http\Message\UriInterface|\chillerlan\HTTP\Psr7\Uri |
||
| 643 | */ |
||
| 644 | public function withoutQueryValue($key):Uri{ |
||
| 660 | |||
| 661 | /** |
||
| 662 | * adds a specific query string value. |
||
| 663 | * |
||
| 664 | * Any existing query string values that exactly match the provided key are |
||
| 665 | * removed and replaced with the given key value pair. |
||
| 666 | * |
||
| 667 | * A value of null will set the query string key without a value, e.g. "key" |
||
| 668 | * instead of "key=value". |
||
| 669 | * |
||
| 670 | * @param string $key Key to set. |
||
| 671 | * @param string|null $value Value to set |
||
| 672 | * |
||
| 673 | * @return \Psr\Http\Message\UriInterface|\chillerlan\HTTP\Psr7\Uri |
||
| 674 | */ |
||
| 675 | public function withQueryValue($key, $value):Uri{ |
||
| 701 | |||
| 702 | } |
||
| 703 |