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

[3.x] Add template annotations #247

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

WyriHaximus
Copy link
Member

@WyriHaximus WyriHaximus commented Jun 24, 2023

Adds template annotations turning the PromiseInterface into a generic.

Variables $p1 and $p2 in the following code example both are PromiseInterface<int|string>.

$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:

$p2->then(static function (int|string $a) {});
$p2->then(static function (bool $a) {});

Builds on top of #246 and #188 and is a requirement for reactphp/async#40

@WyriHaximus WyriHaximus force-pushed the 3.0-templates branch 19 times, most recently from c6131bf to e48bb63 Compare June 28, 2023 14:49
@WyriHaximus WyriHaximus added this to the v3.0.0 milestone Jun 28, 2023
@WyriHaximus WyriHaximus marked this pull request as ready for review June 28, 2023 14:49
Copy link
Member

@clue clue left a comment

Choose a reason for hiding this comment

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

@WyriHaximus Solid progress! 👍

For context: We've had a call together with @WyriHaximus and @SimonFrings earlier today, discussing our progress for template annotations and unhandled rejections. We've discussed most of these points internally already, but figured it makes sense to post here as well for transparency.

.github/workflows/ci.yml Outdated Show resolved Hide resolved
phpstan.legacy.neon.dist Outdated Show resolved Hide resolved
phpstan.neon.dist Outdated Show resolved Hide resolved
src/Deferred.php Show resolved Hide resolved
src/Internal/RejectedPromise.php Outdated Show resolved Hide resolved
tests/Internal/RejectedPromiseTest.php Outdated Show resolved Hide resolved
tests/PromiseAdapter/CallbackPromiseAdapter.php Outdated Show resolved Hide resolved
tests/PromiseAdapter/PromiseAdapterInterface.php Outdated Show resolved Hide resolved
tests/PromiseTest/PromiseFulfilledTestTrait.php Outdated Show resolved Hide resolved
types/Promises.php Outdated Show resolved Hide resolved
@WyriHaximus WyriHaximus force-pushed the 3.0-templates branch 4 times, most recently from 6772ffb to bb7d812 Compare July 4, 2023 13:28
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
Copy link
Member

@clue clue 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:

@WyriHaximus WyriHaximus merged commit d317026 into reactphp:3.x Jul 11, 2023
13 checks passed
@WyriHaximus
Copy link
Member Author

WyriHaximus commented Jul 11, 2023

@clue @SimonFrings Thank you for picking up and finishing this off and having my back in these challenging personal times ❤️ .

@WyriHaximus WyriHaximus deleted the 3.0-templates branch July 11, 2023 12:14
@clue
Copy link
Member

clue commented Jul 11, 2023

@WyriHaximus No problem, we're here! Happy to support you during tough times. Take care and stay strong!

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.

None yet

3 participants