44
44
* }
45
45
* ```
46
46
*
47
- * @param PromiseInterface $promise
48
- * @return mixed returns whatever the promise resolves to
47
+ * @template T
48
+ * @param PromiseInterface<T> $promise
49
+ * @return T returns whatever the promise resolves to
49
50
* @throws \Exception when the promise is rejected with an `Exception`
50
51
* @throws \Throwable when the promise is rejected with a `Throwable`
51
52
* @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only)
@@ -93,13 +94,14 @@ function ($error) use (&$exception, &$rejected, &$wait, &$loopStarted) {
93
94
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
94
95
if (!$ exception instanceof \Throwable) {
95
96
$ exception = new \UnexpectedValueException (
96
- 'Promise rejected with unexpected value of type ' . (is_object ($ exception ) ? get_class ($ exception ) : gettype ($ exception ))
97
+ 'Promise rejected with unexpected value of type ' . (is_object ($ exception ) ? get_class ($ exception ) : gettype ($ exception )) // @phpstan-ignore-line
97
98
);
98
99
}
99
100
100
101
throw $ exception ;
101
102
}
102
103
104
+ /** @var T $resolved */
103
105
return $ resolved ;
104
106
}
105
107
@@ -296,9 +298,16 @@ function delay(float $seconds): void
296
298
* });
297
299
* ```
298
300
*
299
- * @param callable(mixed ...$args):(\Generator<mixed,PromiseInterface,mixed,mixed>|mixed) $function
301
+ * @template T
302
+ * @template TYield
303
+ * @template A1 (any number of function arguments, see https://github.com/phpstan/phpstan/issues/8214)
304
+ * @template A2
305
+ * @template A3
306
+ * @template A4
307
+ * @template A5
308
+ * @param callable(A1, A2, A3, A4, A5):(\Generator<mixed, PromiseInterface<TYield>, TYield, PromiseInterface<T>|T>|PromiseInterface<T>|T) $function
300
309
* @param mixed ...$args Optional list of additional arguments that will be passed to the given `$function` as is
301
- * @return PromiseInterface<mixed >
310
+ * @return PromiseInterface<T >
302
311
* @since 3.0.0
303
312
*/
304
313
function coroutine (callable $ function , ...$ args ): PromiseInterface
@@ -315,7 +324,7 @@ function coroutine(callable $function, ...$args): PromiseInterface
315
324
316
325
$ promise = null ;
317
326
$ deferred = new Deferred (function () use (&$ promise ) {
318
- /** @var ?PromiseInterface $promise */
327
+ /** @var ?PromiseInterface<T> $promise */
319
328
if ($ promise instanceof PromiseInterface && \method_exists ($ promise , 'cancel ' )) {
320
329
$ promise ->cancel ();
321
330
}
@@ -336,7 +345,6 @@ function coroutine(callable $function, ...$args): PromiseInterface
336
345
return ;
337
346
}
338
347
339
- /** @var mixed $promise */
340
348
$ promise = $ generator ->current ();
341
349
if (!$ promise instanceof PromiseInterface) {
342
350
$ next = null ;
@@ -346,6 +354,7 @@ function coroutine(callable $function, ...$args): PromiseInterface
346
354
return ;
347
355
}
348
356
357
+ /** @var PromiseInterface<TYield> $promise */
349
358
assert ($ next instanceof \Closure);
350
359
$ promise ->then (function ($ value ) use ($ generator , $ next ) {
351
360
$ generator ->send ($ value );
@@ -364,12 +373,13 @@ function coroutine(callable $function, ...$args): PromiseInterface
364
373
}
365
374
366
375
/**
367
- * @param iterable<callable():PromiseInterface<mixed>> $tasks
368
- * @return PromiseInterface<array<mixed>>
376
+ * @template T
377
+ * @param iterable<callable():(PromiseInterface<T>|T)> $tasks
378
+ * @return PromiseInterface<array<T>>
369
379
*/
370
380
function parallel (iterable $ tasks ): PromiseInterface
371
381
{
372
- /** @var array<int,PromiseInterface> $pending */
382
+ /** @var array<int,PromiseInterface<T> > $pending */
373
383
$ pending = [];
374
384
$ deferred = new Deferred (function () use (&$ pending ) {
375
385
foreach ($ pending as $ promise ) {
@@ -424,14 +434,15 @@ function parallel(iterable $tasks): PromiseInterface
424
434
}
425
435
426
436
/**
427
- * @param iterable<callable():PromiseInterface<mixed>> $tasks
428
- * @return PromiseInterface<array<mixed>>
437
+ * @template T
438
+ * @param iterable<callable():(PromiseInterface<T>|T)> $tasks
439
+ * @return PromiseInterface<array<T>>
429
440
*/
430
441
function series (iterable $ tasks ): PromiseInterface
431
442
{
432
443
$ pending = null ;
433
444
$ deferred = new Deferred (function () use (&$ pending ) {
434
- /** @var ?PromiseInterface $pending */
445
+ /** @var ?PromiseInterface<T> $pending */
435
446
if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
436
447
$ pending ->cancel ();
437
448
}
@@ -478,14 +489,15 @@ function series(iterable $tasks): PromiseInterface
478
489
}
479
490
480
491
/**
481
- * @param iterable<(callable():PromiseInterface<mixed>)|(callable(mixed):PromiseInterface<mixed>)> $tasks
482
- * @return PromiseInterface<mixed>
492
+ * @template T
493
+ * @param iterable<(callable():(PromiseInterface<T>|T))|(callable(mixed):(PromiseInterface<T>|T))> $tasks
494
+ * @return PromiseInterface<($tasks is non-empty-array|\Traversable ? T : null)>
483
495
*/
484
496
function waterfall (iterable $ tasks ): PromiseInterface
485
497
{
486
498
$ pending = null ;
487
499
$ deferred = new Deferred (function () use (&$ pending ) {
488
- /** @var ?PromiseInterface $pending */
500
+ /** @var ?PromiseInterface<T> $pending */
489
501
if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
490
502
$ pending ->cancel ();
491
503
}
0 commit comments