Skip to content

Commit

Permalink
Merge pull request #141 from jsor-labs/throwable-type-improvements
Browse files Browse the repository at this point in the history
Throwable type improvements
  • Loading branch information
clue authored May 9, 2019
2 parents cedffca + 8967d23 commit d3712e4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 54 deletions.
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.

0 comments on commit d3712e4

Please sign in to comment.