Issues (85)

src/Operation/GroupBy.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use Generator;
9
10
/**
11
 * @immutable
12
 *
13
 * @template TKey
14
 * @template T
15
 */
16
final class GroupBy extends AbstractOperation
17
{
18
    /**
19
     * @return Closure(callable(T, TKey): array-key): Closure(iterable<TKey, T>): Generator<array-key, non-empty-list<T>>
20
     */
21 4
    public function __invoke(): Closure
22
    {
23 4
        return
24
            /**
25
             * @param callable(T, TKey): array-key $callback
26
             *
27
             * @return Closure(iterable<TKey, T>): Generator<array-key, non-empty-list<T>>
28
             */
29 4
            static function (callable $callback): Closure {
30 4
                $reducerFactory =
31
                    /**
32
                     * @param array<array-key, non-empty-list<T>> $collect
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, non-empty-list<T>> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, non-empty-list<T>>.
Loading history...
33
                     * @param T $value
34
                     * @param TKey $key
35
                     *
36
                     * @return non-empty-array<array-key, non-empty-list<T>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-array<array-key, non-empty-list<T>> at position 0 could not be parsed: Unknown type name 'non-empty-array' at position 0 in non-empty-array<array-key, non-empty-list<T>>.
Loading history...
37
                     */
38 4
                    static function (array $collect, mixed $value, mixed $key) use ($callback): array {
39 4
                        $collect[$callback($value, $key)][] = $value;
40
41 4
                        return $collect;
42 4
                    };
43
44
                /** @var Closure(iterable<TKey, T>): Generator<array-key, non-empty-list<T>> $pipe */
45 4
                $pipe = (new Pipe())()(
46 4
                    (new Reduce())()($reducerFactory)([]),
47 4
                    (new Flatten())()(1)
48 4
                );
49
50
                // Point free style.
51 4
                return $pipe;
52 4
            };
53
    }
54
}
55