From fdbabbdfa609e0915d3141b6291184024c7c6669 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sun, 30 Jul 2023 20:42:46 +0200 Subject: [PATCH] Improve BaseUri implementation --- BaseUri.php | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/BaseUri.php b/BaseUri.php index a5c5ada0..42d02440 100644 --- a/BaseUri.php +++ b/BaseUri.php @@ -57,34 +57,34 @@ private function __construct( private function computeOrigin(Psr7UriInterface|UriInterface $uri, ?string $nullValue): Psr7UriInterface|UriInterface|null { $scheme = $uri->getScheme(); - if ('blob' === $scheme) { - $components = UriString::parse($uri->getPath()); - if ($uri instanceof Psr7UriInterface) { - /** @var ComponentMap $components */ - $components = array_map(fn ($component) => null === $component ? '' : $component, $components); - } + if ('blob' !== $scheme) { + return match (true) { + isset(self::WHATWG_SPECIAL_SCHEMES[$scheme]) => $uri + ->withFragment($nullValue) + ->withQuery($nullValue) + ->withPath('') + ->withUserInfo($nullValue), + default => null, + }; + } + + $components = UriString::parse($uri->getPath()); + if ($uri instanceof Psr7UriInterface) { + /** @var ComponentMap $components */ + $components = array_map(fn ($component) => null === $component ? '' : $component, $components); + } - $uri = $uri + return match (true) { + null !== $components['scheme'] && isset(self::WHATWG_SPECIAL_SCHEMES[strtolower($components['scheme'])]) => $uri ->withFragment($this->nullValue) ->withQuery($this->nullValue) ->withPath('') ->withHost($components['host']) ->withPort($components['port']) ->withScheme($components['scheme']) - ->withUserInfo($this->nullValue); - - $scheme = $uri->getScheme(); - } - - if (!isset(self::WHATWG_SPECIAL_SCHEMES[$scheme])) { - return null; - } - - return $uri - ->withFragment($nullValue) - ->withQuery($nullValue) - ->withPath('') - ->withUserInfo($nullValue); + ->withUserInfo($this->nullValue), + default => null, + }; } public static function from(Stringable|string $uri, UriFactoryInterface|null $uriFactory = null): self