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 | * @var array |
||
| 50 | */ |
||
| 51 | protected $data = array( |
||
| 52 | 'scheme' => null, |
||
| 53 | 'host' => null, |
||
| 54 | 'port' => null, |
||
| 55 | 'user' => null, |
||
| 56 | 'pass' => null, |
||
| 57 | 'path' => null, |
||
| 58 | 'query' => null, |
||
| 59 | 'fragment' => null, |
||
| 60 | 'publicSuffix' => null, |
||
| 61 | 'registerableDomain' => null, |
||
| 62 | 'subdomain' => null, |
||
| 63 | 'canonical' => null, |
||
| 64 | 'resource' => null |
||
| 65 | ); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $partClassMap = array( |
||
| 71 | 'path' => 'Purl\Path', |
||
| 72 | 'query' => 'Purl\Query', |
||
| 73 | 'fragment' => 'Purl\Fragment' |
||
| 74 | ); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Construct a new Url instance. |
||
| 78 | * |
||
| 79 | * @param string $url |
||
| 80 | * @param ParserInterface $parser |
||
| 81 | */ |
||
| 82 | public function __construct($url = null, ParserInterface $parser = null) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Static convenience method for creating a new Url instance. |
||
| 90 | * |
||
| 91 | * @param string $url |
||
| 92 | * @return Url |
||
| 93 | */ |
||
| 94 | public static function parse($url) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Extracts urls from a string of text. |
||
| 101 | * |
||
| 102 | * @param string $string |
||
| 103 | * @return array $urls |
||
| 104 | */ |
||
| 105 | public static function extract($string) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Creates an Url instance based on data available on $_SERVER variable. |
||
| 120 | * |
||
| 121 | * @return Url |
||
| 122 | */ |
||
| 123 | public static function fromCurrent() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Gets the ParserInterface instance used to parse this Url instance. |
||
| 166 | * |
||
| 167 | * @return ParserInterface |
||
| 168 | */ |
||
| 169 | public function getParser() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Sets the ParserInterface instance to use to parse this Url instance. |
||
| 180 | * |
||
| 181 | * @param ParserInterface $parser |
||
| 182 | */ |
||
| 183 | public function setParser(ParserInterface $parser) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Join this Url instance together with another Url instance or a string url. |
||
| 190 | * |
||
| 191 | * @param Url|string $url |
||
| 192 | * @return Url |
||
| 193 | */ |
||
| 194 | public function join($url) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @inheritDoc |
||
| 218 | * @override |
||
| 219 | */ |
||
| 220 | public function set($key, $value) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Set the Path instance. |
||
| 230 | * |
||
| 231 | * @param Path |
||
| 232 | */ |
||
| 233 | public function setPath(Path $path) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the Path instance. |
||
| 242 | * |
||
| 243 | * @return Path |
||
| 244 | */ |
||
| 245 | public function getPath() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set the Query instance. |
||
| 253 | * |
||
| 254 | * @param Query |
||
| 255 | */ |
||
| 256 | public function setQuery(Query $query) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the Query instance. |
||
| 265 | * |
||
| 266 | * @return Query |
||
| 267 | */ |
||
| 268 | public function getQuery() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set the Fragment instance. |
||
| 276 | * |
||
| 277 | * @param Fragment |
||
| 278 | */ |
||
| 279 | public function setFragment(Fragment $fragment) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the Fragment instance. |
||
| 288 | * |
||
| 289 | * @return Fragment |
||
| 290 | */ |
||
| 291 | public function getFragment() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Gets the netloc part of the Url. It is the user, pass, host and port returned as a string. |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function getNetloc() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Builds a string url from this Url instance internal data and returns it. |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public function getUrl() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set the string url for this Url instance and sets initialized to false. |
||
| 328 | * |
||
| 329 | * @param string |
||
| 330 | */ |
||
| 331 | public function setUrl($url) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Checks if the Url instance is absolute or not. |
||
| 340 | * |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | public function isAbsolute() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @inheritDoc |
||
| 351 | */ |
||
| 352 | public function __toString() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @inheritDoc |
||
| 359 | */ |
||
| 360 | protected function doInitialize() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Reconstructs a string URL from an array of parts. |
||
| 377 | * |
||
| 378 | * @param array $parts |
||
| 379 | * @return string $url |
||
| 380 | */ |
||
| 381 | private static function httpBuildUrl(array $parts) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Reconstructs relative part of URL from an array of parts. |
||
| 396 | * |
||
| 397 | * @param array $parts |
||
| 398 | * @return string $url |
||
| 399 | */ |
||
| 400 | private static function httpBuildRelativeUrl(array $parts) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Creates the default Parser instance to parse urls. |
||
| 413 | * |
||
| 414 | * @return Parser |
||
| 415 | */ |
||
| 416 | private static function createDefaultParser() |
||
| 423 | } |
||
| 424 |