176
176
* await($promise);
177
177
* ```
178
178
*
179
- * @param callable(mixed ...$args):mixed $function
180
- * @return callable(): PromiseInterface<mixed>
179
+ * @param callable $function
180
+ * @return callable(mixed ... ): PromiseInterface<mixed>
181
181
* @since 4.0.0
182
182
* @see coroutine()
183
183
*/
@@ -192,6 +192,7 @@ function async(callable $function): callable
192
192
} catch (\Throwable $ exception ) {
193
193
$ reject ($ exception );
194
194
} finally {
195
+ assert ($ fiber instanceof \Fiber);
195
196
FiberMap::unregister ($ fiber );
196
197
}
197
198
});
@@ -200,6 +201,7 @@ function async(callable $function): callable
200
201
201
202
$ fiber ->start ();
202
203
}, function () use (&$ fiber ): void {
204
+ assert ($ fiber instanceof \Fiber);
203
205
FiberMap::cancel ($ fiber );
204
206
$ promise = FiberMap::getPromise ($ fiber );
205
207
if ($ promise instanceof PromiseInterface && \method_exists ($ promise , 'cancel ' )) {
@@ -287,6 +289,7 @@ function (mixed $value) use (&$resolved, &$resolvedValue, &$fiber, $lowLevelFibe
287
289
FiberMap::unsetPromise ($ lowLevelFiber , $ promise );
288
290
}
289
291
292
+ /** @var ?\Fiber<mixed,mixed,mixed,mixed> $fiber */
290
293
if ($ fiber === null ) {
291
294
$ resolved = true ;
292
295
$ resolvedValue = $ value ;
@@ -309,6 +312,7 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
309
312
// what a lovely piece of code!
310
313
$ r = new \ReflectionProperty ('Exception ' , 'trace ' );
311
314
$ trace = $ r ->getValue ($ throwable );
315
+ assert (\is_array ($ trace ));
312
316
313
317
// Exception trace arguments only available when zend.exception_ignore_args is not set
314
318
// @codeCoverageIgnoreStart
@@ -340,6 +344,7 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
340
344
}
341
345
342
346
if ($ rejected ) {
347
+ assert ($ rejectedThrowable instanceof \Throwable);
343
348
throw $ rejectedThrowable ;
344
349
}
345
350
@@ -587,7 +592,7 @@ function delay(float $seconds): void
587
592
* });
588
593
* ```
589
594
*
590
- * @param callable(...$args):\Generator<mixed,PromiseInterface,mixed,mixed> $function
595
+ * @param callable(mixed ...$args):( \Generator<mixed,PromiseInterface,mixed,mixed>|mixed) $function
591
596
* @param mixed ...$args Optional list of additional arguments that will be passed to the given `$function` as is
592
597
* @return PromiseInterface<mixed>
593
598
* @since 3.0.0
@@ -606,6 +611,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
606
611
607
612
$ promise = null ;
608
613
$ deferred = new Deferred (function () use (&$ promise ) {
614
+ /** @var ?PromiseInterface $promise */
609
615
if ($ promise instanceof PromiseInterface && \method_exists ($ promise , 'cancel ' )) {
610
616
$ promise ->cancel ();
611
617
}
@@ -626,6 +632,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
626
632
return ;
627
633
}
628
634
635
+ /** @var mixed $promise */
629
636
$ promise = $ generator ->current ();
630
637
if (!$ promise instanceof PromiseInterface) {
631
638
$ next = null ;
@@ -635,6 +642,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
635
642
return ;
636
643
}
637
644
645
+ assert ($ next instanceof \Closure);
638
646
$ promise ->then (function ($ value ) use ($ generator , $ next ) {
639
647
$ generator ->send ($ value );
640
648
$ next ();
@@ -657,6 +665,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
657
665
*/
658
666
function parallel (iterable $ tasks ): PromiseInterface
659
667
{
668
+ /** @var array<int,PromiseInterface> $pending */
660
669
$ pending = [];
661
670
$ deferred = new Deferred (function () use (&$ pending ) {
662
671
foreach ($ pending as $ promise ) {
@@ -718,6 +727,7 @@ function series(iterable $tasks): PromiseInterface
718
727
{
719
728
$ pending = null ;
720
729
$ deferred = new Deferred (function () use (&$ pending ) {
730
+ /** @var ?PromiseInterface $pending */
721
731
if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
722
732
$ pending ->cancel ();
723
733
}
@@ -730,9 +740,9 @@ function series(iterable $tasks): PromiseInterface
730
740
assert ($ tasks instanceof \Iterator);
731
741
}
732
742
733
- /** @var callable():void $next */
734
743
$ taskCallback = function ($ result ) use (&$ results , &$ next ) {
735
744
$ results [] = $ result ;
745
+ /** @var \Closure $next */
736
746
$ next ();
737
747
};
738
748
@@ -746,9 +756,11 @@ function series(iterable $tasks): PromiseInterface
746
756
$ task = $ tasks ->current ();
747
757
$ tasks ->next ();
748
758
} else {
759
+ assert (\is_array ($ tasks ));
749
760
$ task = \array_shift ($ tasks );
750
761
}
751
762
763
+ assert (\is_callable ($ task ));
752
764
$ promise = \call_user_func ($ task );
753
765
assert ($ promise instanceof PromiseInterface);
754
766
$ pending = $ promise ;
@@ -762,13 +774,14 @@ function series(iterable $tasks): PromiseInterface
762
774
}
763
775
764
776
/**
765
- * @param iterable<callable(mixed=) :PromiseInterface<mixed>> $tasks
777
+ * @param iterable<( callable():PromiseInterface< mixed>)|(callable(mixed) :PromiseInterface<mixed>) > $tasks
766
778
* @return PromiseInterface<mixed>
767
779
*/
768
780
function waterfall (iterable $ tasks ): PromiseInterface
769
781
{
770
782
$ pending = null ;
771
783
$ deferred = new Deferred (function () use (&$ pending ) {
784
+ /** @var ?PromiseInterface $pending */
772
785
if ($ pending instanceof PromiseInterface && \method_exists ($ pending , 'cancel ' )) {
773
786
$ pending ->cancel ();
774
787
}
@@ -791,9 +804,11 @@ function waterfall(iterable $tasks): PromiseInterface
791
804
$ task = $ tasks ->current ();
792
805
$ tasks ->next ();
793
806
} else {
807
+ assert (\is_array ($ tasks ));
794
808
$ task = \array_shift ($ tasks );
795
809
}
796
810
811
+ assert (\is_callable ($ task ));
797
812
$ promise = \call_user_func_array ($ task , func_get_args ());
798
813
assert ($ promise instanceof PromiseInterface);
799
814
$ pending = $ promise ;
0 commit comments