Complex classes like Uri 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 Uri, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Uri implements UriInterface |
||
24 | { |
||
25 | /** |
||
26 | * The URI scheme (without "://" suffix) |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $scheme = ''; |
||
31 | |||
32 | /** |
||
33 | * The user info encoded in the url (for URLS like http://user@password:example.com |
||
34 | * |
||
35 | * @var string Either empty or encoded in form username[:password] |
||
36 | */ |
||
37 | protected $userInfo = ''; |
||
38 | |||
39 | /** |
||
40 | * The URI host |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $host = ''; |
||
45 | |||
46 | /** |
||
47 | * The URI port number |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $port; |
||
52 | |||
53 | /** |
||
54 | * The URI path |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $path = ''; |
||
59 | |||
60 | /** |
||
61 | * The URI query string (without "?" prefix) |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $query = ''; |
||
66 | |||
67 | /** |
||
68 | * The URI fragment string (without "#" prefix) |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $fragment = ''; |
||
73 | |||
74 | /** |
||
75 | * Create a new URI |
||
76 | * |
||
77 | * @param string $scheme URI scheme |
||
78 | * @param string $host URI host |
||
79 | * @param int|null $port URI port number |
||
80 | * @param string $path URI path |
||
81 | * @param string $query URI query string |
||
82 | * @param string $fragment |
||
83 | * @param string $userInfo (optional) username & password encoded in URI |
||
84 | */ |
||
85 | public function __construct($scheme, $host, $port = null, $path = '/', $query = '', $fragment = '', $userInfo = '') |
||
100 | |||
101 | /** |
||
102 | * Create new Uri from string |
||
103 | * |
||
104 | * Substantially based on Slim Framework's URI implementation. |
||
105 | * |
||
106 | * @param string $uri Complete Uri string (i.e., https://user:pass@host:443/path?query) |
||
107 | * @link https://github.com/slimphp/Slim/ |
||
108 | * @throws \InvalidArgumentException |
||
109 | * @return self |
||
110 | */ |
||
111 | public static function createFromString($uri) |
||
147 | |||
148 | /** |
||
149 | * Create new URI from the provided environment |
||
150 | * |
||
151 | * Substantially based on Slim Framework's URI implementation. |
||
152 | * |
||
153 | * @param Bag $environment |
||
154 | * @link https://github.com/slimphp/Slim/ |
||
155 | * @return self |
||
156 | */ |
||
157 | public static function createFromEnvironment(Bag $environment) |
||
188 | |||
189 | /** |
||
190 | * Build user info string from username/password components. |
||
191 | * |
||
192 | * @param string $username |
||
193 | * @param string $password |
||
194 | * @return string |
||
195 | */ |
||
196 | protected static function buildUserInfo($username, $password) |
||
209 | |||
210 | /** |
||
211 | * Retrieve the URI scheme. |
||
212 | * |
||
213 | * Implementations SHOULD restrict values to "http", "https", or an empty |
||
214 | * string but MAY accommodate other schemes if required. |
||
215 | * |
||
216 | * If no scheme is present, this method MUST return an empty string. |
||
217 | * |
||
218 | * The string returned MUST omit the trailing "://" delimiter if present. |
||
219 | * |
||
220 | * @return string The scheme of the URI. |
||
221 | */ |
||
222 | public function getScheme() |
||
226 | |||
227 | /** |
||
228 | * Retrieve the authority portion of the URI. |
||
229 | * |
||
230 | * The authority portion of the URI is: |
||
231 | * |
||
232 | * <pre> |
||
233 | * [user-info@]host[:port] |
||
234 | * </pre> |
||
235 | * |
||
236 | * If the port component is not set or is the standard port for the current |
||
237 | * scheme, it SHOULD NOT be included. |
||
238 | * |
||
239 | * This method MUST return an empty string if no authority information is |
||
240 | * present. |
||
241 | * |
||
242 | * @return string Authority portion of the URI, in "[user-info@]host[:port]" |
||
243 | * format. |
||
244 | */ |
||
245 | public function getAuthority() |
||
254 | |||
255 | /** |
||
256 | * Retrieve the user information portion of the URI, if present. |
||
257 | * |
||
258 | * If a user is present in the URI, this will return that value; |
||
259 | * additionally, if the password is also present, it will be appended to the |
||
260 | * user value, with a colon (":") separating the values. |
||
261 | * |
||
262 | * Implementations MUST NOT return the "@" suffix when returning this value. |
||
263 | * |
||
264 | * @return string User information portion of the URI, if present, in |
||
265 | * "username[:password]" format. |
||
266 | */ |
||
267 | public function getUserInfo() |
||
271 | |||
272 | /** |
||
273 | * Retrieve the host segment of the URI. |
||
274 | * |
||
275 | * This method MUST return a string; if no host segment is present, an |
||
276 | * empty string MUST be returned. |
||
277 | * |
||
278 | * @return string Host segment of the URI. |
||
279 | */ |
||
280 | public function getHost() |
||
284 | |||
285 | /** |
||
286 | * Retrieve the port segment of the URI. |
||
287 | * |
||
288 | * If a port is present, and it is non-standard for the current scheme, |
||
289 | * this method MUST return it as an integer. If the port is the standard port |
||
290 | * used with the current scheme, this method SHOULD return null. |
||
291 | * |
||
292 | * If no port is present, and no scheme is present, this method MUST return |
||
293 | * a null value. |
||
294 | * |
||
295 | * If no port is present, but a scheme is present, this method MAY return |
||
296 | * the standard port for that scheme, but SHOULD return null. |
||
297 | * |
||
298 | * @return null|int The port for the URI. |
||
299 | */ |
||
300 | public function getPort() |
||
304 | |||
305 | /** |
||
306 | * Retrieve the path segment of the URI. |
||
307 | * |
||
308 | * This method MUST return a string; if no path is present it MUST return |
||
309 | * the string "/". |
||
310 | * |
||
311 | * @return string The path segment of the URI. |
||
312 | */ |
||
313 | public function getPath() |
||
317 | |||
318 | /** |
||
319 | * Retrieve the query string of the URI. |
||
320 | * |
||
321 | * This method MUST return a string; if no query string is present, it MUST |
||
322 | * return an empty string. |
||
323 | * |
||
324 | * The string returned MUST omit the leading "?" character. |
||
325 | * |
||
326 | * @return string The URI query string. |
||
327 | */ |
||
328 | public function getQuery() |
||
332 | |||
333 | /** |
||
334 | * Retrieve the fragment segment of the URI. |
||
335 | * |
||
336 | * This method MUST return a string; if no fragment is present, it MUST |
||
337 | * return an empty string. |
||
338 | * |
||
339 | * The string returned MUST omit the leading "#" character. |
||
340 | * |
||
341 | * @return string The URI fragment. |
||
342 | */ |
||
343 | public function getFragment() |
||
347 | |||
348 | /** |
||
349 | * Create a new instance with the specified scheme. |
||
350 | * |
||
351 | * This method MUST retain the state of the current instance, and return |
||
352 | * a new instance that contains the specified scheme. If the scheme |
||
353 | * provided includes the "://" delimiter, it MUST be removed. |
||
354 | * |
||
355 | * Implementations SHOULD restrict values to "http", "https", or an empty |
||
356 | * string but MAY accommodate other schemes if required. |
||
357 | * |
||
358 | * An empty scheme is equivalent to removing the scheme. |
||
359 | * |
||
360 | * @param string $scheme The scheme to use with the new instance. |
||
361 | * @return self A new instance with the specified scheme. |
||
362 | * @throws \InvalidArgumentException for invalid or unsupported schemes. |
||
363 | */ |
||
364 | public function withScheme($scheme) |
||
371 | |||
372 | /** |
||
373 | * Create a new instance with the specified user information. |
||
374 | * |
||
375 | * This method MUST retain the state of the current instance, and return |
||
376 | * a new instance that contains the specified user information. |
||
377 | * |
||
378 | * Password is optional, but the user information MUST include the |
||
379 | * user; an empty string for the user is equivalent to removing user |
||
380 | * information. |
||
381 | * |
||
382 | * @param string $user User name to use for authority. |
||
383 | * @param null|string $password Password associated with $user. |
||
384 | * @return self A new instance with the specified user information. |
||
385 | */ |
||
386 | public function withUserInfo($user, $password = null) |
||
394 | |||
395 | /** |
||
396 | * Create a new instance with the specified host. |
||
397 | * |
||
398 | * This method MUST retain the state of the current instance, and return |
||
399 | * a new instance that contains the specified host. |
||
400 | * |
||
401 | * An empty host value is equivalent to removing the host. |
||
402 | * |
||
403 | * @param string $host Hostname to use with the new instance. |
||
404 | * @return self A new instance with the specified host. |
||
405 | * @throws \InvalidArgumentException for invalid hostnames. |
||
406 | */ |
||
407 | public function withHost($host) |
||
414 | |||
415 | /** |
||
416 | * Create a new instance with the specified port. |
||
417 | * |
||
418 | * This method MUST retain the state of the current instance, and return |
||
419 | * a new instance that contains the specified port. |
||
420 | * |
||
421 | * Implementations MUST raise an exception for ports outside the |
||
422 | * established TCP and UDP port ranges. |
||
423 | * |
||
424 | * A null value provided for the port is equivalent to removing the port |
||
425 | * information. |
||
426 | * |
||
427 | * @param null|int $port Port to use with the new instance; a null value |
||
428 | * removes the port information. |
||
429 | * @return self A new instance with the specified port. |
||
430 | * @throws \InvalidArgumentException for invalid ports. |
||
431 | */ |
||
432 | public function withPort($port) |
||
439 | |||
440 | /** |
||
441 | * Create a new instance with the specified path. |
||
442 | * |
||
443 | * This method MUST retain the state of the current instance, and return |
||
444 | * a new instance that contains the specified path. |
||
445 | * |
||
446 | * The path MUST be prefixed with "/"; if not, the implementation MAY |
||
447 | * provide the prefix itself. |
||
448 | * |
||
449 | * The implementation MUST percent-encode reserved characters as |
||
450 | * specified in RFC 3986, Section 2, but MUST NOT double-encode any |
||
451 | * characters. |
||
452 | * |
||
453 | * An empty path value is equivalent to removing the path. |
||
454 | * |
||
455 | * @param string $path The path to use with the new instance. |
||
456 | * @return self A new instance with the specified path. |
||
457 | * @throws \InvalidArgumentException for invalid paths. |
||
458 | */ |
||
459 | public function withPath($path) |
||
466 | |||
467 | /** |
||
468 | * Create a new instance with the specified query string. |
||
469 | * |
||
470 | * This method MUST retain the state of the current instance, and return |
||
471 | * a new instance that contains the specified query string. |
||
472 | * |
||
473 | * If the query string is prefixed by "?", that character MUST be removed. |
||
474 | * Additionally, the query string SHOULD be parseable by parse_str() in |
||
475 | * order to be valid. |
||
476 | * |
||
477 | * The implementation MUST percent-encode reserved characters as |
||
478 | * specified in RFC 3986, Section 2, but MUST NOT double-encode any |
||
479 | * characters. |
||
480 | * |
||
481 | * An empty query string value is equivalent to removing the query string. |
||
482 | * |
||
483 | * @param string $query The query string to use with the new instance. |
||
484 | * @return self A new instance with the specified query string. |
||
485 | * @throws \InvalidArgumentException for invalid query strings. |
||
486 | */ |
||
487 | public function withQuery($query) |
||
494 | |||
495 | /** |
||
496 | * Create a new instance with the specified URI fragment. |
||
497 | * |
||
498 | * This method MUST retain the state of the current instance, and return |
||
499 | * a new instance that contains the specified URI fragment. |
||
500 | * |
||
501 | * If the fragment is prefixed by "#", that character MUST be removed. |
||
502 | * |
||
503 | * An empty fragment value is equivalent to removing the fragment. |
||
504 | * |
||
505 | * @param string $fragment The URI fragment to use with the new instance. |
||
506 | * @return self A new instance with the specified URI fragment. |
||
507 | */ |
||
508 | public function withFragment($fragment) |
||
515 | |||
516 | /** |
||
517 | * Convert this URI to the string representation. |
||
518 | * |
||
519 | * @return string |
||
520 | */ |
||
521 | public function __toString() |
||
531 | |||
532 | /** |
||
533 | * Does this URI use a standard port? |
||
534 | * |
||
535 | * @param int|null $port |
||
536 | * @param string $scheme |
||
537 | * |
||
538 | * @return bool |
||
539 | */ |
||
540 | protected function isStandardPort($port, $scheme) |
||
545 | } |
||
546 |