Skip to content

Commit 0e7e7a8

Browse files
committed
WIP
1 parent b87eda7 commit 0e7e7a8

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

src/PromiseInterface.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace React\Promise;
44

55
/**
6-
* @template T
7-
* @template R
6+
* @template-covariant T
7+
* @template-covariant R
88
*/
99
interface PromiseInterface
1010
{
@@ -36,15 +36,11 @@ interface PromiseInterface
3636
* than once.
3737
* 3. `$onProgress` (deprecated) may be called multiple times.
3838
*
39-
* @template TFulfilled of mixed
40-
* @template TRejected of mixed
41-
* @param (callable(T): (PromiseInterface<TFulfilled>|TFulfilled))|null $onFulfilled
42-
* @param (callable(R): (PromiseInterface<TRejected>|TRejected))|null $onRejected
43-
* @return PromiseInterface<(
44-
* $onFulfilled is not null
45-
* ? ($onRejected is not null ? TFulfilled|TRejected : TFulfilled)
46-
* : ($onRejected is not null ? TRejected : R)
47-
* )>
39+
* @template TFulfilled as PromiseInterface<T>|T
40+
* @template TRejected as \Throwable
41+
* @param callable(T): TFulfilled $onFulfilled
42+
* @param callable(R): TRejected $onRejected
43+
* @return (TFulfilled is PromiseInterface ? TFulfilled : PromiseInterface<TFulfilled, TRejected>)
4844
*/
4945
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null);
5046
}

src/functions.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @template T
1717
* @param T $promiseOrValue
18-
* @return PromiseInterface<T>
18+
* @return (T is PromiseInterface ? T : PromiseInterface<T>)
1919
*/
2020
function resolve($promiseOrValue = null)
2121
{
@@ -53,8 +53,10 @@ function resolve($promiseOrValue = null)
5353
* throwing an exception. For example, it allows you to propagate a rejection with
5454
* the value of another promise.
5555
*
56-
* @param mixed $promiseOrValue
57-
* @return PromiseInterface
56+
* @template T is null
57+
* @template R
58+
* @param R $promiseOrValue
59+
* @return (R is PromiseInterface ? R : PromiseInterface<T, R>)
5860
*/
5961
function reject($promiseOrValue = null)
6062
{

types/Promises.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,34 @@
1414
$passThroughThrowable = static function (Throwable $t): PromiseInterface {
1515
return reject($t);
1616
};
17+
$stringOrInt = function (): string|int {
18+
return time() % 2 ? 'string' : time();
19+
};
1720
$tosseable = new Exception('Oops I did it again!');
1821

1922
assertType('React\Promise\PromiseInterface<bool>', resolve(true));
20-
assertType('React\Promise\PromiseInterface<array<bool>>', all([resolve(true), resolve(false)]));
21-
assertType('React\Promise\PromiseInterface<array<bool>>', all([resolve(true), false]));
22-
assertType('React\Promise\PromiseInterface<array<bool|int>>', all([resolve(true), resolve(time())]));
23-
assertType('React\Promise\PromiseInterface<array<bool|int>>', all([resolve(true), time()]));
24-
assertType('React\Promise\PromiseInterface<array<bool|int>>', all([true, resolve(time())]));
25-
assertType('React\Promise\PromiseInterface<bool>', race([resolve(true), resolve(true)]));
26-
assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then($passThroughBoolFn));
27-
assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then()->then($passThroughBoolFn));
28-
assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then(null)->then($passThroughBoolFn));
29-
assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then($passThroughBoolFn)->then($passThroughBoolFn));
30-
assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then($passThroughBoolFn, $passThroughThrowable)->then($passThroughBoolFn));
23+
//assertType('React\Promise\PromiseInterface<string|int>', resolve($stringOrInt()));
24+
assertType('React\Promise\PromiseInterface<int|string>', resolve($stringOrInt()));
25+
assertType('React\Promise\PromiseInterface<bool>', resolve(resolve(true)));
26+
//assertType('React\Promise\PromiseInterface<array<bool>>', all([resolve(true), resolve(false)]));
27+
//assertType('React\Promise\PromiseInterface<array<bool>>', all([resolve(true), false]));
28+
//assertType('React\Promise\PromiseInterface<array<bool|int>>', all([resolve(true), resolve(time())]));
29+
//assertType('React\Promise\PromiseInterface<array<bool|float>>', all([resolve(true), hrtime()]));
30+
//assertType('React\Promise\PromiseInterface<array<bool|int>>', all([true, resolve(time())]));
31+
//assertType('React\Promise\PromiseInterface<bool>', race([resolve(true), resolve(true)]));
32+
//assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then($passThroughBoolFn));
33+
//assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then()->then($passThroughBoolFn));
34+
//assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then(null)->then($passThroughBoolFn));
35+
//assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then($passThroughBoolFn)->then($passThroughBoolFn));
36+
//assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then($passThroughBoolFn, $passThroughThrowable)->then($passThroughBoolFn));
3137
assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then(null, $passThroughThrowable)->then($passThroughBoolFn));
32-
assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then()->then(null, $passThroughThrowable)->then($passThroughBoolFn));
33-
assertType('React\Promise\FulfilledPromise<bool>', new FulfilledPromise(true));
34-
assertType('React\Promise\PromiseInterface<bool>', (new FulfilledPromise(true))->then($passThroughBoolFn));
38+
//assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then()->then(null, $passThroughThrowable)->then($passThroughBoolFn));
39+
//assertType('React\Promise\FulfilledPromise<bool>', new FulfilledPromise(true));
40+
//assertType('React\Promise\PromiseInterface<bool>', (new FulfilledPromise(true))->then($passThroughBoolFn));
3541

36-
//assertType('React\Promise\PromiseInterface<Throwable>', reject($tosseable));
37-
//assertType('React\Promise\PromiseInterface<Throwable>', reject($tosseable)->then($passThroughBoolFn, $passThroughThrowable));
38-
//assertType('React\Promise\PromiseInterface<Throwable>', reject($tosseable)->then()->then($passThroughBoolFn, $passThroughThrowable));
39-
//assertType('React\Promise\PromiseInterface<Throwable>', reject($tosseable)->then(null, $passThroughThrowable));
40-
//assertType('React\Promise\PromiseInterface<Throwable>', reject($tosseable)->then()->then(null, $passThroughThrowable));
42+
//assertType('React\Promise\PromiseInterface<null, Exception>', reject($tosseable));
43+
//assertType('React\Promise\PromiseInterface<null, Throwable>', reject($tosseable));
44+
//assertType('React\Promise\PromiseInterface<null, Throwable>', reject($tosseable)->then($passThroughBoolFn, $passThroughThrowable));
45+
//assertType('React\Promise\PromiseInterface<null, Throwable>', reject($tosseable)->then()->then($passThroughBoolFn, $passThroughThrowable));
46+
//assertType('React\Promise\PromiseInterface<null, Throwable>', reject($tosseable)->then(null, $passThroughThrowable));
47+
//assertType('React\Promise\PromiseInterface<null, Throwable>', reject($tosseable)->then()->then(null, $passThroughThrowable));

0 commit comments

Comments
 (0)