Skip to content

Commit 0bc6148

Browse files
committed
Add basic template annotations
This adds basic type safety annotations for static analyzers like PHPStan and Psalm. This will cover around 80% of the use cases and a follow-up PR for all supported versions will be proposed later to get it to a 100% of close to a 100%. By adding these annotations methods returning a promise can hint their resolving type by adding `@return PromiseInterface<bool>` when they for example resolve to a boolean. By doing that Psalm and PHPStan will understand that the following bit of code will not become an issue because the method's contract promised a boolean through the promise: ```php $promise->then(static function (bool $isEnabled) {}); ``` However, the following will yield errors: ```php $promise->then(static function (string $isEnabled) {}); ``` This PR is a requirement for reactphp/async#40
1 parent cbd7f05 commit 0bc6148

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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
/phpunit.xml.legacy export-ignore
67
/tests/ export-ignore
8+
/types/ export-ignore

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,17 @@ jobs:
4444
- run: composer self-update --2.2 # downgrade Composer for HHVM
4545
- run: hhvm $(which composer) install
4646
- run: hhvm vendor/bin/phpunit
47+
48+
PHPStan:
49+
name: PHPStan
50+
runs-on: ubuntu-20.04
51+
strategy:
52+
matrix:
53+
php:
54+
- 8.1
55+
steps:
56+
- uses: actions/checkout@v3
57+
- uses: shivammathur/setup-php@v2
58+
with:
59+
php-version: ${{ matrix.php }}
60+
- run: vendor/bin/phpstan

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/React/Promise/PromiseInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
namespace React\Promise;
44

5+
/** @template T */
56
interface PromiseInterface
67
{
8+
/**
9+
* @template TReturn of mixed
10+
* @param callable(T): TReturn $fulfilledHandler
11+
* @return (TReturn is PromiseInterface ? TReturn : PromiseInterface<TReturn>)
12+
*/
713
public function then($fulfilledHandler = null, $errorHandler = null, $progressHandler = null);
814
}

types/PromiseInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use function PHPStan\Testing\assertType;
4+
use function React\Promise\resolve;
5+
6+
$passThroughBoolFn = static fn (bool $bool): bool => $bool;
7+
8+
assertType('React\Promise\PromiseInterface<bool>', resolve(true));
9+
assertType('React\Promise\PromiseInterface<bool>', resolve(true)->then($passThroughBoolFn));

0 commit comments

Comments
 (0)