Skip to content

Commit 36b9d11

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 36b9d11

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

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
}

0 commit comments

Comments
 (0)