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 |
||
| 45 | class Url |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * HTTP-Schema |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | const SCHEME_HTTP = 'http'; |
||
| 53 | /** |
||
| 54 | * HTTPS-Schema |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | const SCHEME_HTTPS = 'https'; |
||
| 59 | /** |
||
| 60 | * Valid schemes |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected static $_schemes = [self::SCHEME_HTTP => true, self::SCHEME_HTTPS => true]; |
||
| 65 | /** |
||
| 66 | * URL parts |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $_urlParts = null; |
||
| 71 | |||
| 72 | /******************************************************************************* |
||
| 73 | * PUBLIC METHODS |
||
| 74 | *******************************************************************************/ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * URL constructor |
||
| 78 | * |
||
| 79 | * @param string $url URL |
||
| 80 | * @throws InvalidArgumentException If the URL is invalid |
||
| 81 | */ |
||
| 82 | 23 | public function __construct($url) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Return the serialized URL |
||
| 97 | * |
||
| 98 | * @return string Serialized URL |
||
| 99 | */ |
||
| 100 | 2 | public function __toString() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Return the full serialized URL |
||
| 107 | * |
||
| 108 | * @return string Full URL |
||
| 109 | */ |
||
| 110 | 2 | public function getUrl() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Return the a complete serialized URL |
||
| 117 | * |
||
| 118 | * @param array $override Override components |
||
| 119 | * @return string Serialized URL |
||
| 120 | */ |
||
| 121 | 3 | protected function _getUrl(array &$override = []) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Return the URL scheme |
||
| 204 | * |
||
| 205 | * @return string URL scheme |
||
| 206 | */ |
||
| 207 | 7 | public function getScheme() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Return the URL user |
||
| 214 | * |
||
| 215 | * @return string|NULL URL user |
||
| 216 | */ |
||
| 217 | 6 | public function getUser() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Return the URL password |
||
| 224 | * |
||
| 225 | * @return string|NULL URL password |
||
| 226 | */ |
||
| 227 | 6 | public function getPassword() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Return the URL host |
||
| 234 | * |
||
| 235 | * @return string URL host |
||
| 236 | */ |
||
| 237 | 7 | public function getHost() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Return the URL port |
||
| 244 | * |
||
| 245 | * @return int URL port |
||
| 246 | */ |
||
| 247 | 6 | public function getPort() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Return the URL fragment |
||
| 254 | * |
||
| 255 | * @return string URL fragment |
||
| 256 | */ |
||
| 257 | 6 | public function getFragment() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Set the URL host |
||
| 264 | * |
||
| 265 | * @param string $host URL host |
||
| 266 | * @return Url New URL |
||
| 267 | * @throws InvalidArgumentException If the URL host is invalid |
||
| 268 | */ |
||
| 269 | 1 | public function setHost($host) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Set the URL port |
||
| 290 | * |
||
| 291 | * @param int $port URL port |
||
| 292 | * @return Url New URL |
||
| 293 | * @throws InvalidArgumentException If the URL port is invalid |
||
| 294 | */ |
||
| 295 | 1 | public function setPort($port) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Set the URL user |
||
| 312 | * |
||
| 313 | * @param string|NULL $user URL user |
||
| 314 | * @return Url New URL |
||
| 315 | */ |
||
| 316 | 1 | public function setUser($user) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Set the URL password |
||
| 325 | * |
||
| 326 | * @param string $pass URL password |
||
| 327 | * @return Url New URL |
||
| 328 | */ |
||
| 329 | 1 | public function setPassword($pass) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Set the URL query |
||
| 338 | * |
||
| 339 | * @param array $query URL query |
||
| 340 | * @return Url New URL |
||
| 341 | */ |
||
| 342 | 1 | public function setQuery(array $query) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Set the URL fragment |
||
| 351 | * |
||
| 352 | * @param string $fragment URL fragment |
||
| 353 | * @return Url New URL |
||
| 354 | */ |
||
| 355 | 1 | public function setFragment($fragment) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Test whether this URL is remote |
||
| 364 | * |
||
| 365 | * @return bool Remote URL |
||
| 366 | */ |
||
| 367 | public function isRemote() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Return whether this URL is absolute |
||
| 374 | * |
||
| 375 | * @return bool Absolute URL |
||
| 376 | */ |
||
| 377 | 21 | public function isAbsolute() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Test whether this URL belongs to the local Apparat instance |
||
| 384 | * |
||
| 385 | * @return bool URL belongs to the local Apparat instance |
||
| 386 | */ |
||
| 387 | 4 | public function isAbsoluteLocal() |
|
| 388 | { |
||
| 389 | // Instantiate the apparat base URL |
||
| 390 | 4 | $apparatBaseUrl = new self(getenv('APPARAT_BASE_URL')); |
|
| 391 | 4 | $apparatBaseUrlPath = $apparatBaseUrl->getPath(); |
|
| 392 | 4 | $apparatBaseUrl = $apparatBaseUrl->setScheme(null)->setPath(null); |
|
| 393 | |||
| 394 | // If the URL matches the Apparat base URL (the scheme is disregarded) |
||
| 395 | 4 | return $this->isAbsolute() && $this->matches($apparatBaseUrl) && !strncmp( |
|
| 396 | 2 | $apparatBaseUrlPath, |
|
| 397 | 2 | $this->getPath(), strlen($apparatBaseUrlPath) |
|
| 398 | 4 | ); |
|
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Return the URL path |
||
| 403 | * |
||
| 404 | * @return string URL path |
||
| 405 | */ |
||
| 406 | 12 | public function getPath() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Set the URL path |
||
| 413 | * |
||
| 414 | * @param string $path URL path |
||
| 415 | * @return Url New URL |
||
| 416 | */ |
||
| 417 | 5 | public function setPath($path) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Set the URL scheme |
||
| 428 | * |
||
| 429 | * @param string $scheme URL scheme |
||
| 430 | * @return Url New URL |
||
| 431 | * @throws InvalidArgumentException If the URL scheme is invalid |
||
| 432 | */ |
||
| 433 | 5 | public function setScheme($scheme) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Test if this URL matches all available parts of a given URL |
||
| 450 | * |
||
| 451 | * @param Url $url Comparison URL |
||
| 452 | * @return bool This URL matches all available parts of the given URL |
||
| 453 | */ |
||
| 454 | 4 | public function matches(Url $url) |
|
| 513 | |||
| 514 | /******************************************************************************* |
||
| 515 | * PRIVATE METHODS |
||
| 516 | *******************************************************************************/ |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Return the URL query |
||
| 520 | * |
||
| 521 | * @return array URL query |
||
| 522 | */ |
||
| 523 | 6 | public function getQuery() |
|
| 531 | } |
||
| 532 |