MapN   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 25 2
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 MapN extends AbstractOperation
17
{
18
    /**
19
     * @return Closure(callable(mixed, mixed, iterable<TKey, T>): mixed ...): Closure(iterable<TKey, T>): Generator<mixed, mixed>
20
     */
21 4
    public function __invoke(): Closure
22
    {
23 4
        return
24
            /**
25
             * @param callable(mixed, mixed, iterable<TKey, T>): mixed ...$callbacks
26
             */
27 4
            static fn (callable ...$callbacks): Closure =>
28
                /**
29
                 * @param iterable<TKey, T> $iterable
30
                 *
31
                 * @return Generator<mixed, mixed>
32
                 */
33 4
                static function (iterable $iterable) use ($callbacks): Generator {
34 4
                    $callbackFactory =
35
                        /**
36
                         * @return Closure(mixed, callable(mixed, mixed, iterable<TKey, T>): mixed): mixed
37
                         */
38 4
                        static fn (mixed $key): Closure =>
39
                            /**
40
                             * @param callable(mixed, mixed, iterable<TKey, T>): mixed $callback
41
                             */
42 4
                            static fn (mixed $carry, callable $callback): mixed => $callback($carry, $key, $iterable);
43
44 4
                    foreach ($iterable as $key => $value) {
45 4
                        yield $key => array_reduce($callbacks, $callbackFactory($key), $value);
46
                    }
47 4
                };
48
    }
49
}
50