| Total Complexity | 57 |
| Total Lines | 470 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 15 | class Uri implements UriInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string The scheme. |
||
| 19 | */ |
||
| 20 | private $scheme; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string The username. |
||
| 24 | */ |
||
| 25 | private $user; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string The password. |
||
| 29 | */ |
||
| 30 | private $pass; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string The hostname. |
||
| 34 | */ |
||
| 35 | private $host; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int|null The port. |
||
| 39 | */ |
||
| 40 | private $port; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string The path. |
||
| 44 | */ |
||
| 45 | private $path; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string The query. |
||
| 49 | */ |
||
| 50 | private $query; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string The fragment. |
||
| 54 | */ |
||
| 55 | private $fragment; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructs the URI. |
||
| 59 | * |
||
| 60 | * @param string|null $uri The URI string to parse or null to create an empty URI. |
||
| 61 | */ |
||
| 62 | public function __construct(string $uri = null) |
||
| 63 | { |
||
| 64 | // Reset attributes. |
||
| 65 | $this->reset(); |
||
| 66 | |||
| 67 | // Parse URI string if not provided as null value. |
||
| 68 | if ($uri !== null) { |
||
| 69 | $this->parseUri($uri); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Resets the current state. |
||
| 75 | */ |
||
| 76 | protected function reset(): void |
||
| 77 | { |
||
| 78 | // Reset attributes. |
||
| 79 | $this->scheme = ''; |
||
| 80 | $this->user = ''; |
||
| 81 | $this->pass = ''; |
||
| 82 | $this->host = ''; |
||
| 83 | $this->port = null; |
||
| 84 | $this->path = ''; |
||
| 85 | $this->query = ''; |
||
| 86 | $this->fragment = ''; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Parses a URI string. |
||
| 91 | * |
||
| 92 | * @param string $uri The URI string to parse. |
||
| 93 | */ |
||
| 94 | protected function parseUri(string $uri): void |
||
| 95 | { |
||
| 96 | // Use builtin parse_url() function to parse the URI. |
||
| 97 | $parseInfo = parse_url($uri); |
||
| 98 | |||
| 99 | if ($parseInfo === false) { |
||
| 100 | throw new \InvalidArgumentException("The URI '{$uri}' could not be parsed by parse_url()."); |
||
| 101 | } |
||
| 102 | |||
| 103 | // Reset attributes before applying new data. |
||
| 104 | $this->reset(); |
||
| 105 | |||
| 106 | if (isset($parseInfo['scheme'])) { |
||
| 107 | $this->setScheme($parseInfo['scheme']); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (isset($parseInfo['user'])) { |
||
| 111 | $this->setUser($parseInfo['user']); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (isset($parseInfo['pass'])) { |
||
| 115 | $this->setPass($parseInfo['pass']); |
||
| 116 | } |
||
| 117 | |||
| 118 | if (isset($parseInfo['host'])) { |
||
| 119 | $this->setHost($parseInfo['host']); |
||
| 120 | } |
||
| 121 | |||
| 122 | if (isset($parseInfo['port'])) { |
||
| 123 | $this->setPort($parseInfo['port']); |
||
| 124 | } |
||
| 125 | |||
| 126 | if (isset($parseInfo['path'])) { |
||
| 127 | $this->setPath($parseInfo['path']); |
||
| 128 | } |
||
| 129 | |||
| 130 | if (isset($parseInfo['query'])) { |
||
| 131 | $this->setQuery($parseInfo['query']); |
||
| 132 | } |
||
| 133 | |||
| 134 | if (isset($parseInfo['fragment'])) { |
||
| 135 | $this->setFragment($parseInfo['fragment']); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @inheritDoc |
||
| 141 | */ |
||
| 142 | public function getScheme() |
||
| 143 | { |
||
| 144 | return $this->scheme; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns whether or not a scheme is present. |
||
| 149 | * |
||
| 150 | * @return bool True if the scheme is present. |
||
| 151 | */ |
||
| 152 | public function hasScheme(): bool |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Sets the scheme of the URI. |
||
| 159 | * |
||
| 160 | * @param string $scheme The URI scheme. |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | protected function setScheme(string $scheme): self |
||
| 164 | { |
||
| 165 | // According to PSR-7 the scheme is lowercase. |
||
| 166 | $this->scheme = strtolower($scheme); |
||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @inheritDoc |
||
| 172 | */ |
||
| 173 | public function getAuthority() |
||
| 174 | { |
||
| 175 | return rtrim(ltrim("{$this->getUserInfo()}@{$this->getHost()}:{$this->getPort()}", '@'), ':'); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns whether or not a authority is present. |
||
| 180 | * |
||
| 181 | * @return bool True if the authority is present. |
||
| 182 | */ |
||
| 183 | public function hasAuthority(): bool |
||
| 184 | { |
||
| 185 | return !empty($this->getAuthority()); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritDoc |
||
| 190 | */ |
||
| 191 | public function getUserInfo() |
||
| 192 | { |
||
| 193 | return trim("{$this->user}:{$this->pass}", ':'); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns whether or not a user is present. |
||
| 198 | * |
||
| 199 | * @return bool True if the user is present. |
||
| 200 | */ |
||
| 201 | public function hasUser(): bool |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Sets the username of the URI authority part. |
||
| 208 | * |
||
| 209 | * @param string $user The name of the user. |
||
| 210 | * @return $this |
||
| 211 | */ |
||
| 212 | protected function setUser(string $user): self |
||
| 213 | { |
||
| 214 | $this->user = $user; |
||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns whether or not a password is present. |
||
| 220 | * |
||
| 221 | * @return bool True if the password is present. |
||
| 222 | */ |
||
| 223 | public function hasPass(): bool |
||
| 224 | { |
||
| 225 | return !empty($this->pass); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Sets the password of the URI authority part. |
||
| 230 | * |
||
| 231 | * @param string $pass The password of the user. |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | protected function setPass(string $pass): self |
||
| 235 | { |
||
| 236 | $this->pass = $pass; |
||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @inheritDoc |
||
| 242 | */ |
||
| 243 | public function getHost() |
||
| 244 | { |
||
| 245 | return $this->host; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Returns whether or not a host is present. |
||
| 250 | * |
||
| 251 | * @return bool True if the host is present. |
||
| 252 | */ |
||
| 253 | public function hasHost(): bool |
||
| 254 | { |
||
| 255 | return !empty($this->host); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Sets the hostname of the authority part. |
||
| 260 | * |
||
| 261 | * @param string $host The hostname. |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | protected function setHost(string $host): self |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @inheritDoc |
||
| 273 | */ |
||
| 274 | public function getPort() |
||
| 275 | { |
||
| 276 | return |
||
| 277 | ($this->getScheme() === 'http' && $this->port === 80) || |
||
| 278 | ($this->getScheme() === 'https' && $this->port === 443) |
||
| 279 | ? null |
||
| 280 | : $this->port; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns whether or not a port is present. |
||
| 285 | * |
||
| 286 | * @return bool True if the port is present. |
||
| 287 | */ |
||
| 288 | public function hasPort(): bool |
||
| 289 | { |
||
| 290 | return !empty($this->port); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Sets the port of the authority part. |
||
| 295 | * |
||
| 296 | * @param int|null $port The port number. |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | protected function setPort(?int $port): self |
||
| 300 | { |
||
| 301 | $this->port = $port; |
||
| 302 | return $this; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @inheritDoc |
||
| 307 | */ |
||
| 308 | public function getPath() |
||
| 309 | { |
||
| 310 | return $this->path; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns whether or not a path is present. |
||
| 315 | * |
||
| 316 | * @return bool True if the path is present. |
||
| 317 | */ |
||
| 318 | public function hasPath(): bool |
||
| 319 | { |
||
| 320 | return !empty($this->path); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Sets the path of the URI. |
||
| 325 | * |
||
| 326 | * @param string $path The URI path. |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | protected function setPath(string $path): self |
||
| 330 | { |
||
| 331 | $this->path = $path; |
||
| 332 | return $this; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @inheritDoc |
||
| 337 | */ |
||
| 338 | public function getQuery() |
||
| 339 | { |
||
| 340 | return $this->query; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Returns whether or not a query is present. |
||
| 345 | * |
||
| 346 | * @return bool True if the query is present. |
||
| 347 | */ |
||
| 348 | public function hasQuery(): bool |
||
| 349 | { |
||
| 350 | return !empty($this->query); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Sets the query part of the URI. |
||
| 355 | * |
||
| 356 | * @param string $query The URI query. |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | protected function setQuery(string $query): self |
||
| 360 | { |
||
| 361 | $this->query = $query; |
||
| 362 | return $this; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @inheritDoc |
||
| 367 | */ |
||
| 368 | public function getFragment() |
||
| 369 | { |
||
| 370 | return $this->fragment; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Returns whether or not a fragment is present. |
||
| 375 | * |
||
| 376 | * @return bool True if the fragment is present. |
||
| 377 | */ |
||
| 378 | public function hasFragment(): bool |
||
| 379 | { |
||
| 380 | return !empty($this->fragment); |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Sets the fragment of the URI. |
||
| 385 | * |
||
| 386 | * @param string $fragment The URI fragment. |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | protected function setFragment(string $fragment): self |
||
| 390 | { |
||
| 391 | $this->fragment = $fragment; |
||
| 392 | return $this; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @inheritDoc |
||
| 397 | */ |
||
| 398 | public function withScheme($scheme) |
||
| 399 | { |
||
| 400 | return (clone $this)->setScheme($scheme); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @inheritDoc |
||
| 405 | */ |
||
| 406 | public function withUserInfo($user, $password = null) |
||
| 407 | { |
||
| 408 | return (clone $this) |
||
| 409 | ->setUser($user) |
||
| 410 | ->setPass($password ?? ''); |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @inheritDoc |
||
| 415 | */ |
||
| 416 | public function withHost($host) |
||
| 417 | { |
||
| 418 | return (clone $this)->setHost($host); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @inheritDoc |
||
| 423 | */ |
||
| 424 | public function withPort($port) |
||
| 425 | { |
||
| 426 | return (clone $this)->setPort($port); |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @inheritDoc |
||
| 431 | */ |
||
| 432 | public function withPath($path) |
||
| 433 | { |
||
| 434 | return (clone $this)->setPath($path); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @inheritDoc |
||
| 439 | */ |
||
| 440 | public function withQuery($query) |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @inheritDoc |
||
| 447 | */ |
||
| 448 | public function withFragment($fragment) |
||
| 449 | { |
||
| 450 | return (clone $this)->setFragment($fragment); |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @inheritDoc |
||
| 455 | */ |
||
| 456 | public function __toString() |
||
| 457 | { |
||
| 458 | $uri = ''; |
||
| 459 | |||
| 460 | if ($this->hasScheme()) { |
||
| 461 | $uri .= "{$this->getScheme()}:"; |
||
| 462 | } |
||
| 463 | |||
| 464 | if ($this->hasAuthority()) { |
||
| 465 | $uri .= "//{$this->getAuthority()}"; |
||
| 466 | } |
||
| 467 | |||
| 468 | if ($this->hasPath()) { |
||
| 469 | if ($this->getPath()[0] === '/' || $this->hasAuthority()) { |
||
| 470 | $uri .= '/'; |
||
| 471 | } |
||
| 485 | } |
||
| 486 | } |
||
| 487 |