From bef96362ffdce8c12d8d786282b3cd6b7e71075e Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Thu, 19 Dec 2024 08:08:17 +0100 Subject: [PATCH] Improve Modifier::__call method implementation --- components/Modifier.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/components/Modifier.php b/components/Modifier.php index 71ae685b..6f0da852 100644 --- a/components/Modifier.php +++ b/components/Modifier.php @@ -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; @@ -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))), };