Complex classes like HttpUri 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 HttpUri, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class HttpUri implements UriInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Pattern extracted from Symfony\Component\Validator\Constraints\UrlValidator |
||
| 19 | */ |
||
| 20 | const PATTERN = '~^ |
||
| 21 | (?<scheme>%s):// # protocol |
||
| 22 | (?<userInfo>([\pL\pN-]+:)?([\pL\pN-]+)@)? # basic auth |
||
| 23 | (?<host> |
||
| 24 | ([\pL\pN\pS-\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name |
||
| 25 | | # or |
||
| 26 | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address |
||
| 27 | | # or |
||
| 28 | \[ |
||
| 29 | (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?: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5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::)))) |
||
| 30 | \] # an IPv6 address |
||
| 31 | ) |
||
| 32 | (?<port>:[0-9]+)? # a port (optional) |
||
| 33 | (?<path>(?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%%[0-9A-Fa-f]{2})* )*) # a path |
||
| 34 | (?<query>\? (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a query (optional) |
||
| 35 | (?<fragment>\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%%[0-9A-Fa-f]{2})* )? # a fragment (optional) |
||
| 36 | $~ixu'; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | private $scheme = ''; |
||
| 40 | |||
| 41 | /** @var string */ |
||
| 42 | private $userInfo = ''; |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | private $host = ''; |
||
| 46 | |||
| 47 | /** @var null|int */ |
||
| 48 | private $port = null; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $path = ''; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $query = ''; |
||
| 55 | |||
| 56 | /** @var string */ |
||
| 57 | private $fragment = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Retrieve the scheme component of the URI. |
||
| 61 | * |
||
| 62 | * If no scheme is present, this method MUST return an empty string. |
||
| 63 | * |
||
| 64 | * The value returned MUST be normalized to lowercase, per RFC 3986 |
||
| 65 | * Section 3.1. |
||
| 66 | * |
||
| 67 | * The trailing ":" character is not part of the scheme and MUST NOT be |
||
| 68 | * added. |
||
| 69 | * |
||
| 70 | * @see https://tools.ietf.org/html/rfc3986#section-3.1 |
||
| 71 | * @return string The URI scheme. |
||
| 72 | */ |
||
| 73 | 10 | public function getScheme() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Retrieve the authority component of the URI. |
||
| 80 | * |
||
| 81 | * If no authority information is present, this method MUST return an empty |
||
| 82 | * string. |
||
| 83 | * |
||
| 84 | * The authority syntax of the URI is: |
||
| 85 | * |
||
| 86 | * <pre> |
||
| 87 | * [user-info@]host[:port] |
||
| 88 | * </pre> |
||
| 89 | * |
||
| 90 | * If the port component is not set or is the standard port for the current |
||
| 91 | * scheme, it SHOULD NOT be included. |
||
| 92 | * |
||
| 93 | * @see https://tools.ietf.org/html/rfc3986#section-3.2 |
||
| 94 | * @return string The URI authority, in "[user-info@]host[:port]" format. |
||
| 95 | */ |
||
| 96 | 9 | public function getAuthority() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Retrieve the user information component of the URI. |
||
| 105 | * |
||
| 106 | * If no user information is present, this method MUST return an empty |
||
| 107 | * string. |
||
| 108 | * |
||
| 109 | * If a user is present in the URI, this will return that value; |
||
| 110 | * additionally, if the password is also present, it will be appended to the |
||
| 111 | * user value, with a colon (":") separating the values. |
||
| 112 | * |
||
| 113 | * The trailing "@" character is not part of the user information and MUST |
||
| 114 | * NOT be added. |
||
| 115 | * |
||
| 116 | * @return string The URI user information, in "username[:password]" format. |
||
| 117 | */ |
||
| 118 | 9 | public function getUserInfo() |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Retrieve the host component of the URI. |
||
| 125 | * |
||
| 126 | * If no host is present, this method MUST return an empty string. |
||
| 127 | * |
||
| 128 | * The value returned MUST be normalized to lowercase, per RFC 3986 |
||
| 129 | * Section 3.2.2. |
||
| 130 | * |
||
| 131 | * @see http://tools.ietf.org/html/rfc3986#section-3.2.2 |
||
| 132 | * @return string The URI host. |
||
| 133 | */ |
||
| 134 | 10 | public function getHost() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Retrieve the port component of the URI. |
||
| 141 | * |
||
| 142 | * If a port is present, and it is non-standard for the current scheme, |
||
| 143 | * this method MUST return it as an integer. If the port is the standard port |
||
| 144 | * used with the current scheme, this method SHOULD return null. |
||
| 145 | * |
||
| 146 | * If no port is present, and no scheme is present, this method MUST return |
||
| 147 | * a null value. |
||
| 148 | * |
||
| 149 | * If no port is present, but a scheme is present, this method MAY return |
||
| 150 | * the standard port for that scheme, but SHOULD return null. |
||
| 151 | * |
||
| 152 | * @return null|int The URI port. |
||
| 153 | */ |
||
| 154 | 9 | public function getPort() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Retrieve the path component of the URI. |
||
| 161 | * |
||
| 162 | * The path can either be empty or absolute (starting with a slash) or |
||
| 163 | * rootless (not starting with a slash). Implementations MUST support all |
||
| 164 | * three syntaxes. |
||
| 165 | * |
||
| 166 | * Normally, the empty path "" and absolute path "/" are considered equal as |
||
| 167 | * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically |
||
| 168 | * do this normalization because in contexts with a trimmed base path, e.g. |
||
| 169 | * the front controller, this difference becomes significant. It's the task |
||
| 170 | * of the user to handle both "" and "/". |
||
| 171 | * |
||
| 172 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
| 173 | * any characters. To determine what characters to encode, please refer to |
||
| 174 | * RFC 3986, Sections 2 and 3.3. |
||
| 175 | * |
||
| 176 | * As an example, if the value should include a slash ("/") not intended as |
||
| 177 | * delimiter between path segments, that value MUST be passed in encoded |
||
| 178 | * form (e.g., "%2F") to the instance. |
||
| 179 | * |
||
| 180 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
| 181 | * @see https://tools.ietf.org/html/rfc3986#section-3.3 |
||
| 182 | * @return string The URI path. |
||
| 183 | */ |
||
| 184 | 9 | public function getPath() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Retrieve the query string of the URI. |
||
| 191 | * |
||
| 192 | * If no query string is present, this method MUST return an empty string. |
||
| 193 | * |
||
| 194 | * The leading "?" character is not part of the query and MUST NOT be |
||
| 195 | * added. |
||
| 196 | * |
||
| 197 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
| 198 | * any characters. To determine what characters to encode, please refer to |
||
| 199 | * RFC 3986, Sections 2 and 3.4. |
||
| 200 | * |
||
| 201 | * As an example, if a value in a key/value pair of the query string should |
||
| 202 | * include an ampersand ("&") not intended as a delimiter between values, |
||
| 203 | * that value MUST be passed in encoded form (e.g., "%26") to the instance. |
||
| 204 | * |
||
| 205 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
| 206 | * @see https://tools.ietf.org/html/rfc3986#section-3.4 |
||
| 207 | * @return string The URI query string. |
||
| 208 | */ |
||
| 209 | 9 | public function getQuery() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Retrieve the fragment component of the URI. |
||
| 216 | * |
||
| 217 | * If no fragment is present, this method MUST return an empty string. |
||
| 218 | * |
||
| 219 | * The leading "#" character is not part of the fragment and MUST NOT be |
||
| 220 | * added. |
||
| 221 | * |
||
| 222 | * The value returned MUST be percent-encoded, but MUST NOT double-encode |
||
| 223 | * any characters. To determine what characters to encode, please refer to |
||
| 224 | * RFC 3986, Sections 2 and 3.5. |
||
| 225 | * |
||
| 226 | * @see https://tools.ietf.org/html/rfc3986#section-2 |
||
| 227 | * @see https://tools.ietf.org/html/rfc3986#section-3.5 |
||
| 228 | * @return string The URI fragment. |
||
| 229 | */ |
||
| 230 | 9 | public function getFragment() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Return an instance with the specified scheme. |
||
| 237 | * |
||
| 238 | * This method MUST retain the state of the current instance, and return |
||
| 239 | * an instance that contains the specified scheme. |
||
| 240 | * |
||
| 241 | * Implementations MUST support the schemes "http" and "https" case |
||
| 242 | * insensitively, and MAY accommodate other schemes if required. |
||
| 243 | * |
||
| 244 | * An empty scheme is equivalent to removing the scheme. |
||
| 245 | * |
||
| 246 | * @param string $scheme The scheme to use with the new instance. |
||
| 247 | * @return static A new instance with the specified scheme. |
||
|
|
|||
| 248 | * @throws \InvalidArgumentException for invalid or unsupported schemes. |
||
| 249 | */ |
||
| 250 | public function withScheme($scheme) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Return an instance with the specified user information. |
||
| 257 | * |
||
| 258 | * This method MUST retain the state of the current instance, and return |
||
| 259 | * an instance that contains the specified user information. |
||
| 260 | * |
||
| 261 | * Password is optional, but the user information MUST include the |
||
| 262 | * user; an empty string for the user is equivalent to removing user |
||
| 263 | * information. |
||
| 264 | * |
||
| 265 | * @param string $user The user name to use for authority. |
||
| 266 | * @param null|string $password The password associated with $user. |
||
| 267 | * @return static A new instance with the specified user information. |
||
| 268 | */ |
||
| 269 | public function withUserInfo($user, $password = null) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Return an instance with the specified host. |
||
| 276 | * |
||
| 277 | * This method MUST retain the state of the current instance, and return |
||
| 278 | * an instance that contains the specified host. |
||
| 279 | * |
||
| 280 | * An empty host value is equivalent to removing the host. |
||
| 281 | * |
||
| 282 | * @param string $host The hostname to use with the new instance. |
||
| 283 | * @return static A new instance with the specified host. |
||
| 284 | * @throws \InvalidArgumentException for invalid hostnames. |
||
| 285 | */ |
||
| 286 | public function withHost($host) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Return an instance with the specified port. |
||
| 293 | * |
||
| 294 | * This method MUST retain the state of the current instance, and return |
||
| 295 | * an instance that contains the specified port. |
||
| 296 | * |
||
| 297 | * Implementations MUST raise an exception for ports outside the |
||
| 298 | * established TCP and UDP port ranges. |
||
| 299 | * |
||
| 300 | * A null value provided for the port is equivalent to removing the port |
||
| 301 | * information. |
||
| 302 | * |
||
| 303 | * @param null|int $port The port to use with the new instance; a null value |
||
| 304 | * removes the port information. |
||
| 305 | * @return static A new instance with the specified port. |
||
| 306 | * @throws \InvalidArgumentException for invalid ports. |
||
| 307 | */ |
||
| 308 | public function withPort($port) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Return an instance with the specified path. |
||
| 315 | * |
||
| 316 | * This method MUST retain the state of the current instance, and return |
||
| 317 | * an instance that contains the specified path. |
||
| 318 | * |
||
| 319 | * The path can either be empty or absolute (starting with a slash) or |
||
| 320 | * rootless (not starting with a slash). Implementations MUST support all |
||
| 321 | * three syntaxes. |
||
| 322 | * |
||
| 323 | * If the path is intended to be domain-relative rather than path relative then |
||
| 324 | * it must begin with a slash ("/"). Paths not starting with a slash ("/") |
||
| 325 | * are assumed to be relative to some base path known to the application or |
||
| 326 | * consumer. |
||
| 327 | * |
||
| 328 | * Users can provide both encoded and decoded path characters. |
||
| 329 | * Implementations ensure the correct encoding as outlined in getPath(). |
||
| 330 | * |
||
| 331 | * @param string $path The path to use with the new instance. |
||
| 332 | * @return static A new instance with the specified path. |
||
| 333 | * @throws \InvalidArgumentException for invalid paths. |
||
| 334 | */ |
||
| 335 | public function withPath($path) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Return an instance with the specified query string. |
||
| 342 | * |
||
| 343 | * This method MUST retain the state of the current instance, and return |
||
| 344 | * an instance that contains the specified query string. |
||
| 345 | * |
||
| 346 | * Users can provide both encoded and decoded query characters. |
||
| 347 | * Implementations ensure the correct encoding as outlined in getQuery(). |
||
| 348 | * |
||
| 349 | * An empty query string value is equivalent to removing the query string. |
||
| 350 | * |
||
| 351 | * @param string $query The query string to use with the new instance. |
||
| 352 | * @return static A new instance with the specified query string. |
||
| 353 | * @throws \InvalidArgumentException for invalid query strings. |
||
| 354 | */ |
||
| 355 | public function withQuery($query) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Return an instance with the specified URI fragment. |
||
| 362 | * |
||
| 363 | * This method MUST retain the state of the current instance, and return |
||
| 364 | * an instance that contains the specified URI fragment. |
||
| 365 | * |
||
| 366 | * Users can provide both encoded and decoded fragment characters. |
||
| 367 | * Implementations ensure the correct encoding as outlined in getFragment(). |
||
| 368 | * |
||
| 369 | * An empty fragment value is equivalent to removing the fragment. |
||
| 370 | * |
||
| 371 | * @param string $fragment The fragment to use with the new instance. |
||
| 372 | * @return static A new instance with the specified fragment. |
||
| 373 | */ |
||
| 374 | public function withFragment($fragment) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Return the string representation as a URI reference. |
||
| 381 | * |
||
| 382 | * Depending on which components of the URI are present, the resulting |
||
| 383 | * string is either a full URI or relative reference according to RFC 3986, |
||
| 384 | * Section 4.1. The method concatenates the various components of the URI, |
||
| 385 | * using the appropriate delimiters: |
||
| 386 | * |
||
| 387 | * - If a scheme is present, it MUST be suffixed by ":". |
||
| 388 | * - If an authority is present, it MUST be prefixed by "//". |
||
| 389 | * - The path can be concatenated without delimiters. But there are two |
||
| 390 | * cases where the path has to be adjusted to make the URI reference |
||
| 391 | * valid as PHP does not allow to throw an exception in __toString(): |
||
| 392 | * - If the path is rootless and an authority is present, the path MUST |
||
| 393 | * be prefixed by "/". |
||
| 394 | * - If the path is starting with more than one "/" and no authority is |
||
| 395 | * present, the starting slashes MUST be reduced to one. |
||
| 396 | * - If a query is present, it MUST be prefixed by "?". |
||
| 397 | * - If a fragment is present, it MUST be prefixed by "#". |
||
| 398 | * |
||
| 399 | * @see http://tools.ietf.org/html/rfc3986#section-4.1 |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | 9 | public function __toString() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $scheme |
||
| 415 | * @return HttpUri |
||
| 416 | */ |
||
| 417 | 10 | public function setScheme($scheme) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $userInfo |
||
| 426 | * @return HttpUri |
||
| 427 | */ |
||
| 428 | 10 | public function setUserInfo($userInfo) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param string $host |
||
| 437 | * @return HttpUri |
||
| 438 | */ |
||
| 439 | 10 | public function setHost($host) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * @param int|null $port |
||
| 448 | * @return HttpUri |
||
| 449 | */ |
||
| 450 | 10 | public function setPort($port) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @param string $path |
||
| 459 | * @return HttpUri |
||
| 460 | */ |
||
| 461 | 10 | public function setPath($path) |
|
| 467 | |||
| 468 | /** |
||
| 469 | * @param string $query |
||
| 470 | * @return HttpUri |
||
| 471 | */ |
||
| 472 | 10 | public function setQuery($query) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @param string $fragment |
||
| 481 | * @return HttpUri |
||
| 482 | */ |
||
| 483 | 10 | public function setFragment($fragment) |
|
| 489 | |||
| 490 | 12 | public static function parseUri($uri) |
|
| 543 | |||
| 544 | 10 | public static function createFromString($uri) |
|
| 550 | |||
| 551 | 10 | public static function createFromComponents($parts) |
|
| 567 | |||
| 568 | 12 | private static function getDefaultComponents() |
|
| 580 | } |
||
| 581 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.