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 | 34 | public function __construct($url) |
|
83 | { |
||
84 | |||
85 | // Parse the URL |
||
86 | 34 | $this->urlParts = @parse_url($url); |
|
87 | 34 | if ($this->urlParts === false) { |
|
88 | 1 | throw new InvalidArgumentException( |
|
89 | 1 | sprintf('Invalid URL "%s"', $url), |
|
90 | InvalidArgumentException::INVALID_URL |
||
91 | 1 | ); |
|
92 | } |
||
93 | 33 | } |
|
94 | |||
95 | /** |
||
96 | * Return the serialized URL |
||
97 | * |
||
98 | * @return string Serialized URL |
||
99 | */ |
||
100 | 8 | public function __toString() |
|
104 | |||
105 | /** |
||
106 | * Return the full serialized URL |
||
107 | * |
||
108 | * @return string Full URL |
||
109 | */ |
||
110 | 8 | 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 | 11 | protected function getUrlInternal(array &$override = []) |
|
122 | { |
||
123 | // Prepare the URL scheme |
||
124 | 11 | if (isset($override['scheme'])) { |
|
125 | 1 | $scheme = trim($override['scheme']); |
|
126 | 1 | if (strlen($scheme)) { |
|
127 | 1 | $scheme .= '://'; |
|
128 | 1 | } |
|
129 | 1 | } else { |
|
130 | 10 | $scheme = !empty($this->urlParts['scheme']) ? $this->getScheme().'://' : ''; |
|
131 | } |
||
132 | 11 | $override['scheme'] = $scheme; |
|
133 | |||
134 | // Prepare the URL user |
||
135 | 11 | if (isset($override['user'])) { |
|
136 | 1 | $user = $override['user']; |
|
137 | 1 | } else { |
|
138 | 10 | $user = !empty($this->urlParts['user']) ? rawurlencode($this->getUser()) : ''; |
|
139 | } |
||
140 | 11 | $override['user'] = $user; |
|
141 | |||
142 | // Prepare the URL password |
||
143 | 11 | if (isset($override['pass'])) { |
|
144 | 1 | $pass = ':'.$override['pass']; |
|
145 | 1 | } else { |
|
146 | 10 | $pass = !empty($this->urlParts['pass']) ? ':'.rawurlencode($this->getPassword()) : ''; |
|
147 | } |
||
148 | 11 | if ($user || $pass) { |
|
149 | 5 | $pass .= '@'; |
|
150 | 5 | } |
|
151 | 11 | $override['pass'] = $pass; |
|
152 | |||
153 | // Prepare the URL host |
||
154 | 11 | if (isset($override['host'])) { |
|
155 | 1 | $host = $override['host']; |
|
156 | 1 | } else { |
|
157 | 10 | $host = !empty($this->urlParts['host']) ? $this->getHost() : ''; |
|
158 | } |
||
159 | 11 | $override['host'] = $host; |
|
160 | |||
161 | // Prepare the URL port |
||
162 | 11 | if (isset($override['port'])) { |
|
163 | 1 | $port = ':'.$override['port']; |
|
164 | 1 | } else { |
|
165 | 10 | $port = !empty($this->urlParts['port']) ? ':'.$this->getPort() : ''; |
|
166 | } |
||
167 | 11 | $override['port'] = $port; |
|
168 | |||
169 | // Prepare the URL path |
||
170 | 11 | if (isset($override['path'])) { |
|
171 | 1 | $path = $override['path']; |
|
172 | 1 | } else { |
|
173 | 10 | $path = empty($this->urlParts['path']) ? '' : $this->urlParts['path']; |
|
174 | } |
||
175 | 11 | $override['path'] = $path; |
|
176 | |||
177 | // Prepare the URL query |
||
178 | 11 | if (isset($override['query'])) { |
|
179 | 4 | $query = (is_array($override['query']) ? http_build_query($override['query']) : strval($override['query'])); |
|
180 | 4 | if (strlen($query)) { |
|
181 | 1 | $query = '?'.$query; |
|
182 | 1 | } |
|
183 | 4 | } else { |
|
184 | 8 | $query = !empty($this->urlParts['query']) ? '?'.$this->urlParts['query'] : ''; |
|
185 | } |
||
186 | 11 | $override['query'] = $query; |
|
187 | |||
188 | // Prepare the URL fragment |
||
189 | 11 | if (isset($override['fragment'])) { |
|
190 | 4 | $fragment = $override['fragment']; |
|
191 | 4 | if (strlen($fragment)) { |
|
192 | 1 | $fragment = '#'.$fragment; |
|
193 | 1 | } |
|
194 | 4 | } else { |
|
195 | 8 | $fragment = !empty($this->urlParts['fragment']) ? '#'.$this->getFragment() : ''; |
|
196 | } |
||
197 | 11 | $override['fragment'] = $fragment; |
|
198 | |||
199 | 11 | return "$scheme$user$pass$host$port$path$query$fragment"; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Return the URL scheme |
||
204 | * |
||
205 | * @return string URL scheme |
||
206 | */ |
||
207 | 14 | public function getScheme() |
|
211 | |||
212 | /** |
||
213 | * Return the URL user |
||
214 | * |
||
215 | * @return string|NULL URL user |
||
216 | */ |
||
217 | 10 | public function getUser() |
|
221 | |||
222 | /** |
||
223 | * Return the URL password |
||
224 | * |
||
225 | * @return string|NULL URL password |
||
226 | */ |
||
227 | 10 | public function getPassword() |
|
231 | |||
232 | /** |
||
233 | * Return the URL host |
||
234 | * |
||
235 | * @return string URL host |
||
236 | */ |
||
237 | 14 | public function getHost() |
|
241 | |||
242 | /** |
||
243 | * Return the URL port |
||
244 | * |
||
245 | * @return int URL port |
||
246 | */ |
||
247 | 10 | public function getPort() |
|
251 | |||
252 | /** |
||
253 | * Return the URL fragment |
||
254 | * |
||
255 | * @return string URL fragment |
||
256 | */ |
||
257 | 7 | 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 | 1 | public function isRemote() |
|
368 | { |
||
369 | 1 | return $this->isAbsolute() && !$this->isAbsoluteLocal(); |
|
370 | } |
||
371 | |||
372 | /** |
||
373 | * Return whether this URL is absolute |
||
374 | * |
||
375 | * @return bool Absolute URL |
||
376 | */ |
||
377 | 31 | 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 | 12 | public function isAbsoluteLocal() |
|
388 | { |
||
389 | // Instantiate the apparat base URL |
||
390 | 12 | $apparatBaseUrl = new self(getenv('APPARAT_BASE_URL')); |
|
391 | 12 | $apparatBaseUrlPath = $apparatBaseUrl->getPath(); |
|
392 | 12 | $apparatBaseUrl = $apparatBaseUrl->setScheme(null)->setPath(null); |
|
393 | |||
394 | // If the URL matches the Apparat base URL (the scheme is disregarded) |
||
395 | 12 | return $this->isAbsolute() && $this->matches($apparatBaseUrl) && !strncmp( |
|
396 | 3 | $apparatBaseUrlPath, |
|
397 | 3 | $this->getPath(), strlen($apparatBaseUrlPath) |
|
398 | 12 | ); |
|
399 | } |
||
400 | |||
401 | /** |
||
402 | * Return the URL path |
||
403 | * |
||
404 | * @return string URL path |
||
405 | */ |
||
406 | 20 | public function getPath() |
|
410 | |||
411 | /** |
||
412 | * Set the URL path |
||
413 | * |
||
414 | * @param string $path URL path |
||
415 | * @return Url New URL |
||
416 | */ |
||
417 | 13 | 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 | 13 | public function setScheme($scheme) |
|
434 | { |
||
435 | // If the URL scheme is not valid |
||
436 | 13 | if (strlen($scheme) && !array_key_exists($scheme, static::$schemes)) { |
|
437 | 1 | throw new InvalidArgumentException( |
|
438 | 1 | sprintf('Invalid URL scheme "%s"', $scheme), |
|
439 | InvalidArgumentException::INVALID_URL_SCHEME |
||
440 | 1 | ); |
|
441 | } |
||
442 | |||
443 | 13 | $url = clone $this; |
|
444 | 13 | $url->urlParts['scheme'] = $scheme; |
|
445 | 13 | return $url; |
|
446 | } |
||
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 | 8 | public function matches(Url $url) |
|
507 | |||
508 | /******************************************************************************* |
||
509 | * PRIVATE METHODS |
||
510 | *******************************************************************************/ |
||
511 | |||
512 | /** |
||
513 | * Return the URL query |
||
514 | * |
||
515 | * @return array URL query |
||
516 | */ |
||
517 | 7 | public function getQuery() |
|
525 | } |
||
526 |
If you suppress an error, we recommend checking for the error condition explicitly: