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 |
||
| 47 | class Url implements UriInterface |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * Use PSR-7 method |
||
| 51 | */ |
||
| 52 | use Psr7Trait; |
||
| 53 | /** |
||
| 54 | * HTTP-Schema |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | const SCHEME_HTTP = 'http'; |
||
| 59 | /** |
||
| 60 | * HTTPS-Schema |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | const SCHEME_HTTPS = 'https'; |
||
| 65 | /** |
||
| 66 | * Valid schemes |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected static $schemes = [self::SCHEME_HTTP => true, self::SCHEME_HTTPS => true]; |
||
| 71 | /** |
||
| 72 | * URL parts |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected $urlParts = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * URL constructor |
||
| 80 | * |
||
| 81 | * @param string $url URL |
||
| 82 | * @throws InvalidArgumentException If the URL is invalid |
||
| 83 | */ |
||
| 84 | 52 | public function __construct($url) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Return the serialized URL |
||
| 99 | * |
||
| 100 | * @return string Serialized URL |
||
| 101 | */ |
||
| 102 | 26 | public function __toString() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Return the full serialized URL |
||
| 109 | * |
||
| 110 | * @return string Full URL |
||
| 111 | */ |
||
| 112 | 26 | public function getUrl() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Return the a complete serialized URL |
||
| 119 | * |
||
| 120 | * @param array $override Override components |
||
| 121 | * @return string Serialized URL |
||
| 122 | */ |
||
| 123 | 29 | protected function getUrlInternal(array &$override = []) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Return the URL user |
||
| 198 | * |
||
| 199 | * @return string|NULL URL user |
||
| 200 | */ |
||
| 201 | 11 | public function getUser() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Return the URL password |
||
| 208 | * |
||
| 209 | * @return string|NULL URL password |
||
| 210 | */ |
||
| 211 | 11 | public function getPassword() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Return the URL query parameters as list |
||
| 218 | * |
||
| 219 | * @return array URL query parameters |
||
| 220 | */ |
||
| 221 | 2 | public function getQueryParams() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Set the URL host |
||
| 232 | * |
||
| 233 | * @param string $host URL host |
||
| 234 | * @return Url New URL |
||
| 235 | * @throws InvalidArgumentException If the URL host is invalid |
||
| 236 | */ |
||
| 237 | 2 | public function setHost($host) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Set the URL port |
||
| 256 | * |
||
| 257 | * @param int|null $port URL port |
||
| 258 | * @return Url New URL |
||
| 259 | * @throws InvalidArgumentException If the URL port is invalid |
||
| 260 | */ |
||
| 261 | 2 | public function setPort($port) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Set the URL user |
||
| 278 | * |
||
| 279 | * @param string|NULL $user URL user |
||
| 280 | * @return Url New URL |
||
| 281 | */ |
||
| 282 | 2 | public function setUser($user) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Set the URL password |
||
| 291 | * |
||
| 292 | * @param string $pass URL password |
||
| 293 | * @return Url New URL |
||
| 294 | */ |
||
| 295 | 2 | public function setPassword($pass) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Set the URL query |
||
| 304 | * |
||
| 305 | * @param array $query URL query |
||
| 306 | * @return Url New URL |
||
| 307 | */ |
||
| 308 | 2 | public function setQuery($query) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Set the URL query parameters |
||
| 317 | * |
||
| 318 | * @param array $query URL query parameters |
||
| 319 | * @return Url New URL |
||
| 320 | */ |
||
| 321 | 1 | public function setQueryParams(array $query) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Set the URL fragment |
||
| 330 | * |
||
| 331 | * @param string $fragment URL fragment |
||
| 332 | * @return Url New URL |
||
| 333 | */ |
||
| 334 | 2 | public function setFragment($fragment) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Test whether this URL is remote |
||
| 343 | * |
||
| 344 | * @return bool Remote URL |
||
| 345 | */ |
||
| 346 | 1 | public function isRemote() |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Return whether this URL is absolute |
||
| 353 | * |
||
| 354 | * @return bool Absolute URL |
||
| 355 | */ |
||
| 356 | 46 | public function isAbsolute() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Test whether this URL belongs to the local Apparat instance |
||
| 363 | * |
||
| 364 | * @return bool URL belongs to the local Apparat instance |
||
| 365 | */ |
||
| 366 | 21 | public function isAbsoluteLocal() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Set the URL path |
||
| 383 | * |
||
| 384 | * @param string $path URL path |
||
| 385 | * @return Url New URL |
||
| 386 | */ |
||
| 387 | 23 | public function setPath($path) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Set the URL scheme |
||
| 398 | * |
||
| 399 | * @param string $scheme URL scheme |
||
| 400 | * @return Url New URL |
||
| 401 | * @throws InvalidArgumentException If the URL scheme is invalid |
||
| 402 | */ |
||
| 403 | 23 | public function setScheme($scheme) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Test if this URL matches all available parts of a given URL |
||
| 420 | * |
||
| 421 | * @param Url $url Comparison URL |
||
| 422 | * @return bool This URL matches all available parts of the given URL |
||
| 423 | */ |
||
| 424 | 8 | public function matches(Url $url) |
|
| 477 | } |
||
| 478 |