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] Improve type safety for test environment #89

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ function coroutine(callable $function, ...$args): PromiseInterface
}

$promise = null;
/** @var Deferred<T> $deferred*/
$deferred = new Deferred(function () use (&$promise) {
/** @var ?PromiseInterface<T> $promise */
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
Expand Down Expand Up @@ -381,6 +382,7 @@ function parallel(iterable $tasks): PromiseInterface
{
/** @var array<int,PromiseInterface<T>> $pending */
$pending = [];
/** @var Deferred<array<T>> $deferred */
$deferred = new Deferred(function () use (&$pending) {
foreach ($pending as $promise) {
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
Expand Down Expand Up @@ -430,6 +432,7 @@ function parallel(iterable $tasks): PromiseInterface
$deferred->resolve($results);
}

/** @var PromiseInterface<array<T>> Remove once defining `Deferred()` above is supported by PHPStan, see https://github.com/phpstan/phpstan/issues/11032 */
return $deferred->promise();
}

Expand All @@ -441,6 +444,7 @@ function parallel(iterable $tasks): PromiseInterface
function series(iterable $tasks): PromiseInterface
{
$pending = null;
/** @var Deferred<array<T>> $deferred */
$deferred = new Deferred(function () use (&$pending) {
/** @var ?PromiseInterface<T> $pending */
if ($pending instanceof PromiseInterface && \method_exists($pending, 'cancel')) {
Expand Down Expand Up @@ -485,6 +489,7 @@ function series(iterable $tasks): PromiseInterface

$next();

/** @var PromiseInterface<array<T>> Remove once defining `Deferred()` above is supported by PHPStan, see https://github.com/phpstan/phpstan/issues/11032 */
return $deferred->promise();
}

Expand All @@ -496,6 +501,7 @@ function series(iterable $tasks): PromiseInterface
function waterfall(iterable $tasks): PromiseInterface
{
$pending = null;
/** @var Deferred<T> $deferred*/
$deferred = new Deferred(function () use (&$pending) {
/** @var ?PromiseInterface<T> $pending */
if ($pending instanceof PromiseInterface && \method_exists($pending, 'cancel')) {
Expand Down
6 changes: 3 additions & 3 deletions tests/AwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
}

$promise = new Promise(function ($_, $reject) {
$reject(false);
$reject(false); // @phpstan-ignore-line
});

$this->expectException(\UnexpectedValueException::class);
Expand All @@ -41,7 +41,7 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
}

$promise = new Promise(function ($_, $reject) {
$reject(null);
$reject(null); // @phpstan-ignore-line
});

$this->expectException(\UnexpectedValueException::class);
Expand Down Expand Up @@ -278,7 +278,7 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
gc_collect_cycles();

$promise = new Promise(function ($_, $reject) {
$reject(null);
$reject(null); // @phpstan-ignore-line
});
try {
React\Async\await($promise);
Expand Down