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 |
||
| 36 | class Url extends AbstractPart |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var string The original url string. |
||
| 40 | */ |
||
| 41 | private $url; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ParserInterface |
||
| 45 | */ |
||
| 46 | private $parser; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Construct a new Url instance. |
||
| 50 | * |
||
| 51 | * @param string $url |
||
| 52 | * @param ParserInterface $parser |
||
| 53 | */ |
||
| 54 | 61 | public function __construct($url = null, ParserInterface $parser = null) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Static convenience method for creating a new Url instance. |
||
| 84 | * |
||
| 85 | * @param string $url |
||
| 86 | * |
||
| 87 | * @return Url |
||
| 88 | */ |
||
| 89 | 4 | public static function parse($url): self |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Extracts urls from a string of text. |
||
| 96 | * |
||
| 97 | * @param string $string |
||
| 98 | * |
||
| 99 | * @return array $urls |
||
| 100 | */ |
||
| 101 | 1 | public static function extract($string): array |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Creates an Url instance based on data available on $_SERVER variable. |
||
| 116 | * |
||
| 117 | * @return Url |
||
| 118 | */ |
||
| 119 | 1 | public static function fromCurrent(): self |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Gets the ParserInterface instance used to parse this Url instance. |
||
| 171 | * |
||
| 172 | * @return ParserInterface |
||
| 173 | */ |
||
| 174 | 60 | public function getParser(): ParserInterface |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Sets the ParserInterface instance to use to parse this Url instance. |
||
| 185 | * |
||
| 186 | * @param ParserInterface $parser |
||
| 187 | */ |
||
| 188 | 1 | public function setParser(ParserInterface $parser) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Join this Url instance together with another Url instance or a string url. |
||
| 195 | * |
||
| 196 | * @param Url|string $url |
||
| 197 | * |
||
| 198 | * @return Url |
||
| 199 | */ |
||
| 200 | 1 | public function join($url): self |
|
| 217 | |||
| 218 | /** @noinspection PhpMissingParentCallCommonInspection */ |
||
| 219 | /** |
||
| 220 | * @inheritDoc |
||
| 221 | * @override |
||
| 222 | */ |
||
| 223 | 8 | public function set($key, $value) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param $string |
||
| 233 | */ |
||
| 234 | 1 | public function setPathString($string) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Set the Path instance. |
||
| 241 | * |
||
| 242 | * @param Path $path |
||
| 243 | * |
||
| 244 | * @return $this |
||
| 245 | */ |
||
| 246 | 1 | public function setPath(Path $path) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Get the Path instance. |
||
| 255 | * |
||
| 256 | * @return Path |
||
| 257 | */ |
||
| 258 | public function getPath(): Path |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param $string |
||
| 267 | */ |
||
| 268 | 1 | public function setQueryString($string) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Set the Query instance. |
||
| 275 | * |
||
| 276 | * @param Query $query |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | 2 | public function setQuery(Query $query) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Get the Query instance. |
||
| 289 | * |
||
| 290 | * @return Query |
||
| 291 | */ |
||
| 292 | public function getQuery(): Query |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param $string |
||
| 301 | */ |
||
| 302 | 1 | public function setFragmentString($string) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Set the Fragment instance. |
||
| 309 | * |
||
| 310 | * @param Fragment $fragment |
||
| 311 | * |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | 2 | public function setFragment(Fragment $fragment) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get the Fragment instance. |
||
| 323 | * |
||
| 324 | * @return Fragment |
||
| 325 | */ |
||
| 326 | public function getFragment(): Fragment |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Gets the netloc part of the Url. It is the user, pass, host and port returned as a string. |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | 1 | public function getNetloc(): string |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Builds a string url from this Url instance internal data and returns it. |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 18 | public function getUrl(): string |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Set the string url for this Url instance and sets initialized to false. |
||
| 374 | * |
||
| 375 | * @param string |
||
| 376 | */ |
||
| 377 | 2 | public function setUrl($url) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Checks if the Url instance is absolute or not. |
||
| 386 | * |
||
| 387 | * @return boolean |
||
| 388 | */ |
||
| 389 | 19 | public function isAbsolute(): bool |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @inheritDoc |
||
| 398 | */ |
||
| 399 | 15 | public function __toString() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @inheritDoc |
||
| 406 | */ |
||
| 407 | 59 | protected function doInitialize() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Reconstructs a string URL from an array of parts. |
||
| 424 | * |
||
| 425 | * @param array $parts |
||
| 426 | * |
||
| 427 | * @return string $url |
||
| 428 | */ |
||
| 429 | 17 | private static function httpBuildUrl(array $parts): string |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Reconstructs relative part of URL from an array of parts. |
||
| 445 | * |
||
| 446 | * @param array $parts |
||
| 447 | * |
||
| 448 | * @return string $url |
||
| 449 | */ |
||
| 450 | 18 | private static function httpBuildUrlRelative(array $parts): string |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Creates the default Parser instance to parse urls. |
||
| 464 | * |
||
| 465 | * @return Parser |
||
| 466 | */ |
||
| 467 | 59 | private static function createDefaultParser(): Parser |
|
| 474 | } |
||
| 475 |