[4.x] Add template annotations#40
Merged
WyriHaximus merged 1 commit intoreactphp:4.xfrom Oct 27, 2023
Merged
Conversation
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
This was referenced Jun 21, 2022
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
243d3e0 to
e9c6e86
Compare
clue
reviewed
Jul 9, 2022
e9c6e86 to
6e7887f
Compare
Member
Author
|
@clue I assume most of the location related comments also go for the promise counter parts? |
6e7887f to
2999337
Compare
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
2999337 to
f097d2e
Compare
WyriHaximus
commented
Jul 9, 2022
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
bbd5379 to
45dae63
Compare
a8481f9 to
2930a8e
Compare
e50d673 to
fe528f8
Compare
5e42fbd to
54b62fb
Compare
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
```
54b62fb to
643316a
Compare
clue
approved these changes
Oct 27, 2023
Member
clue
left a comment
There was a problem hiding this comment.
@WyriHaximus Went over your changes together with @SimonFrings and addressed all outstanding issues, great work, now let's get this shipped!
![]()
clue
reviewed
Oct 27, 2023
| * @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) |
Member
There was a problem hiding this comment.
"640K ought to be enough for anybody."
See phpstan/phpstan#8214 for longer-term solution once supported in PHPStan.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.