Skip to content

Commit 6772ffb

Browse files
simPodWyriHaximus
authored andcommitted
[3.x] Add template annotations
Adds template annotations turning the `PromiseInterface` into a generic. Variables `$p1` and `$p2` in the following code example both are `PromiseInterface<int|string>`. ```php $f = function (): int|string { return time() % 2 ? 'string' : time(); }; /** * @return PromiseInterface<int|string> */ $fp = function (): PromiseInterface { return resolve(time() % 2 ? 'string' : time()); }; $p1 = resolve($f()); $p2 = $fp(); ``` When calling `then` on `$p1` or `$p2`, PHPStan understand that function `$f1` is type hinting its parameter fine, but `$f2` will throw during runtime: ```php $p2->then(static function (int|string $a) {}); $p2->then(static function (bool $a) {}); ``` Builds on top of #246 and #188 and is a requirement for reactphp/async#40
1 parent d66fa66 commit 6772ffb

31 files changed

+313
-94
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/.gitattributes export-ignore
22
/.github/ export-ignore
33
/.gitignore export-ignore
4+
/phpstan.legacy.neon.dist export-ignore
45
/phpstan.neon.dist export-ignore
56
/phpunit.xml.dist export-ignore
67
/phpunit.xml.legacy export-ignore

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
name: PHPStan (PHP ${{ matrix.php }})
3636
runs-on: ubuntu-22.04
3737
strategy:
38+
fail-fast: false
3839
matrix:
3940
php:
4041
- 8.2
@@ -52,3 +53,6 @@ jobs:
5253
coverage: none
5354
- run: composer install
5455
- run: vendor/bin/phpstan
56+
if: ${{ matrix.php >= 7.2 }}
57+
- run: vendor/bin/phpstan --configuration="phpstan.legacy.neon.dist"
58+
if: ${{ matrix.php < 7.2 }}

phpstan.legacy.neon.dist

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
ignoreErrors:
3+
- '#Template type T is declared as covariant, but occurs in contravariant position in parameter result of method React\\Promise\\Promise::settle\(\).#'
4+
- '#Template type T is declared as covariant, but occurs in contravariant position in parameter promise of method React\\Promise\\Promise::unwrap\(\).#'
5+
6+
includes:
7+
- phpstan.neon.dist

src/Deferred.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace React\Promise;
44

5+
/**
6+
* @template T
7+
*/
58
final class Deferred
69
{
7-
/** @var Promise */
10+
/**
11+
* @var PromiseInterface<T>
12+
*/
813
private $promise;
914

1015
/** @var callable */
@@ -21,13 +26,16 @@ public function __construct(callable $canceller = null)
2126
}, $canceller);
2227
}
2328

29+
/**
30+
* @return PromiseInterface<T>
31+
*/
2432
public function promise(): PromiseInterface
2533
{
2634
return $this->promise;
2735
}
2836

2937
/**
30-
* @param mixed $value
38+
* @param T $value
3139
*/
3240
public function resolve($value): void
3341
{

src/Internal/FulfilledPromise.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77

88
/**
99
* @internal
10+
*
11+
* @template-implements PromiseInterface<T>
12+
* @template-covariant T
1013
*/
1114
final class FulfilledPromise implements PromiseInterface
1215
{
13-
/** @var mixed */
16+
/** @var T */
1417
private $value;
1518

1619
/**
17-
* @param mixed $value
20+
* @param T $value
1821
* @throws \InvalidArgumentException
1922
*/
2023
public function __construct($value = null)
@@ -47,9 +50,9 @@ public function catch(callable $onRejected): PromiseInterface
4750
public function finally(callable $onFulfilledOrRejected): PromiseInterface
4851
{
4952
return $this->then(function ($value) use ($onFulfilledOrRejected): PromiseInterface {
50-
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
51-
return $value;
52-
});
53+
$onFulfilledOrRejected();
54+
55+
return resolve($value);
5356
});
5457
}
5558

