forked from reactphp/promise
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 reactphp#246 and reactphp#188 and is a requirement for reactphp/async#40
- Loading branch information
1 parent
d87b562
commit e6dda58
Showing
32 changed files
with
306 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
parameters: | ||
ignoreErrors: | ||
- '#Template type T is declared as covariant, but occurs in contravariant position in parameter result of method React\\Promise\\Promise::settle\(\).#' | ||
- '#Template type T is declared as covariant, but occurs in contravariant position in parameter promise of method React\\Promise\\Promise::unwrap\(\).#' | ||
|
||
includes: | ||
- phpstan.neon.dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
|
||
namespace React\Promise; | ||
|
||
/** | ||
* @template-covariant T | ||
*/ | ||
interface PromiseInterface | ||
{ | ||
/** | ||
|
@@ -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 Fulfilled as PromiseInterface<T>|T | ||
* @param ?(callable(T): (Fulfilled|void)) $onFulfilled | ||
* @return PromiseInterface<T> | ||
*/ | ||
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface; | ||
Check failure on line 38 in src/PromiseInterface.php GitHub Actions / PHPStan (PHP 7.1)
|
||
|
||
|
@@ -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; | ||
|
||
|
@@ -91,8 +93,8 @@ public function catch(callable $onRejected): PromiseInterface; | |
* ->finally('cleanup'); | ||
* ``` | ||
* | ||
* @param callable $onFulfilledOrRejected | ||
* @return PromiseInterface | ||
* @param callable(): (mixed|void) $onFulfilledOrRejected | ||
* @return PromiseInterface<T> | ||
*/ | ||
public function finally(callable $onFulfilledOrRejected): PromiseInterface; | ||
|
||
|
@@ -118,7 +120,7 @@ public function cancel(): void; | |
* ``` | ||
* | ||
* @param callable $onRejected | ||
* @return PromiseInterface | ||
* @return PromiseInterface<T> | ||
* @deprecated 3.0.0 Use catch() instead | ||
* @see self::catch() | ||
*/ | ||
|
@@ -135,7 +137,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() | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.