Skip to content

Commit

Permalink
Add template annotations
Browse files Browse the repository at this point in the history
These annotations will aid static analyses like PHPStan and Psalm to enhance type-safety for this project and projects depending on it
  • Loading branch information
WyriHaximus committed Jul 8, 2022
1 parent 4dae336 commit 243d3e0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ jobs:
coverage: xdebug
- run: composer install
- run: vendor/bin/phpunit --coverage-text

static-analysis:
name: PHPStan
runs-on: ubuntu-20.04
continue-on-error: true
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: 8.1
- run: composer require phpstan/phpstan
- name: Execute type checking
run: vendor/bin/phpstan --configuration="phpstan.types.neon.dist"
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@
"require": {
"php": ">=8.1",
"react/event-loop": "^1.2",
"react/promise": "^3.0 || ^2.8 || ^1.2.1"
"react/promise": "dev-2.x-add-basic-template-annotations as 2.13.0"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/WyriHaximus-secret-labs/promise"
}
],
"require-dev": {
"phpunit/phpunit": "^9.3"
},
Expand Down
4 changes: 4 additions & 0 deletions phpstan.types.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
paths:
- types
level: max
11 changes: 6 additions & 5 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@
* $promise->cancel();
* await($promise);
* ```
*
* @param callable(mixed ...$args):mixed $function
* @return callable(): PromiseInterface<mixed>
* @template T
* @param callable(): T $function
* @return callable(): PromiseInterface<T>
* @since 4.0.0
* @see coroutine()
*/
Expand Down Expand Up @@ -266,8 +266,9 @@ function async(callable $function): callable
* }
* ```
*
* @param PromiseInterface $promise
* @return mixed returns whatever the promise resolves to
* @template T
* @param PromiseInterface<T> $promise
* @return T
* @throws \Exception when the promise is rejected with an `Exception`
* @throws \Throwable when the promise is rejected with a `Throwable`
* @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only)
Expand Down
10 changes: 10 additions & 0 deletions types/await.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use function PHPStan\Testing\assertType;
use function React\Async\await;
use function React\Promise\resolve;

$passThroughBoolFn = static fn (bool $bool): bool => $bool;

assertType('bool', await(resolve(true)));
assertType('bool', await(resolve(true)->then($passThroughBoolFn)));

0 comments on commit 243d3e0

Please sign in to comment.