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 | 72 | public function __construct($url) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Unserialize the string representation of this property |
||
| 102 | * |
||
| 103 | * @param string $str Serialized property |
||
| 104 | * @return SerializablePropertyInterface Property |
||
| 105 | */ |
||
| 106 | 1 | public static function unserialize($str) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Return the serialized URL |
||
| 113 | * |
||
| 114 | * @return string Serialized URL |
||
| 115 | */ |
||
| 116 | 38 | public function __toString() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Return the full serialized URL |
||
| 123 | * |
||
| 124 | * @return string Full URL |
||
| 125 | */ |
||
| 126 | 38 | public function getUrl() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Return the a complete serialized URL |
||
| 133 | * |
||
| 134 | * @param array $override Override components |
||
| 135 | * @return string Serialized URL |
||
| 136 | */ |
||
| 137 | 42 | protected function getUrlInternal(array &$override = []) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Return the URL user |
||
| 212 | * |
||
| 213 | * @return string|NULL URL user |
||
| 214 | */ |
||
| 215 | 14 | public function getUser() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Return the URL password |
||
| 222 | * |
||
| 223 | * @return string|NULL URL password |
||
| 224 | */ |
||
| 225 | 14 | public function getPassword() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Return the URL query parameters as list |
||
| 232 | * |
||
| 233 | * @return array URL query parameters |
||
| 234 | */ |
||
| 235 | 2 | public function getQueryParams() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Set the URL host |
||
| 246 | * |
||
| 247 | * @param string $host URL host |
||
| 248 | * @return Url New URL |
||
| 249 | * @throws InvalidArgumentException If the URL host is invalid |
||
| 250 | */ |
||
| 251 | 2 | public function setHost($host) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Set the URL port |
||
| 270 | * |
||
| 271 | * @param int|null $port URL port |
||
| 272 | * @return Url New URL |
||
| 273 | * @throws InvalidArgumentException If the URL port is invalid |
||
| 274 | */ |
||
| 275 | 2 | public function setPort($port) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Set the URL user |
||
| 292 | * |
||
| 293 | * @param string|NULL $user URL user |
||
| 294 | * @return Url New URL |
||
| 295 | */ |
||
| 296 | 2 | public function setUser($user) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Set the URL password |
||
| 305 | * |
||
| 306 | * @param string $pass URL password |
||
| 307 | * @return Url New URL |
||
| 308 | */ |
||
| 309 | 2 | public function setPassword($pass) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Set the URL query |
||
| 318 | * |
||
| 319 | * @param string $query URL query |
||
| 320 | * @return Url New URL |
||
| 321 | */ |
||
| 322 | 2 | public function setQuery($query) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Set the URL query parameters |
||
| 331 | * |
||
| 332 | * @param array $query URL query parameters |
||
| 333 | * @return Url New URL |
||
| 334 | */ |
||
| 335 | 1 | public function setQueryParams(array $query) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Set the URL fragment |
||
| 344 | * |
||
| 345 | * @param string $fragment URL fragment |
||
| 346 | * @return Url New URL |
||
| 347 | */ |
||
| 348 | 2 | public function setFragment($fragment) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Test whether this URL is remote |
||
| 357 | * |
||
| 358 | * @return bool Remote URL |
||
| 359 | */ |
||
| 360 | 1 | public function isRemote() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Return whether this URL is absolute |
||
| 367 | * |
||
| 368 | * @return bool Absolute URL |
||
| 369 | */ |
||
| 370 | 64 | public function isAbsolute() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Test whether this URL belongs to the local Apparat instance |
||
| 377 | * |
||
| 378 | * @return bool URL belongs to the local Apparat instance |
||
| 379 | */ |
||
| 380 | 30 | public function isAbsoluteLocal() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Set the URL path |
||
| 397 | * |
||
| 398 | * @param string $path URL path |
||
| 399 | * @return Url New URL |
||
| 400 | */ |
||
| 401 | 32 | public function setPath($path) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Set the URL scheme |
||
| 412 | * |
||
| 413 | * @param string $scheme URL scheme |
||
| 414 | * @return Url New URL |
||
| 415 | * @throws InvalidArgumentException If the URL scheme is invalid |
||
| 416 | */ |
||
| 417 | 32 | public function setScheme($scheme) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Test if this URL matches all available parts of a given URL |
||
| 434 | * |
||
| 435 | * @param Url $url Comparison URL |
||
| 436 | * @return bool This URL matches all available parts of the given URL |
||
| 437 | */ |
||
| 438 | 10 | public function matches(Url $url) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Serialize the property |
||
| 494 | * |
||
| 495 | * @return mixed Property serialization |
||
| 496 | */ |
||
| 497 | 2 | public function serialize() |
|
| 501 | } |
||
| 502 |