Skip to content

Commit

Permalink
Merge pull request #86 from SimonFrings/tempalte_types
Browse files Browse the repository at this point in the history
[4.x] Use Promise v3 template types
  • Loading branch information
WyriHaximus authored May 22, 2024
2 parents 7c3738e + 6a15425 commit 16d653f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 7 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 @@ -685,6 +687,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 @@ -734,6 +737,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 @@ -745,6 +749,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 @@ -789,6 +794,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 @@ -800,6 +806,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 16d653f

Please sign in to comment.