Skip to content

Commit 16d653f

Browse files
authored
Merge pull request #86 from SimonFrings/tempalte_types
[4.x] Use Promise v3 template types
2 parents 7c3738e + 6a15425 commit 16d653f

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/functions.php

+7
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ function async(callable $function): callable
191191
{
192192
return static function (mixed ...$args) use ($function): PromiseInterface {
193193
$fiber = null;
194+
/** @var PromiseInterface<T> $promise*/
194195
$promise = new Promise(function (callable $resolve, callable $reject) use ($function, $args, &$fiber): void {
195196
$fiber = new \Fiber(function () use ($resolve, $reject, $function, $args, &$fiber): void {
196197
try {
@@ -627,6 +628,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
627628
}
628629

629630
$promise = null;
631+
/** @var Deferred<T> $deferred*/
630632
$deferred = new Deferred(function () use (&$promise) {
631633
/** @var ?PromiseInterface<T> $promise */
632634
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
@@ -685,6 +687,7 @@ function parallel(iterable $tasks): PromiseInterface
685687
{
686688
/** @var array<int,PromiseInterface<T>> $pending */
687689
$pending = [];
690+
/** @var Deferred<array<T>> $deferred */
688691
$deferred = new Deferred(function () use (&$pending) {
689692
foreach ($pending as $promise) {
690693
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
@@ -734,6 +737,7 @@ function parallel(iterable $tasks): PromiseInterface
734737
$deferred->resolve($results);
735738
}
736739

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

@@ -745,6 +749,7 @@ function parallel(iterable $tasks): PromiseInterface
745749
function series(iterable $tasks): PromiseInterface
746750
{
747751
$pending = null;
752+
/** @var Deferred<array<T>> $deferred */
748753
$deferred = new Deferred(function () use (&$pending) {
749754
/** @var ?PromiseInterface<T> $pending */
750755
if ($pending instanceof PromiseInterface && \method_exists($pending, 'cancel')) {
@@ -789,6 +794,7 @@ function series(iterable $tasks): PromiseInterface
789794

790795
$next();
791796

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

@@ -800,6 +806,7 @@ function series(iterable $tasks): PromiseInterface
800806
function waterfall(iterable $tasks): PromiseInterface
801807
{
802808
$pending = null;
809+
/** @var Deferred<T> $deferred*/
803810
$deferred = new Deferred(function () use (&$pending) {
804811
/** @var ?PromiseInterface<T> $pending */
805812
if ($pending instanceof PromiseInterface && \method_exists($pending, 'cancel')) {

tests/AwaitTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
129129
}
130130

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

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

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

153153
try {
@@ -331,7 +331,7 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
331331
gc_collect_cycles();
332332

333333
$promise = new Promise(function ($_, $reject) {
334-
$reject(null);
334+
$reject(null); // @phpstan-ignore-line
335335
});
336336
try {
337337
$await($promise);

0 commit comments

Comments
 (0)