Every   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
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 26
ccs 9
cts 9
cp 1
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 21 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use Generator;
9
use loophp\collection\Utils\CallbacksArrayReducer;
10
11
/**
12
 * @immutable
13
 *
14
 * @template TKey
15
 * @template T
16
 */
17
final class Every extends AbstractOperation
18
{
19
    /**
20
     * @return Closure(...callable(int, T, TKey, iterable<TKey, T>): bool): Closure(iterable<TKey, T>): Generator<int, bool>
21
     */
22 118
    public function __invoke(): Closure
23
    {
24 118
        return
25
            /**
26
             * @param callable(int, T, TKey, iterable<TKey, T>): bool ...$predicates
27
             *
28
             * @return Closure(iterable<TKey, T>): Generator<int, bool>
29
             */
30 118
            static fn (callable ...$predicates): Closure => static function (iterable $iterable) use ($predicates): Generator {
31 118
                $predicate = CallbacksArrayReducer::or()($predicates);
32
33
                /** @var Generator<int, array{0: TKey, 1:T}> $packed */
34 118
                $packed = (new Pack())()($iterable);
35
36 118
                foreach ($packed as $index => [$key, $value]) {
37 112
                    if (false === $predicate($index, $value, $key, $iterable)) {
38 78
                        return yield $index => false;
39
                    }
40
                }
41
42 84
                yield true;
43 118
            };
44
    }
45
}
46