Apply   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

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