Skip to content

Commit

Permalink
Merge pull request #89 from clue-labs/templates-v3
Browse files Browse the repository at this point in the history
[3.x] Improve type safety for test environment
  • Loading branch information
WyriHaximus authored Aug 27, 2024
2 parents bc3ef67 + 3404f63 commit 226b3f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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

0 comments on commit 226b3f2

Please sign in to comment.