Skip to content

Commit 2999337

Browse files
committed
Add template annotations to async and await
These annotations will aid static analyses like PHPStan and Psalm to enhance type-safety for this project and projects depending on it
1 parent 4dae336 commit 2999337

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/.gitattributes export-ignore
22
/.github/ export-ignore
33
/.gitignore export-ignore
4+
/phpstan.neon.dist export-ignore
45
/phpunit.xml.dist export-ignore
56
/tests/ export-ignore
7+
/types/ export-ignore

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ jobs:
2020
coverage: xdebug
2121
- run: composer install
2222
- run: vendor/bin/phpunit --coverage-text
23+
24+
PHPStan:
25+
name: PHPStan
26+
runs-on: ubuntu-20.04
27+
steps:
28+
- uses: actions/checkout@v3
29+
- uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: 8.1
32+
- run: vendor/bin/phpstan

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"react/promise": "^3.0 || ^2.8 || ^1.2.1"
3232
},
3333
"require-dev": {
34-
"phpunit/phpunit": "^9.3"
34+
"phpunit/phpunit": "^9.3",
35+
"phpstan/phpstan": "^1.8"
3536
},
3637
"autoload": {
3738
"psr-4": {

phpstan.neon.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
paths:
3+
- types
4+
level: max

src/functions.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@
174174
* $promise->cancel();
175175
* await($promise);
176176
* ```
177-
*
178-
* @param callable(mixed ...$args):mixed $function
179-
* @return callable(): PromiseInterface<mixed>
177+
* @template T
178+
* @param callable(): T $function
179+
* @return callable(mixed ...$args): PromiseInterface<T>
180180
* @since 4.0.0
181181
* @see coroutine()
182182
*/
@@ -266,8 +266,9 @@ function async(callable $function): callable
266266
* }
267267
* ```
268268
*
269-
* @param PromiseInterface $promise
270-
* @return mixed returns whatever the promise resolves to
269+
* @template T
270+
* @param PromiseInterface<T> $promise
271+
* @return T
271272
* @throws \Exception when the promise is rejected with an `Exception`
272273
* @throws \Throwable when the promise is rejected with a `Throwable`
273274
* @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only)

types/await.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use function PHPStan\Testing\assertType;
4+
use function React\Async\await;
5+
use function React\Promise\resolve;
6+
7+
assertType('bool', await(resolve(true)));

0 commit comments

Comments
 (0)