Every::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 21
ccs 9
cts 9
cp 1
crap 3
rs 10
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