From be81b2631a72cc6359d3f0486ad741b37d1a67a9 Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Thu, 9 May 2019 10:49:06 +0200 Subject: [PATCH 1/4] Improve _checkTypehint() * Add type-declaration to $reason parameter * Avoid double call of ->getClass() --- src/functions.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/functions.php b/src/functions.php index dea44bf0..cf6f4323 100644 --- a/src/functions.php +++ b/src/functions.php @@ -324,12 +324,8 @@ function fatalError($error) /** * @internal */ -function _checkTypehint(callable $callback, $object) +function _checkTypehint(callable $callback, \Throwable $reason) { - if (!\is_object($object)) { - return true; - } - if (\is_array($callback)) { $callbackReflection = new \ReflectionMethod($callback[0], $callback[1]); } elseif (\is_object($callback) && !$callback instanceof \Closure) { @@ -344,11 +340,11 @@ function _checkTypehint(callable $callback, $object) return true; } - $expectedException = $parameters[0]; + $expectedClass = $parameters[0]->getClass(); - if (!$expectedException->getClass()) { + if (!$expectedClass) { return true; } - return $expectedException->getClass()->isInstance($object); + return $expectedClass->isInstance($reason); } From bbf67e6823ae4411c87230add624b40ef2ad6bdb Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Thu, 9 May 2019 10:54:28 +0200 Subject: [PATCH 2/4] Fix variable name in docblock description and @param declaration --- src/functions.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/functions.php b/src/functions.php index cf6f4323..c70b44c4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -41,19 +41,19 @@ function resolve($promiseOrValue = null) } /** - * Creates a rejected promise for the supplied `$promiseOrValue`. + * Creates a rejected promise for the supplied `$reason`. * - * If `$promiseOrValue` is a value, it will be the rejection value of the + * If `$reason` is a value, it will be the rejection value of the * returned promise. * - * If `$promiseOrValue` is a promise, its completion value will be the rejected + * If `$reason` is a promise, its completion value will be the rejected * value of the returned promise. * * This can be useful in situations where you need to reject a promise without * throwing an exception. For example, it allows you to propagate a rejection with * the value of another promise. * - * @param \Throwable $promiseOrValue + * @param \Throwable $reason * @return PromiseInterface */ function reject(\Throwable $reason) From f12d524917ec065a25ff5b3b6056a8e747a7aa7f Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Thu, 9 May 2019 11:03:30 +0200 Subject: [PATCH 3/4] Remove FunctionalRejectTest as it only tests non-throwable rejection reasons --- tests/FunctionalRejectTest.php | 37 ---------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 tests/FunctionalRejectTest.php diff --git a/tests/FunctionalRejectTest.php b/tests/FunctionalRejectTest.php deleted file mode 100644 index d303d7a4..00000000 --- a/tests/FunctionalRejectTest.php +++ /dev/null @@ -1,37 +0,0 @@ - [1]; - yield 'true' => [true]; - yield 'stdClass' => [new stdClass()]; - } - - /** - * @test - * @dataProvider nonThrowables - */ - public function shouldThrowWhenCalledWithANonException($input) - { - $errorCollector = new ErrorCollector(); - $errorCollector->start(); - - (new Promise(function ($_, $reject) use ($input) { - $reject($input); - }))->done($this->expectCallableNever()); - - $errors = $errorCollector->stop(); - - $this->assertEquals(E_USER_ERROR, $errors[0]['errno']); - $this->assertContains( - 'TypeError: Argument 1 passed to React\Promise\reject() must implement interface Throwable', - $errors[0]['errstr'] - ); - } -} From 8967d233b3476042025d74d8e96df45a3b1c1f1b Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Thu, 9 May 2019 11:10:09 +0200 Subject: [PATCH 4/4] Add missing Throwable parameter type-declarations to rejection functions --- src/Deferred.php | 2 +- src/Promise.php | 4 ++-- src/RejectedPromise.php | 2 +- src/functions.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Deferred.php b/src/Deferred.php index 6bc40293..ef06069f 100644 --- a/src/Deferred.php +++ b/src/Deferred.php @@ -36,7 +36,7 @@ public function resolve($value = null) \call_user_func($this->resolveCallback, $value); } - public function reject($reason) + public function reject(\Throwable $reason) { $this->promise(); diff --git a/src/Promise.php b/src/Promise.php index 0d75bde3..f25d3667 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -129,7 +129,7 @@ private function resolve($value = null) $this->settle(resolve($value)); } - private function reject($reason) + private function reject(\Throwable $reason) { if (null !== $this->result) { return; @@ -198,7 +198,7 @@ private function call(callable $callback) function ($value = null) { $this->resolve($value); }, - function ($reason) { + function (\Throwable $reason) { $this->reject($reason); } ); diff --git a/src/RejectedPromise.php b/src/RejectedPromise.php index bfa85655..d256b98f 100644 --- a/src/RejectedPromise.php +++ b/src/RejectedPromise.php @@ -62,7 +62,7 @@ public function otherwise(callable $onRejected) public function always(callable $onFulfilledOrRejected) { - return $this->then(null, function ($reason) use ($onFulfilledOrRejected) { + return $this->then(null, function (\Throwable $reason) use ($onFulfilledOrRejected) { return resolve($onFulfilledOrRejected())->then(function () use ($reason) { return new RejectedPromise($reason); }); diff --git a/src/functions.php b/src/functions.php index c70b44c4..4f2dc54f 100644 --- a/src/functions.php +++ b/src/functions.php @@ -188,7 +188,7 @@ function some(array $promisesOrValues, $howMany) } }; - $rejecter = function ($reason) use ($i, &$reasons, &$toReject, $toResolve, $reject) { + $rejecter = function (\Throwable $reason) use ($i, &$reasons, &$toReject, $toResolve, $reject) { if ($toResolve < 1 || $toReject < 1) { return; }