Equals::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 33
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 33
ccs 15
cts 15
cp 1
crap 4
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use Generator;
9
use loophp\iterators\IterableIteratorAggregate;
10
11
/**
12
 * @immutable
13
 */
14
final class Equals extends AbstractOperation
15
{
16
    /**
17
     * @return Closure(iterable<mixed, mixed>): Closure(iterable<mixed, mixed>): Generator<int, bool, mixed, false|mixed>
18
     */
19 36
    public function __invoke(): Closure
20
    {
21 36
        return
22
            /**
23
             * @param iterable<mixed, mixed> $other
24
             *
25
             * @return Closure(iterable<mixed, mixed>): Generator<int, bool, mixed, false|mixed>
26
             */
27 36
            static fn (iterable $other): Closure =>
28
                /**
29
                 * @param iterable<mixed, mixed> $iterable
30
                 *
31
                 * @return Generator<int, bool, mixed, false|mixed>
32
                 */
33 36
                static function (iterable $iterable) use ($other): Generator {
34 36
                    $otherAggregate = new IterableIteratorAggregate($other);
35 36
                    $iteratorAggregate = new IterableIteratorAggregate($iterable);
36
37 36
                    $iterator = $iteratorAggregate->getIterator();
38 36
                    $other = $otherAggregate->getIterator();
39
40 36
                    while ($other->valid() && $iterator->valid()) {
41 30
                        $iterator->next();
42 30
                        $other->next();
43
                    }
44
45 36
                    if ($other->valid() !== $iterator->valid()) {
46 12
                        return yield false;
47
                    }
48
49 24
                    $containsCallback = static fn (int $index, mixed $current): bool => (new Contains())()([$current])($otherAggregate)->current();
50
51 24
                    yield from (new Every())()($containsCallback)($iteratorAggregate);
52 36
                };
53
    }
54
}
55