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 | 59 | 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 | * @return Url |
||
| 87 | */ |
||
| 88 | 3 | public static function parse($url) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Extracts urls from a string of text. |
||
| 95 | * |
||
| 96 | * @param string $string |
||
| 97 | * @return array $urls |
||
| 98 | */ |
||
| 99 | 1 | public static function extract($string) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Creates an Url instance based on data available on $_SERVER variable. |
||
| 114 | * |
||
| 115 | * @return Url |
||
| 116 | */ |
||
| 117 | 1 | public static function fromCurrent() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Gets the ParserInterface instance used to parse this Url instance. |
||
| 165 | * |
||
| 166 | * @return ParserInterface |
||
| 167 | */ |
||
| 168 | 59 | public function getParser() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Sets the ParserInterface instance to use to parse this Url instance. |
||
| 179 | * |
||
| 180 | * @param ParserInterface $parser |
||
| 181 | */ |
||
| 182 | 1 | public function setParser(ParserInterface $parser) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Join this Url instance together with another Url instance or a string url. |
||
| 189 | * |
||
| 190 | * @param Url|string $url |
||
| 191 | * @return Url |
||
| 192 | */ |
||
| 193 | 1 | public function join($url) |
|
| 210 | |||
| 211 | /** @noinspection PhpMissingParentCallCommonInspection */ |
||
| 212 | /** |
||
| 213 | * @inheritDoc |
||
| 214 | * @override |
||
| 215 | */ |
||
| 216 | 8 | public function set($key, $value) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param $string |
||
| 226 | */ |
||
| 227 | 1 | public function setPathString($string) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Set the Path instance. |
||
| 234 | * |
||
| 235 | * @param Path $path |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | 1 | public function setPath(Path $path) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Get the Path instance. |
||
| 248 | * |
||
| 249 | * @return Path |
||
| 250 | */ |
||
| 251 | public function getPath() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param $string |
||
| 259 | */ |
||
| 260 | 1 | public function setQueryString($string) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Set the Query instance. |
||
| 267 | * |
||
| 268 | * @param Query $query |
||
| 269 | * |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | 2 | public function setQuery(Query $query) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Get the Query instance. |
||
| 281 | * |
||
| 282 | * @return Query |
||
| 283 | */ |
||
| 284 | public function getQuery() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $string |
||
| 292 | */ |
||
| 293 | 1 | public function setFragmentString($string) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Set the Fragment instance. |
||
| 300 | * |
||
| 301 | * @param Fragment $fragment |
||
| 302 | * |
||
| 303 | * @return $this |
||
| 304 | */ |
||
| 305 | 2 | public function setFragment(Fragment $fragment) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Get the Fragment instance. |
||
| 314 | * |
||
| 315 | * @return Fragment |
||
| 316 | */ |
||
| 317 | public function getFragment() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Gets the netloc part of the Url. It is the user, pass, host and port returned as a string. |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | 1 | public function getNetloc() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Builds a string url from this Url instance internal data and returns it. |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | 17 | public function getUrl() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Set the string url for this Url instance and sets initialized to false. |
||
| 360 | * |
||
| 361 | * @param string |
||
| 362 | */ |
||
| 363 | 2 | public function setUrl($url) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Checks if the Url instance is absolute or not. |
||
| 372 | * |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | 1 | public function isAbsolute() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @inheritDoc |
||
| 383 | */ |
||
| 384 | 14 | public function __toString() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @inheritDoc |
||
| 391 | */ |
||
| 392 | 58 | protected function doInitialize() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Reconstructs a string URL from an array of parts. |
||
| 409 | * |
||
| 410 | * @param array $parts |
||
| 411 | * @return string $url |
||
| 412 | */ |
||
| 413 | 17 | private static function httpBuildUrl(array $parts) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Creates the default Parser instance to parse urls. |
||
| 430 | * |
||
| 431 | * @return Parser |
||
| 432 | */ |
||
| 433 | 58 | private static function createDefaultParser() |
|
| 440 | } |
||
| 441 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: