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 #82

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

clue
Copy link
Member

@clue clue commented Oct 29, 2023

This changeset backports #40 from 4.x to 3.x to add type annotations to aid with static analysis. Most changes have been applied as-is, but v3 requires a number of workarounds to support legacy PHP versions.

If this PR is merged, we could backport most of the changes from v3 to v2. The changes are somewhat similar across all versions, but v2 still supports legacy PHP and doesn't currently employ PHPStan as discussed in #77. Let's continue this discussion outside of this PR.

Builds on top of #40 and #77

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 added the new feature New feature or request label Oct 29, 2023
@clue clue added this to the v3.2.0 milestone Oct 29, 2023
Copy link
Member

@WyriHaximus WyriHaximus left a comment

Choose a reason for hiding this comment

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

:shipit:

@WyriHaximus WyriHaximus merged commit c41af0c into reactphp:3.x Nov 3, 2023
13 checks passed
@clue clue deleted the template-types-v3 branch November 3, 2023 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants