Skip to content

Commit

Permalink
Use Promise v3 template types
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed May 17, 2024
1 parent 7c3738e commit 1400c28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function async(callable $function): callable
{
return static function (mixed ...$args) use ($function): PromiseInterface {
$fiber = null;
/** @var PromiseInterface<T> $promise*/
$promise = new Promise(function (callable $resolve, callable $reject) use ($function, $args, &$fiber): void {
$fiber = new \Fiber(function () use ($resolve, $reject, $function, $args, &$fiber): void {
try {
Expand Down Expand Up @@ -627,6 +628,7 @@ function coroutine(callable $function, mixed ...$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 @@ -734,6 +736,7 @@ function parallel(iterable $tasks): PromiseInterface
$deferred->resolve($results);
}

/** @var PromiseInterface<array<T>> Can't define `Deferred()` above, currently not supported by PHPStan, see https://github.com/phpstan/phpstan/issues/11032 */
return $deferred->promise();
}

Expand Down Expand Up @@ -789,6 +792,7 @@ function series(iterable $tasks): PromiseInterface

$next();

/** @var PromiseInterface<array<T>> Can't define `Deferred()` above, currently not supported by PHPStan, see https://github.com/phpstan/phpstan/issues/11032 */
return $deferred->promise();
}

Expand All @@ -800,6 +804,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 @@ -129,7 +129,7 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
}

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

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

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

try {
Expand Down Expand Up @@ -331,7 +331,7 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
gc_collect_cycles();

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

0 comments on commit 1400c28

Please sign in to comment.