WyriHaximus · GitHub

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

Closed

Closed

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

@WyriHaximus WyriHaximus changed the title [WIP] [4.x] Add template annotations [4.x] Add template annotations to async and await

Jul 8, 2022

@WyriHaximus

@WyriHaximus

@WyriHaximus

clue

@WyriHaximus

@WyriHaximus

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

@WyriHaximus

WyriHaximus

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 8, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 8, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 8, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 8, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 8, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 8, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus pushed a commit to WyriHaximus-secret-labs/promise that referenced this pull request

Jul 9, 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

WyriHaximus

WyriHaximus

Closed

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
```

clue

clue approved these changes Oct 27, 2023

clue

SimonFrings

@clue clue mentioned this pull request

Oct 29, 2023

Merged

@clue clue mentioned this pull request

Dec 6, 2023

Merged

@clue clue mentioned this pull request

Dec 17, 2023

Merged

@clue clue mentioned this pull request

Mar 14, 2025

Merged

Read the original on github.com ↗