Complex classes like Url 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 Url, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class Url extends Uri implements UriInterface, SerializablePropertyInterface |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * Use PSR-7 method |
||
| 53 | */ |
||
| 54 | use Psr7Trait; |
||
| 55 | /** |
||
| 56 | * HTTP scheme |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | const SCHEME_HTTP = 'http'; |
||
| 61 | /** |
||
| 62 | * HTTPS schema |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | const SCHEME_HTTPS = 'https'; |
||
| 67 | /** |
||
| 68 | * Valid schemes |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected static $schemes = [self::SCHEME_HTTP => true, self::SCHEME_HTTPS => true]; |
||
| 73 | /** |
||
| 74 | * URL parts |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $urlParts = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * URL constructor |
||
| 82 | * |
||
| 83 | * @param string $url URL |
||
| 84 | * @throws InvalidArgumentException If the URL is invalid |
||
| 85 | */ |
||
| 86 | 63 | public function __construct($url) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Return the serialized URL |
||
| 102 | * |
||
| 103 | * @return string Serialized URL |
||
| 104 | */ |
||
| 105 | 31 | public function __toString() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Return the full serialized URL |
||
| 112 | * |
||
| 113 | * @return string Full URL |
||
| 114 | */ |
||
| 115 | 31 | public function getUrl() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Return the a complete serialized URL |
||
| 122 | * |
||
| 123 | * @param array $override Override components |
||
| 124 | * @return string Serialized URL |
||
| 125 | */ |
||
| 126 | 34 | protected function getUrlInternal(array &$override = []) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Return the URL user |
||
| 201 | * |
||
| 202 | * @return string|NULL URL user |
||
| 203 | */ |
||
| 204 | 12 | public function getUser() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Return the URL password |
||
| 211 | * |
||
| 212 | * @return string|NULL URL password |
||
| 213 | */ |
||
| 214 | 12 | public function getPassword() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Return the URL query parameters as list |
||
| 221 | * |
||
| 222 | * @return array URL query parameters |
||
| 223 | */ |
||
| 224 | 2 | public function getQueryParams() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Set the URL host |
||
| 235 | * |
||
| 236 | * @param string $host URL host |
||
| 237 | * @return Url New URL |
||
| 238 | * @throws InvalidArgumentException If the URL host is invalid |
||
| 239 | */ |
||
| 240 | 2 | public function setHost($host) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Set the URL port |
||
| 259 | * |
||
| 260 | * @param int|null $port URL port |
||
| 261 | * @return Url New URL |
||
| 262 | * @throws InvalidArgumentException If the URL port is invalid |
||
| 263 | */ |
||
| 264 | 2 | public function setPort($port) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Set the URL user |
||
| 281 | * |
||
| 282 | * @param string|NULL $user URL user |
||
| 283 | * @return Url New URL |
||
| 284 | */ |
||
| 285 | 2 | public function setUser($user) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Set the URL password |
||
| 294 | * |
||
| 295 | * @param string $pass URL password |
||
| 296 | * @return Url New URL |
||
| 297 | */ |
||
| 298 | 2 | public function setPassword($pass) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Set the URL query |
||
| 307 | * |
||
| 308 | * @param string $query URL query |
||
| 309 | * @return Url New URL |
||
| 310 | */ |
||
| 311 | 2 | public function setQuery($query) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Set the URL query parameters |
||
| 320 | * |
||
| 321 | * @param array $query URL query parameters |
||
| 322 | * @return Url New URL |
||
| 323 | */ |
||
| 324 | 1 | public function setQueryParams(array $query) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Set the URL fragment |
||
| 333 | * |
||
| 334 | * @param string $fragment URL fragment |
||
| 335 | * @return Url New URL |
||
| 336 | */ |
||
| 337 | 2 | public function setFragment($fragment) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Test whether this URL is remote |
||
| 346 | * |
||
| 347 | * @return bool Remote URL |
||
| 348 | */ |
||
| 349 | 1 | public function isRemote() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Return whether this URL is absolute |
||
| 356 | * |
||
| 357 | * @return bool Absolute URL |
||
| 358 | */ |
||
| 359 | 55 | public function isAbsolute() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Test whether this URL belongs to the local Apparat instance |
||
| 366 | * |
||
| 367 | * @return bool URL belongs to the local Apparat instance |
||
| 368 | */ |
||
| 369 | 25 | public function isAbsoluteLocal() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Set the URL path |
||
| 386 | * |
||
| 387 | * @param string $path URL path |
||
| 388 | * @return Url New URL |
||
| 389 | */ |
||
| 390 | 27 | public function setPath($path) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Set the URL scheme |
||
| 401 | * |
||
| 402 | * @param string $scheme URL scheme |
||
| 403 | * @return Url New URL |
||
| 404 | * @throws InvalidArgumentException If the URL scheme is invalid |
||
| 405 | */ |
||
| 406 | 27 | public function setScheme($scheme) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Test if this URL matches all available parts of a given URL |
||
| 423 | * |
||
| 424 | * @param Url $url Comparison URL |
||
| 425 | * @return bool This URL matches all available parts of the given URL |
||
| 426 | */ |
||
| 427 | 8 | public function matches(Url $url) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Unserialize the string representation of this property |
||
| 483 | * |
||
| 484 | * @param string $str Serialized property |
||
| 485 | * @return SerializablePropertyInterface Property |
||
| 486 | */ |
||
| 487 | 1 | public static function unserialize($str) { |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Serialize the property |
||
| 493 | * |
||
| 494 | * @return mixed Property serialization |
||
| 495 | */ |
||
| 496 | 1 | public function serialize() { |
|
| 499 | } |
||
| 500 |