From fc352a303fd833f8d4f1c7f7040820d86b13ddab Mon Sep 17 00:00:00 2001 From: Marcin Czarnecki Date: Thu, 16 Dec 2021 22:39:48 +0100 Subject: [PATCH] Improve performance of Assertion Chain class (#318) --- lib/Assert/AssertionChain.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/Assert/AssertionChain.php b/lib/Assert/AssertionChain.php index b3f8fa5e..4c444350 100644 --- a/lib/Assert/AssertionChain.php +++ b/lib/Assert/AssertionChain.php @@ -15,7 +15,6 @@ namespace Assert; use LogicException; -use ReflectionClass; /** * Chaining builder for assertions. @@ -171,13 +170,12 @@ public function __call($methodName, $args): AssertionChain return $this; } - if (!\method_exists($this->assertionClassName, $methodName)) { + try { + $method = new \ReflectionMethod($this->assertionClassName, $methodName); + } catch (\ReflectionException $exception) { throw new \RuntimeException("Assertion '".$methodName."' does not exist."); } - $reflClass = new ReflectionClass($this->assertionClassName); - $method = $reflClass->getMethod($methodName); - \array_unshift($args, $this->value); $params = $method->getParameters(); @@ -186,12 +184,13 @@ public function __call($methodName, $args): AssertionChain continue; } - if ('message' == $param->getName()) { - $args[$idx] = $this->defaultMessage; - } - - if ('propertyPath' == $param->getName()) { - $args[$idx] = $this->defaultPropertyPath; + switch ($param->getName()) { + case 'message': + $args[$idx] = $this->defaultMessage; + break; + case 'propertyPath': + $args[$idx] = $this->defaultPropertyPath; + break; } }