From 323c5f211f755192625b6b5ce1931d6a8df3ac46 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 11:55:32 +0200 Subject: [PATCH 01/12] pass calls to overloaded method if possible fix #108 --- src/Support/Reflection.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 4c0cecf6..5b74b10e 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -27,11 +27,19 @@ final class Reflection { $reflectionClass = new ReflectionClass($object); - $reflectionMethod = $reflectionClass->getMethod($method); + try { + $reflectionMethod = $reflectionClass->getMethod($method); - $reflectionMethod->setAccessible(true); + $reflectionMethod->setAccessible(true); - return $reflectionMethod->invoke($object, ...$args); + return $reflectionMethod->invoke($object, ...$args); + } catch(ReflectionException $ex) { + if (method_exists($object, '__call')) { + return $object->__call($method, $args); + } + + throw $ex; + } } /** From 0c0488238957aa9c9679481a3de7e452c92739bb Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 13:08:06 +0200 Subject: [PATCH 02/12] use long exception variable name --- src/Support/Reflection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 5b74b10e..44805ba2 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -33,12 +33,12 @@ final class Reflection $reflectionMethod->setAccessible(true); return $reflectionMethod->invoke($object, ...$args); - } catch(ReflectionException $ex) { + } catch (ReflectionException $exception) { if (method_exists($object, '__call')) { return $object->__call($method, $args); } - throw $ex; + throw $exception; } } From ef5715ce10d1a9c0d9b84a9ed7d04a3aaa490457 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 14:02:42 +0200 Subject: [PATCH 03/12] ignore null return values and re-use old target --- src/Support/HigherOrderMessageCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/HigherOrderMessageCollection.php b/src/Support/HigherOrderMessageCollection.php index 51d744bc..f16765e1 100644 --- a/src/Support/HigherOrderMessageCollection.php +++ b/src/Support/HigherOrderMessageCollection.php @@ -30,7 +30,7 @@ final class HigherOrderMessageCollection public function chain(object $target): void { foreach ($this->messages as $message) { - $target = $message->call($target); + $target = $message->call($target) ?? $target; } } From 53a8c7b05e2050e9b8c49c0d99a1dce74348596b Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 14:02:56 +0200 Subject: [PATCH 04/12] add test for new macro behavior --- tests/.snapshots/success.txt | 7 +++++-- tests/Features/Macro.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/Features/Macro.php diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 1671b70b..6c2d741e 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -67,6 +67,9 @@ ✓ it is a test ✓ it is a higher order message test + PASS Tests\Features\Macro + ✓ it can call chained macro method + PASS Tests\Features\Mocks ✓ it has bar @@ -151,5 +154,5 @@ WARN Tests\Visual\Success s visual snapshot of test suite on success - Tests: 6 skipped, 86 passed - Time: 3.58s + Tests: 6 skipped, 87 passed + Time: 3.60s diff --git a/tests/Features/Macro.php b/tests/Features/Macro.php new file mode 100644 index 00000000..7ba8c127 --- /dev/null +++ b/tests/Features/Macro.php @@ -0,0 +1,12 @@ +macro('bar', function () { + assertInstanceOf(TestCase::class, $this); + + return $this; +})->bar(); From e135979f34bd6d9dcbffe5c10c4dbf4f9a2a7806 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 18:22:34 +0200 Subject: [PATCH 05/12] revert null old-target logic --- src/Support/HigherOrderMessageCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/HigherOrderMessageCollection.php b/src/Support/HigherOrderMessageCollection.php index f16765e1..51d744bc 100644 --- a/src/Support/HigherOrderMessageCollection.php +++ b/src/Support/HigherOrderMessageCollection.php @@ -30,7 +30,7 @@ final class HigherOrderMessageCollection public function chain(object $target): void { foreach ($this->messages as $message) { - $target = $message->call($target) ?? $target; + $target = $message->call($target); } } From eff2d7f6131abcff15a0a4d254fb18576d4eede5 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 18:23:39 +0200 Subject: [PATCH 06/12] fix test and add exception test --- tests/Features/Macro.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/Features/Macro.php b/tests/Features/Macro.php index 7ba8c127..bb230e19 100644 --- a/tests/Features/Macro.php +++ b/tests/Features/Macro.php @@ -5,8 +5,16 @@ use PHPUnit\Framework\TestCase; uses(Macroable::class); -it('can call chained macro method')->macro('bar', function () { - assertInstanceOf(TestCase::class, $this); +beforeEach(function () { + $this->macro('bar', function () { + assertInstanceOf(TestCase::class, $this); - return $this; -})->bar(); + return $this; + }); +}); + +it('can call chained macro method')->bar(); + +it('will throw exception from call if no macro exists') + ->throws(BadMethodCallException::class) + ->foo(); From 74c14808cf55368e6670efb5f65afb5015588d3d Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 18:24:49 +0200 Subject: [PATCH 07/12] rebuild success.txt --- tests/.snapshots/success.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 6c2d741e..787984ae 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -69,6 +69,7 @@ PASS Tests\Features\Macro ✓ it can call chained macro method + ✓ it will throw exception from call if no macro exists PASS Tests\Features\Mocks ✓ it has bar @@ -154,5 +155,5 @@ WARN Tests\Visual\Success s visual snapshot of test suite on success - Tests: 6 skipped, 87 passed - Time: 3.60s + Tests: 6 skipped, 88 passed + Time: 3.53s From bff97418edb5ca8b5493f24172780acbde2107b6 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 18:27:43 +0200 Subject: [PATCH 08/12] do not return $this --- tests/Features/Macro.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Features/Macro.php b/tests/Features/Macro.php index bb230e19..c90a3ea3 100644 --- a/tests/Features/Macro.php +++ b/tests/Features/Macro.php @@ -8,8 +8,6 @@ uses(Macroable::class); beforeEach(function () { $this->macro('bar', function () { assertInstanceOf(TestCase::class, $this); - - return $this; }); }); From 321b3e8df370c734b60d61f42581ae9deecc947a Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 18:31:09 +0200 Subject: [PATCH 09/12] use call_user_func_array instead of direct calling __call add check for __callStatic --- src/Support/Reflection.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 44805ba2..79342e74 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -34,8 +34,11 @@ final class Reflection return $reflectionMethod->invoke($object, ...$args); } catch (ReflectionException $exception) { - if (method_exists($object, '__call')) { - return $object->__call($method, $args); + if ( + method_exists($object, '__call') + || method_exists($object, '__callStatic') + ) { + return call_user_func_array([$object, $method], $args); } throw $exception; From 9a0cfaf33940af11471c89147c56735ad721d494 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 18:39:47 +0200 Subject: [PATCH 10/12] use shorter beforeEach chain syntax --- tests/Features/Macro.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Features/Macro.php b/tests/Features/Macro.php index c90a3ea3..fa9f63ee 100644 --- a/tests/Features/Macro.php +++ b/tests/Features/Macro.php @@ -5,10 +5,8 @@ use PHPUnit\Framework\TestCase; uses(Macroable::class); -beforeEach(function () { - $this->macro('bar', function () { - assertInstanceOf(TestCase::class, $this); - }); +beforeEach()->macro('bar', function () { + assertInstanceOf(TestCase::class, $this); }); it('can call chained macro method')->bar(); From 6e27c6d85d245fd9352ea9d5a28ff12f2b8d8fc4 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 18:44:04 +0200 Subject: [PATCH 11/12] fix tye check --- src/Support/Reflection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 79342e74..8b8e73ae 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -38,7 +38,7 @@ final class Reflection method_exists($object, '__call') || method_exists($object, '__callStatic') ) { - return call_user_func_array([$object, $method], $args); + return $object->{$method}(...$args); } throw $exception; From fac44d9afee034049507fcd7c3542f2501d5ca5d Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sun, 21 Jun 2020 18:46:11 +0200 Subject: [PATCH 12/12] revert to first approach by calling __call directly --- src/Support/Reflection.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 8b8e73ae..44805ba2 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -34,11 +34,8 @@ final class Reflection return $reflectionMethod->invoke($object, ...$args); } catch (ReflectionException $exception) { - if ( - method_exists($object, '__call') - || method_exists($object, '__callStatic') - ) { - return $object->{$method}(...$args); + if (method_exists($object, '__call')) { + return $object->__call($method, $args); } throw $exception;