src/Internal/RejectedPromise.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
/**
1010
* @internal
11+
*
12+
* @template-implements PromiseInterface<never>
13+
* @template-covariant T
1114
*/
1215
final class RejectedPromise implements PromiseInterface
1316
{
@@ -47,9 +50,9 @@ public function catch(callable $onRejected): PromiseInterface
4750
public function finally(callable $onFulfilledOrRejected): PromiseInterface
4851
{
4952
return $this->then(null, function (\Throwable $reason) use ($onFulfilledOrRejected): PromiseInterface {
50-
return resolve($onFulfilledOrRejected())->then(function () use ($reason): PromiseInterface {
51-
return new RejectedPromise($reason);
52-
});
53+
$onFulfilledOrRejected();
54+
55+
return new RejectedPromise($reason);
5356
});
5457
}
5558

src/Promise.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
use React\Promise\Internal\RejectedPromise;
66

7+
/**
8+
* @template-implements PromiseInterface<T>
9+
* @template-covariant T
10+
*/
711
final class Promise implements PromiseInterface
812
{
913
/** @var ?callable */
1014
private $canceller;
1115

12-
/** @var ?PromiseInterface */
16+
/** @var ?PromiseInterface<T> */
1317
private $result;
1418

1519
/** @var callable[] */
@@ -77,13 +81,13 @@ public function catch(callable $onRejected): PromiseInterface
7781
public function finally(callable $onFulfilledOrRejected): PromiseInterface
7882
{
7983
return $this->then(static function ($value) use ($onFulfilledOrRejected) {
80-
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
81-
return $value;
82-
});
84+
$onFulfilledOrRejected();
85+
86+
return $value;
8387
}, static function ($reason) use ($onFulfilledOrRejected) {
84-
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
85-
return new RejectedPromise($reason);
86-
});
88+
$onFulfilledOrRejected();
89+
90+
return new RejectedPromise($reason);
8791
});
8892
}
8993

@@ -166,6 +170,10 @@ private function reject(\Throwable $reason): void
166170
$this->settle(reject($reason));
167171
}
168172

173+
/**
174+
* Test out if null can be promise
175+
* @param PromiseInterface<T>|PromiseInterface<never> $result
176+
*/
169177
private function settle(PromiseInterface $result): void
170178
{
171179
$result = $this->unwrap($result);
@@ -193,13 +201,17 @@ private function settle(PromiseInterface $result): void
193201
}
194202
}
195203

204+
/**
205+
* @param PromiseInterface<T>|PromiseInterface<never> $promise
206+
* @return PromiseInterface<T>
207+
*/
196208
private function unwrap(PromiseInterface $promise): PromiseInterface
197209
{
198210
while ($promise instanceof self && null !== $promise->result) {
199211
$promise = $promise->result;
200212
}
201213

202-
return $promise;
214+
return $promise; /** @phpstan-ignore-line */
203215
}
204216

205217
private function call(callable $cb): void

src/PromiseInterface.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace React\Promise;
44

