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 dea44bf0..4f2dc54f 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) @@ -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; } @@ -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); } 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'] - ); - } -}