From c10a585fd515c281a7666c439faba3b12781fe5b Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Wed, 23 Aug 2023 21:42:49 +0200 Subject: [PATCH] Improve codebase by using match --- BaseUri.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/BaseUri.php b/BaseUri.php index 25c4995d..31ddbcdf 100644 --- a/BaseUri.php +++ b/BaseUri.php @@ -109,8 +109,11 @@ public function isCrossOrigin(Stringable|string $uri): bool $uri = static::filterUri($uri); $uriOrigin = $this->computeOrigin($uri, $uri instanceof Psr7UriInterface ? '' : null); - return null === $uriOrigin - || $uriOrigin->__toString() !== $this->origin->__toString(); + return match(true) { + null === $uriOrigin, + $uriOrigin->__toString() !== $this->origin->__toString() => true, + default => false, + }; } /** @@ -447,7 +450,8 @@ final protected static function formatHost(Psr7UriInterface|UriInterface $uri): return match (true) { null !== $converted => $uri->withHost($converted), - $uri instanceof UriInterface, '' === $host => $uri, + '' === $host, + $uri instanceof UriInterface => $uri, default => $uri->withHost((string) Uri::fromComponents(['host' => $host])->getHost()), }; } @@ -504,20 +508,16 @@ final protected static function getSegments(string $path): array */ final protected static function formatPath(string $path, string $basePath): string { - if ('' === $path) { - return in_array($basePath, ['', '/'], true) ? $basePath : './'; - } - - if (false === ($colonPosition = strpos($path, ':'))) { - return $path; - } - + $colonPosition = strpos($path, ':'); $slashPosition = strpos($path, '/'); - if (false === $slashPosition || $colonPosition < $slashPosition) { - return "./$path"; - } - return $path; + return match (true) { + '' === $path => in_array($basePath, ['', '/'], true) ? $basePath : './', + false === $colonPosition => $path, + false === $slashPosition, + $colonPosition < $slashPosition => "./$path", + default => $path, + }; } /**