Skip to content

Commit

Permalink
[3.x] Add template annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored and WyriHaximus committed Jun 25, 2023
1 parent d66fa66 commit 7390a8c
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 27 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
name: PHPStan (PHP ${{ matrix.php }})
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
php:
- 8.2
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
level: max
checkGenericClassInNonGenericObjectType: false

paths:
- src/
- tests/
- types/
8 changes: 7 additions & 1 deletion src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace React\Promise;

/**
* @template-covariant T
*/
final class Deferred
{
/** @var Promise */
/** @var PromiseInterface<T> */
private $promise;

/** @var callable */
Expand All @@ -21,6 +24,9 @@ public function __construct(callable $canceller = null)
}, $canceller);
}

/**
* @return PromiseInterface<T>
*/
public function promise(): PromiseInterface
{
return $this->promise;
Expand Down
18 changes: 16 additions & 2 deletions src/Internal/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

/**
* @internal
*
* @template-implements PromiseInterface<T>
* @template-covariant T
*/
final class FulfilledPromise implements PromiseInterface
{
/** @var mixed */
/** @var T */
private $value;

/**
* @param mixed $value
* @param T $value
* @throws \InvalidArgumentException
*/
public function __construct($value = null)
Expand All @@ -26,6 +29,9 @@ public function __construct($value = null)
$this->value = $value;
}

/**
* @inheritdoc
*/
public function then(callable $onFulfilled = null, callable $onRejected = null): PromiseInterface
{
if (null === $onFulfilled) {
Expand All @@ -39,11 +45,17 @@ public function then(callable $onFulfilled = null, callable $onRejected = null):
}
}

/**
* @inheritdoc
*/
public function catch(callable $onRejected): PromiseInterface
{
return $this;
}

/**
* @inheritdoc
*/
public function finally(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(function ($value) use ($onFulfilledOrRejected): PromiseInterface {
Expand All @@ -60,6 +72,7 @@ public function cancel(): void
/**
* @deprecated 3.0.0 Use `catch()` instead
* @see self::catch()
* @inheritdoc
*/
public function otherwise(callable $onRejected): PromiseInterface
{
Expand All @@ -69,6 +82,7 @@ public function otherwise(callable $onRejected): PromiseInterface
/**
* @deprecated 3.0.0 Use `finally()` instead
* @see self::finally()
* @inheritdoc
*/
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
Expand Down
14 changes: 14 additions & 0 deletions src/Internal/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

/**
* @internal
*
* @template-implements PromiseInterface<T>
* @template-covariant T
*/
final class RejectedPromise implements PromiseInterface
{
Expand All @@ -22,6 +25,9 @@ public function __construct(\Throwable $reason)
$this->reason = $reason;
}

/**
* @inheritdoc
*/
public function then(callable $onFulfilled = null, callable $onRejected = null): PromiseInterface
{
if (null === $onRejected) {
Expand All @@ -35,6 +41,9 @@ public function then(callable $onFulfilled = null, callable $onRejected = null):
}
}

/**
* @inheritdoc
*/
public function catch(callable $onRejected): PromiseInterface
{
if (!_checkTypehint($onRejected, $this->reason)) {
Expand All @@ -44,6 +53,9 @@ public function catch(callable $onRejected): PromiseInterface
return $this->then(null, $onRejected);
}

/**
* @inheritdoc
*/
public function finally(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(null, function (\Throwable $reason) use ($onFulfilledOrRejected): PromiseInterface {
Expand All @@ -60,6 +72,7 @@ public function cancel(): void
/**
* @deprecated 3.0.0 Use `catch()` instead
* @see self::catch()
* @inheritdoc
*/
public function otherwise(callable $onRejected): PromiseInterface
{
Expand All @@ -69,6 +82,7 @@ public function otherwise(callable $onRejected): PromiseInterface
/**
* @deprecated 3.0.0 Use `always()` instead
* @see self::always()
* @inheritdoc
*/
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
Expand Down
21 changes: 18 additions & 3 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

use React\Promise\Internal\RejectedPromise;

/**
* @template-implements PromiseInterface<T>
* @template-covariant T
*/
final class Promise implements PromiseInterface
{
/** @var ?callable */
private $canceller;

/** @var ?PromiseInterface */
/** @var PromiseInterface<T> */
private $result;

/** @var callable[] */
Expand All @@ -30,6 +34,9 @@ public function __construct(callable $resolver, callable $canceller = null)
$this->call($cb);
}

/**
* @inheritdoc
*/
public function then(callable $onFulfilled = null, callable $onRejected = null): PromiseInterface
{
if (null !== $this->result) {
Expand Down Expand Up @@ -63,6 +70,9 @@ static function () use (&$parent) {
);
}

/**
* @inheritdoc
*/
public function catch(callable $onRejected): PromiseInterface
{
return $this->then(null, static function ($reason) use ($onRejected) {
Expand All @@ -74,14 +84,17 @@ public function catch(callable $onRejected): PromiseInterface
});
}

/**
* @inheritdoc
*/
public function finally(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(static function ($value) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
return resolve($onFulfilledOrRejected())->then(function ($_) use ($value) {
return $value;
});
}, static function ($reason) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return resolve($onFulfilledOrRejected())->then(function ($_) use ($reason) {
return new RejectedPromise($reason);
});
});
Expand Down Expand Up @@ -125,6 +138,7 @@ public function cancel(): void
/**
* @deprecated 3.0.0 Use `catch()` instead
* @see self::catch()
* @inheritdoc
*/
public function otherwise(callable $onRejected): PromiseInterface
{
Expand All @@ -134,6 +148,7 @@ public function otherwise(callable $onRejected): PromiseInterface
/**
* @deprecated 3.0.0 Use `finally()` instead
* @see self::finally()
* @inheritdoc
*/
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
Expand Down
21 changes: 12 additions & 9 deletions src/PromiseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace React\Promise;

/**
* @template-covariant T
*/
interface PromiseInterface
{
/**
Expand All @@ -28,9 +31,9 @@ interface PromiseInterface
* 2. `$onFulfilled` and `$onRejected` will never be called more
* than once.
*
* @param callable|null $onFulfilled
* @param callable|null $onRejected
* @return PromiseInterface
* @template TFulfilled as PromiseInterface<T>|T
* @param (callable(T): TFulfilled)|null $onFulfilled
* @return PromiseInterface<T>
*/
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface;

Expand All @@ -44,8 +47,7 @@ public function then(?callable $onFulfilled = null, ?callable $onRejected = null
* Additionally, you can type hint the `$reason` argument of `$onRejected` to catch
* only specific errors.
*
* @param callable $onRejected
* @return PromiseInterface
* @return PromiseInterface<T>
*/
public function catch(callable $onRejected): PromiseInterface;

Expand Down Expand Up @@ -91,8 +93,9 @@ public function catch(callable $onRejected): PromiseInterface;
* ->finally('cleanup');
* ```
*
* @param callable $onFulfilledOrRejected
* @return PromiseInterface
* @template TReturn of PromiseInterface<T>|T
* @param callable(T=): TReturn $onFulfilledOrRejected
* @return PromiseInterface<T>
*/
public function finally(callable $onFulfilledOrRejected): PromiseInterface;

Expand All @@ -118,7 +121,7 @@ public function cancel(): void;
* ```
*
* @param callable $onRejected
* @return PromiseInterface
* @return PromiseInterface<T>
* @deprecated 3.0.0 Use catch() instead
* @see self::catch()
*/
Expand All @@ -135,7 +138,7 @@ public function otherwise(callable $onRejected): PromiseInterface;
* ```
*
* @param callable $onFulfilledOrRejected
* @return PromiseInterface
* @return PromiseInterface<T>
* @deprecated 3.0.0 Use finally() instead
* @see self::finally()
*/
Expand Down
27 changes: 16 additions & 11 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*
* If `$promiseOrValue` is a promise, it will be returned as is.
*
* @param mixed $promiseOrValue
* @return PromiseInterface
* @template T
* @param PromiseInterface<T>|T $promiseOrValue
* @return PromiseInterface<T>
*/
function resolve($promiseOrValue): PromiseInterface
{
Expand All @@ -30,7 +31,9 @@ function resolve($promiseOrValue): PromiseInterface
$canceller = null;

if (\method_exists($promiseOrValue, 'cancel')) {
$canceller = [$promiseOrValue, 'cancel'];
$canceller = static function () use ($promiseOrValue) {
$promiseOrValue->cancel();
};
}

return new Promise(function ($resolve, $reject) use ($promiseOrValue): void {
Expand All @@ -54,8 +57,7 @@ function resolve($promiseOrValue): PromiseInterface
* throwing an exception. For example, it allows you to propagate a rejection with
* the value of another promise.
*
* @param \Throwable $reason
* @return PromiseInterface
* @return PromiseInterface<null>
*/
function reject(\Throwable $reason): PromiseInterface
{
Expand All @@ -68,8 +70,9 @@ function reject(\Throwable $reason): PromiseInterface
* will be an array containing the resolution values of each of the items in
* `$promisesOrValues`.
*
* @param iterable<mixed> $promisesOrValues
* @return PromiseInterface
* @template T
* @param array<PromiseInterface<T>|T> $promisesOrValues
* @return PromiseInterface<array<T>>
*/
function all(iterable $promisesOrValues): PromiseInterface
{
Expand Down Expand Up @@ -119,8 +122,9 @@ function (\Throwable $reason) use (&$continue, $reject): void {
* The returned promise will become **infinitely pending** if `$promisesOrValues`
* contains 0 items.
*
* @param iterable<mixed> $promisesOrValues
* @return PromiseInterface
* @template T
* @param array<PromiseInterface<T>|T> $promisesOrValues
* @return PromiseInterface<T>
*/
function race(iterable $promisesOrValues): PromiseInterface
{
Expand Down Expand Up @@ -154,8 +158,9 @@ function race(iterable $promisesOrValues): PromiseInterface
* The returned promise will also reject with a `React\Promise\Exception\LengthException`
* if `$promisesOrValues` contains 0 items.
*
* @param iterable<mixed> $promisesOrValues
* @return PromiseInterface
* @template T
* @param array<PromiseInterface<T>|T> $promisesOrValues
* @return PromiseInterface<T>
*/
function any(iterable $promisesOrValues): PromiseInterface
{
Expand Down
Loading

0 comments on commit 7390a8c

Please sign in to comment.