reactphp / async

Async utilities and fibers for ReactPHP.
https://reactphp.org/async/
MIT License
204 stars 19 forks source link

[4.x] Add template annotations #40

Closed WyriHaximus closed 1 year ago

WyriHaximus commented 2 years ago

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:

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

This PR builds on the discussion at https://github.com/vimeo/psalm/discussions/7559 and the following PR's https://github.com/reactphp/promise/pull/247, https://github.com/reactphp/promise/pull/246, and others down the line.

WyriHaximus commented 2 years ago

@clue I assume most of the location related comments also go for the promise counter parts?

WyriHaximus commented 2 years ago

+1+1+1

Needed to highlight how much I like this change despite all the nit-picking in this review. Keep it up!

Much appreciated @clue . Updated this PR, and https://github.com/reactphp/promise/pull/227 (and https://github.com/reactphp/promise/pull/228) with related changes.