From 5b9d1535f6b16ec27c8b778ab5afad83bbf8a43d Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 3 Jan 2021 13:47:09 +0100 Subject: [PATCH] refactor: Update Split operation. Use the same OR array_reduce callback everywhere. --- src/Operation/Split.php | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/Operation/Split.php b/src/Operation/Split.php index c4c22f71a..d5f59edeb 100644 --- a/src/Operation/Split.php +++ b/src/Operation/Split.php @@ -43,28 +43,39 @@ public function __invoke(): Closure static function (Iterator $iterator) use ($type, $callbacks): Generator { $carry = []; - $reducer = + $reducerCallback = /** * @param mixed $key * @psalm-param TKey $key * - * @psalm-return Closure(T): Closure(bool, callable(T, TKey): bool): bool + * @psalm-return Closure(T): Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool */ static fn ($key): Closure => /** - * @param mixed $value - * @psalm-param T $value + * @param mixed $current + * @psalm-param T $current * - * @psalm-return Closure(bool, callable(T, TKey): bool): bool + * @psalm-return Closure(Iterator): Closure(bool, callable(T, TKey, Iterator): bool): bool */ - static fn ($value): Closure => + static fn ($current): Closure => /** - * @psalm-param callable(T, TKey): bool $callback + * @psalm-param Iterator $iterator + * + * @psalm-return Closure(bool, callable(T, TKey, Iterator): bool): bool */ - static fn (bool $carry, callable $callback): bool => $callback($value, $key) || $carry; + static fn (Iterator $iterator): Closure => + /** + * @psalm-param bool $carry + * @psalm-param callable(T, TKey, Iterator): bool $callable + */ + static fn (bool $carry, callable $callable): bool => ($callable($current, $key, $iterator)) || $carry; foreach ($iterator as $key => $value) { - $callbackReturn = array_reduce($callbacks, $reducer($key)($value), false); + $callbackReturn = array_reduce( + $callbacks, + $reducerCallback($key)($value)($iterator), + false + ); if (Splitable::AFTER === $type) { $carry[] = $value;