Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Add basic template annotations #228

Conversation

WyriHaximus
Copy link
Member

@WyriHaximus WyriHaximus commented 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%.

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:

$promise->then(static function (bool $isEnabled) {});

However, the following will yield errors:

$promise->then(static function (string $isEnabled) {});

This PR is a requirement for reactphp/async#40

@WyriHaximus WyriHaximus added this to the v1.2.2 milestone Jun 21, 2022
@WyriHaximus WyriHaximus requested review from clue and jsor June 21, 2022 21:52
@WyriHaximus WyriHaximus force-pushed the 1.x-add-basic-template-annotations branch from 4cd23dc to 36b9d11 Compare June 22, 2022 06:48
@WyriHaximus WyriHaximus force-pushed the 1.x-add-basic-template-annotations branch from 36b9d11 to 0bc6148 Compare July 9, 2022 22:35
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 WyriHaximus force-pushed the 1.x-add-basic-template-annotations branch from 0bc6148 to d152ae2 Compare July 9, 2022 22:38
@WyriHaximus
Copy link
Member Author

Closing this in favor of a PR in the fashion of #223.

@WyriHaximus WyriHaximus closed this May 2, 2023
@SimonFrings SimonFrings modified the milestones: v1.2.2, v1.3.0 Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants