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 | * @return Url |
||
87 | */ |
||
88 | 4 | 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() |
|
165 | |||
166 | /** |
||
167 | * Gets the ParserInterface instance used to parse this Url instance. |
||
168 | * |
||
169 | * @return ParserInterface |
||
170 | */ |
||
171 | 60 | public function getParser() |
|
179 | |||
180 | /** |
||
181 | * Sets the ParserInterface instance to use to parse this Url instance. |
||
182 | * |
||
183 | * @param ParserInterface $parser |
||
184 | */ |
||
185 | 1 | public function setParser(ParserInterface $parser) |
|
189 | |||
190 | /** |
||
191 | * Join this Url instance together with another Url instance or a string url. |
||
192 | * |
||
193 | * @param Url|string $url |
||
194 | * @return Url |
||
195 | */ |
||
196 | 1 | public function join($url) |
|
213 | |||
214 | /** @noinspection PhpMissingParentCallCommonInspection */ |
||
215 | /** |
||
216 | * @inheritDoc |
||
217 | * @override |
||
218 | */ |
||
219 | 8 | public function set($key, $value) |
|
226 | |||
227 | /** |
||
228 | * @param $string |
||
229 | */ |
||
230 | 1 | public function setPathString($string) |
|
234 | |||
235 | /** |
||
236 | * Set the Path instance. |
||
237 | * |
||
238 | * @param Path $path |
||
239 | * |
||
240 | * @return $this |
||
241 | */ |
||
242 | 1 | public function setPath(Path $path) |
|
248 | |||
249 | /** |
||
250 | * Get the Path instance. |
||
251 | * |
||
252 | * @return Path |
||
253 | */ |
||
254 | public function getPath() |
||
259 | |||
260 | /** |
||
261 | * @param $string |
||
262 | */ |
||
263 | 1 | public function setQueryString($string) |
|
267 | |||
268 | /** |
||
269 | * Set the Query instance. |
||
270 | * |
||
271 | * @param Query $query |
||
272 | * |
||
273 | * @return $this |
||
274 | */ |
||
275 | 2 | public function setQuery(Query $query) |
|
281 | |||
282 | /** |
||
283 | * Get the Query instance. |
||
284 | * |
||
285 | * @return Query |
||
286 | */ |
||
287 | public function getQuery() |
||
292 | |||
293 | /** |
||
294 | * @param $string |
||
295 | */ |
||
296 | 1 | public function setFragmentString($string) |
|
300 | |||
301 | /** |
||
302 | * Set the Fragment instance. |
||
303 | * |
||
304 | * @param Fragment $fragment |
||
305 | * |
||
306 | * @return $this |
||
307 | */ |
||
308 | 2 | public function setFragment(Fragment $fragment) |
|
314 | |||
315 | /** |
||
316 | * Get the Fragment instance. |
||
317 | * |
||
318 | * @return Fragment |
||
319 | */ |
||
320 | public function getFragment() |
||
325 | |||
326 | /** |
||
327 | * Gets the netloc part of the Url. It is the user, pass, host and port returned as a string. |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | 1 | public function getNetloc() |
|
341 | |||
342 | /** |
||
343 | * Builds a string url from this Url instance internal data and returns it. |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | 18 | public function getUrl() |
|
364 | |||
365 | /** |
||
366 | * Set the string url for this Url instance and sets initialized to false. |
||
367 | * |
||
368 | * @param string |
||
369 | */ |
||
370 | 2 | public function setUrl($url) |
|
376 | |||
377 | /** |
||
378 | * Checks if the Url instance is absolute or not. |
||
379 | * |
||
380 | * @return boolean |
||
381 | */ |
||
382 | 19 | public function isAbsolute() |
|
388 | |||
389 | /** |
||
390 | * @inheritDoc |
||
391 | */ |
||
392 | 15 | public function __toString() |
|
396 | |||
397 | /** |
||
398 | * @inheritDoc |
||
399 | */ |
||
400 | 59 | protected function doInitialize() |
|
414 | |||
415 | /** |
||
416 | * Reconstructs a string URL from an array of parts. |
||
417 | * |
||
418 | * @param array $parts |
||
419 | * @return string $url |
||
420 | */ |
||
421 | 17 | private static function httpBuildUrl(array $parts) |
|
433 | |||
434 | /** |
||
435 | * Reconstructs relative part of URL from an array of parts. |
||
436 | * |
||
437 | * @param array $parts |
||
438 | * @return string $url |
||
439 | */ |
||
440 | 18 | private static function httpBuildUrlRelative(array $parts) |
|
450 | |||
451 | /** |
||
452 | * Creates the default Parser instance to parse urls. |
||
453 | * |
||
454 | * @return Parser |
||
455 | */ |
||
456 | 59 | private static function createDefaultParser() |
|
463 | } |
||
464 |
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: