From 2d85842777b324e7f3ba350cd45a3ca1d2e12506 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 13 May 2020 22:21:59 +0200 Subject: [PATCH] refacto: higher order messages --- src/Support/HigherOrderMessage.php | 28 +++++++++++++++++ src/Support/HigherOrderMessageCollection.php | 32 ++------------------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/Support/HigherOrderMessage.php b/src/Support/HigherOrderMessage.php index 53b59c61..d5bb0385 100644 --- a/src/Support/HigherOrderMessage.php +++ b/src/Support/HigherOrderMessage.php @@ -4,11 +4,16 @@ declare(strict_types=1); namespace Pest\Support; +use ReflectionClass; +use Throwable; + /** * @internal */ final class HigherOrderMessage { + public const UNDEFINED_METHOD = 'Method %s does not exist'; + /** * The filename where the function was originally called. * @@ -57,4 +62,27 @@ final class HigherOrderMessage $this->methodName = $methodName; $this->arguments = $arguments; } + + /** + * Re-throws the given `$throwable` with the good line and filename. + * + * @return mixed + */ + public function call(object $target) + { + try { + return Reflection::call($target, $this->methodName, $this->arguments); + } catch (Throwable $throwable) { + Reflection::setPropertyValue($throwable, 'file', $this->filename); + Reflection::setPropertyValue($throwable, 'line', $this->line); + + if ($throwable->getMessage() === sprintf(self::UNDEFINED_METHOD, $this->methodName)) { + /** @var \ReflectionClass $reflection */ + $reflection = (new ReflectionClass($target))->getParentClass(); + Reflection::setPropertyValue($throwable, 'message', sprintf('Call to undefined method %s::%s()', $reflection->getName(), $this->methodName)); + } + + throw $throwable; + } + } } diff --git a/src/Support/HigherOrderMessageCollection.php b/src/Support/HigherOrderMessageCollection.php index fa52308a..51d744bc 100644 --- a/src/Support/HigherOrderMessageCollection.php +++ b/src/Support/HigherOrderMessageCollection.php @@ -4,16 +4,11 @@ declare(strict_types=1); namespace Pest\Support; -use ReflectionClass; -use Throwable; - /** * @internal */ final class HigherOrderMessageCollection { - public const UNDEFINED_METHOD = 'Method %s does not exist'; - /** * @var array */ @@ -35,7 +30,7 @@ final class HigherOrderMessageCollection public function chain(object $target): void { foreach ($this->messages as $message) { - $target = $this->attempt($target, $message); + $target = $message->call($target); } } @@ -45,30 +40,7 @@ final class HigherOrderMessageCollection public function proxy(object $target): void { foreach ($this->messages as $message) { - $this->attempt($target, $message); - } - } - - /** - * Re-throws the given `$throwable` with the good line and filename. - * - * @return mixed - */ - private function attempt(object $target, HigherOrderMessage $message) - { - try { - return Reflection::call($target, $message->methodName, $message->arguments); - } catch (Throwable $throwable) { - Reflection::setPropertyValue($throwable, 'file', $message->filename); - Reflection::setPropertyValue($throwable, 'line', $message->line); - - if ($throwable->getMessage() === sprintf(self::UNDEFINED_METHOD, $message->methodName)) { - /** @var \ReflectionClass $reflection */ - $reflection = (new ReflectionClass($target))->getParentClass(); - Reflection::setPropertyValue($throwable, 'message', sprintf('Call to undefined method %s::%s()', $reflection->getName(), $message->methodName)); - } - - throw $throwable; + $message->call($target); } } }