diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ac86d2cea..b67714139 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -86,7 +86,7 @@ parameters: path: src/Collection.php - - message: "#^Parameter \\#1 \\$callable of class loophp\\\\collection\\\\Collection constructor expects callable\\(\\.\\.\\.mixed\\)\\: iterable\\\\>, Closure\\(iterable\\)\\: Generator\\, mixed, void\\> given\\.$#" + message: "#^Parameter \\#1 \\$callable of class loophp\\\\collection\\\\Collection constructor expects callable\\(\\.\\.\\.mixed\\)\\: iterable\\\\>, Closure\\(iterable\\<\\(int\\|string\\), mixed\\>\\)\\: Generator\\, mixed, mixed\\> given\\.$#" count: 1 path: src/Collection.php diff --git a/src/Operation/Tails.php b/src/Operation/Tails.php index c8bd07e17..bad47508f 100644 --- a/src/Operation/Tails.php +++ b/src/Operation/Tails.php @@ -6,6 +6,10 @@ use Closure; use Generator; +use loophp\iterators\ConcatIterableAggregate; +use loophp\iterators\ReductionIterableAggregate; + +use function array_slice; /** * @immutable @@ -16,29 +20,35 @@ final class Tails extends AbstractOperation { /** - * @return Closure(iterable): Generator, mixed, void> + * @return Closure(iterable): Generator> */ public function __invoke(): Closure { return /** - * @param iterable $iterable + * @param iterable $iterable * - * @return Generator, mixed, void> + * @return Generator> */ static function (iterable $iterable): Generator { - /** @var Generator $generator */ - $generator = (new Pack())()($iterable); - $data = [...$generator]; - - while ([] !== $data) { - /** @psalm-suppress InvalidOperand */ - yield [...(new Unpack())()($data)]; + $iterator = new ConcatIterableAggregate([ + [0 => 0], + $iterable, + ]); - array_shift($data); - } + /** @var array $generator */ + $generator = iterator_to_array((new Normalize())()($iterator)); - yield []; + yield from new ReductionIterableAggregate( + $generator, + /** + * @param list $stack + * + * @return list + */ + static fn (array $stack): array => array_slice($stack, 1), + $generator + ); }; } }