5+
/**
6+
* @template-covariant T
7+
*/
58
interface PromiseInterface
69
{
710
/**
@@ -28,9 +31,9 @@ interface PromiseInterface
2831
* 2. `$onFulfilled` and `$onRejected` will never be called more
2932
* than once.
3033
*
31-
* @param callable|null $onFulfilled
32-
* @param callable|null $onRejected
33-
* @return PromiseInterface
34+
* @template TFulfilled as PromiseInterface<T>|T|void
35+
* @param (callable(T): TFulfilled)|null $onFulfilled
36+
* @return PromiseInterface<T>
3437
*/
3538
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface;
3639

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

@@ -91,8 +93,8 @@ public function catch(callable $onRejected): PromiseInterface;
9193
* ->finally('cleanup');
9294
* ```
9395
*
94-
* @param callable $onFulfilledOrRejected
95-
* @return PromiseInterface
96+
* @param callable(): void $onFulfilledOrRejected
97+
* @return PromiseInterface<T>
9698
*/
9799
public function finally(callable $onFulfilledOrRejected): PromiseInterface;
98100

@@ -118,7 +120,7 @@ public function cancel(): void;
118120
* ```
119121
*
120122
* @param callable $onRejected
121-
* @return PromiseInterface
123+
* @return PromiseInterface<T>
122124
* @deprecated 3.0.0 Use catch() instead
123125
* @see self::catch()
124126
*/
@@ -135,7 +137,7 @@ public function otherwise(callable $onRejected): PromiseInterface;
135137
* ```
136138
*
137139
* @param callable $onFulfilledOrRejected
138-
* @return PromiseInterface
140+
* @return PromiseInterface<T>
139141
* @deprecated 3.0.0 Use finally() instead
140142
* @see self::finally()
141143
*/

src/functions.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
*
1818
* If `$promiseOrValue` is a promise, it will be returned as is.
1919
*
20-
* @param mixed $promiseOrValue
21-
* @return PromiseInterface
20+
* @template T
21+
* @param PromiseInterface<T>|T $promiseOrValue
22+
* @return PromiseInterface<T>
2223
*/
2324
function resolve($promiseOrValue): PromiseInterface
2425
{
@@ -30,6 +31,7 @@ function resolve($promiseOrValue): PromiseInterface
3031
$canceller = null;
3132

3233
if (\method_exists($promiseOrValue, 'cancel')) {
34+
/** @var callable $canceller */
3335
$canceller = [$promiseOrValue, 'cancel'];
3436
}
3537

@@ -54,8 +56,7 @@ function resolve($promiseOrValue): PromiseInterface
5456
* throwing an exception. For example, it allows you to propagate a rejection with
5557
* the value of another promise.
5658
*
57-
* @param \Throwable $reason
58-
* @return PromiseInterface
59+
* @return PromiseInterface<never>
5960
*/
6061
function reject(\Throwable $reason): PromiseInterface
6162
{
@@ -68,8 +69,9 @@ function reject(\Throwable $reason): PromiseInterface
6869
* will be an array containing the resolution values of each of the items in
6970
* `$promisesOrValues`.
7071
*
71-
* @param iterable<mixed> $promisesOrValues
72-
* @return PromiseInterface
72+
* @template T
73+
* @param iterable<PromiseInterface<T>|T> $promisesOrValues
74+
* @return PromiseInterface<array<T>>
7375
*/
7476
function all(iterable $promisesOrValues): PromiseInterface
7577
{
@@ -119,8 +121,9 @@ function (\Throwable $reason) use (&$continue, $reject): void {
119121
* The returned promise will become **infinitely pending** if `$promisesOrValues`
120122
* contains 0 items.
121123
*
122-
* @param iterable<mixed> $promisesOrValues
123-
* @return PromiseInterface
124+
* @template T
125+
* @param iterable<PromiseInterface<T>|T> $promisesOrValues
126+
* @return PromiseInterface<T>
124127
*/
125128
function race(iterable $promisesOrValues): PromiseInterface
126129
{
@@ -154,8 +157,9 @@ function race(iterable $promisesOrValues): PromiseInterface
154157
* The returned promise will also reject with a `React\Promise\Exception\LengthException`
155158
* if `$promisesOrValues` contains 0 items.
156159
*
157-
* @param iterable<mixed> $promisesOrValues
158-
* @return PromiseInterface
160+
* @template T
161+
* @param iterable<PromiseInterface<T>|T> $promisesOrValues
162+
* @return PromiseInterface<T>
159163
*/
160164
function any(iterable $promisesOrValues): PromiseInterface
161165
{

tests/FunctionAllTest.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class FunctionAllTest extends TestCase
1010
public function shouldResolveEmptyInput(): void
1111
{
1212
$mock = $this->createCallableMock();
13-
$mock
14-
->expects(self::once())
13+
$mock->expects(self::once())
1514
->method('__invoke')
1615
->with(self::identicalTo([]));
1716

@@ -23,8 +22,7 @@ public function shouldResolveEmptyInput(): void
2322
public function shouldResolveValuesArray(): void
2423
{
2524
$mock = $this->createCallableMock();
26-
$mock
27-
->expects(self::once())
25+
$mock->expects(self::once())
2826
->method('__invoke')
2927
->with(self::identicalTo([1, 2, 3]));
3028

@@ -36,8 +34,7 @@ public function shouldResolveValuesArray(): void
3634
public function shouldResolvePromisesArray(): void
3735
{
3836
$mock = $this->createCallableMock();
39-
$mock
40-
->expects(self::once())
37+
$mock->expects(self::once())
4138
->method('__invoke')
4239
->with(self::identicalTo([1, 2, 3]));
4340

@@ -49,8 +46,7 @@ public function shouldResolvePromisesArray(): void
4946
public function shouldResolveSparseArrayInput(): void
5047
{
5148
$mock = $this->createCallableMock();
52-
$mock
53-
->expects(self::once())
49+
$mock->expects(self::once())
5450
->method('__invoke')
5551
->with(self::identicalTo([null, 1, null, 1, 1]));
5652

@@ -62,8 +58,7 @@ public function shouldResolveSparseArrayInput(): void
6258
public function shouldResolveValuesGenerator(): void
6359
{
6460
$mock = $this->createCallableMock();
65-
$mock
66-
->expects(self::once())
61+
$mock->expects(self::once())
6762
->method('__invoke')
6863
->with(self::identicalTo([1, 2, 3]));
6964

@@ -80,8 +75,7 @@ public function shouldResolveValuesGenerator(): void
8075
public function shouldResolveValuesGeneratorEmpty(): void
8176
{
8277
$mock = $this->createCallableMock();
83-
$mock
84-
->expects(self::once())
78+
$mock->expects(self::once())
8579
->method('__invoke')
8680
->with(self::identicalTo([]));
8781

@@ -132,8 +126,7 @@ public function shouldRejectInfiteGeneratorOrRejectedPromises(): void
132126
public function shouldPreserveTheOrderOfArrayWhenResolvingAsyncPromises(): void
133127
{
134128
$mock = $this->createCallableMock();
135-
$mock
136-
->expects(self::once())
129+
$mock->expects(self::once())
137130
->method('__invoke')
138131
->with(self::identicalTo([1, 2, 3]));
139132

0 commit comments

Comments
 (0)