Skip to content

Throwable type improvements #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -198,7 +198,7 @@ private function call(callable $callback)
function ($value = null) {
$this->resolve($value);
},
function ($reason) {
function (\Throwable $reason) {
$this->reject($reason);
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
22 changes: 9 additions & 13 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
37 changes: 0 additions & 37 deletions tests/FunctionalRejectTest.php

This file was deleted.