Skip to content

Conversation

@WyriHaximus
Copy link
Member

@WyriHaximusWyriHaximus commented Mar 24, 2022

These annotations will aid static analyses like PHPStan and Psalm to enhance type-safety for this project and projects depending on it

These changes make the following example understandable by PHPStan:

finalreadonlyclass User{publicfunction__construct( publicstring$name, ) } /** * \React\Promise\PromiseInterface<User> */functiongetCurrentUserFromDatabase(): \React\Promise\PromiseInterface{// The following line would do the database query and fetch the result from it// but keeping it simple for the sake of the example.return\React\Promise\resolve(newUser('WyriHaximus'))} // For the sake of this example we're going to assume the following code runs// in \React\Async\async callechoawait(getCurrentUserFromDatabase())->name; // This echos: WyriHaximus

This PR builds on the discussion at vimeo/psalm#7559 and the following PR's
reactphp/promise#247, reactphp/promise#246, and others down the line.

@WyriHaximusWyriHaximus added this to the v4.0.0 milestone Mar 24, 2022
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 21, 2022
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%. This PR is a requirement for reactphp/async#40
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 21, 2022
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%. This PR is a requirement for reactphp/async#40
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 22, 2022
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
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 22, 2022
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
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 23, 2022
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
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 23, 2022
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
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 23, 2022
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
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 23, 2022
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
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 29, 2022
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
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jun 29, 2022
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
@WyriHaximusWyriHaximus changed the title [WIP] [4.x] Add template annotations[4.x] Add template annotations to async and awaitJul 8, 2022
@WyriHaximusWyriHaximus requested a review from clueJuly 8, 2022 19:55
@WyriHaximusWyriHaximus marked this pull request as ready for review July 8, 2022 19:55
@WyriHaximusWyriHaximusforce-pushed the 4.x-add-template-annotations branch 5 times, most recently from 243d3e0 to e9c6e86CompareJuly 9, 2022 13:37
@clueclue added the new feature New feature or request label Jul 9, 2022
@WyriHaximusWyriHaximusforce-pushed the 4.x-add-template-annotations branch from e9c6e86 to 6e7887fCompareJuly 9, 2022 20:35
@WyriHaximus
Copy link
MemberAuthor

@clue I assume most of the location related comments also go for the promise counter parts?

@WyriHaximusWyriHaximusforce-pushed the 4.x-add-template-annotations branch from 6e7887f to 2999337CompareJuly 9, 2022 21:19
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jul 9, 2022
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
WyriHaximus added a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jul 9, 2022
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
@WyriHaximusWyriHaximusforce-pushed the 4.x-add-template-annotations branch from 2999337 to f097d2eCompareJuly 9, 2022 22:09
clue pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jul 11, 2023
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
clue pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request Jul 11, 2023
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
@WyriHaximusWyriHaximusforce-pushed the 4.x-add-template-annotations branch 2 times, most recently from bbd5379 to 45dae63CompareJuly 11, 2023 20:26
@WyriHaximusWyriHaximus changed the title [4.x] Add template annotations to async and await[4.x] Add template annotationsJul 12, 2023
@WyriHaximusWyriHaximus added this to the v4.2.0 milestone Jul 22, 2023
@WyriHaximusWyriHaximusforce-pushed the 4.x-add-template-annotations branch 6 times, most recently from a8481f9 to 2930a8eCompareJuly 22, 2023 09:23
@WyriHaximusWyriHaximusforce-pushed the 4.x-add-template-annotations branch 2 times, most recently from e50d673 to fe528f8CompareAugust 8, 2023 06:20
@WyriHaximusWyriHaximusforce-pushed the 4.x-add-template-annotations branch 4 times, most recently from 5e42fbd to 54b62fbCompareAugust 29, 2023 07:16
These annotations will aid static analyses like PHPStan and Psalm to enhance type-safety for this project and projects depending on it These changes make the following example understandable by PHPStan: ```php final readonly class User{public function __construct( public string $name, ) } /** * \React\Promise\PromiseInterface<User> */ function getCurrentUserFromDatabase(): \React\Promise\PromiseInterface{// The following line would do the database query and fetch the result from it // but keeping it simple for the sake of the example. return \React\Promise\resolve(new User('WyriHaximus'))} // For the sake of this example we're going to assume the following code runs // in \React\Async\async call echo await(getCurrentUserFromDatabase())->name; // This echos: WyriHaximus ```
@clueclueforce-pushed the 4.x-add-template-annotations branch from 54b62fb to 643316aCompareOctober 27, 2023 15:27
Copy link
Member

@clueclue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WyriHaximus Went over your changes together with @SimonFrings and addressed all outstanding issues, great work, now let's get this shipped! :shipit::shipit::shipit:

* @param callable $function
* @return callable(mixed ...): PromiseInterface<mixed>
* @template T
* @template A1 (any number of function arguments, see https://github.com/phpstan/phpstan/issues/8214)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"640K ought to be enough for anybody."

See phpstan/phpstan#8214 for longer-term solution once supported in PHPStan.

Copy link
Member

@SimonFringsSimonFrings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one 👍

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new featureNew feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@WyriHaximus@clue@SimonFrings