Skip to content

Commit

Permalink
Improve Modifier::__call method implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 19, 2024
1 parent b7d429e commit bef9636
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions components/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use function get_object_vars;
use function is_bool;
use function ltrim;
use function method_exists;
use function rtrim;
use function sprintf;
use function str_ends_with;
Expand Down Expand Up @@ -122,9 +121,18 @@ public function displayUriString(): string

final public function __call(string $name, array $arguments): static
{
static $allowedMethods = [
'withScheme',
'withUserInfo',
'withHost',
'withPort',
'withPath',
'withQuery',
'withFragment',
];

return match (true) {
!str_starts_with($name, 'with'),
!method_exists($this->uri, $name) => throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', self::class, $name)),
!in_array($name, $allowedMethods, true) => throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', self::class, $name)),
$this->uri instanceof UriInterface || 'withPort' === $name => new static($this->uri->$name(...$arguments)),
default => new static($this->uri->$name(...array_map(fn (Stringable|string|null $value): string => (string) $value, $arguments))),
};
Expand Down

0 comments on commit bef9636

Please sign in to comment.