Same::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 37
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 37
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 Same extends AbstractOperation
15
{
16
    /**
17
     * @return Closure(iterable<mixed, mixed>): Closure(callable(mixed, mixed): Closure(mixed, mixed): bool): Closure(iterable<mixed, mixed>): Generator<int, bool>
18
     */
19 46
    public function __invoke(): Closure
20
    {
21 46
        return
22
            /**
23
             * @param iterable<mixed, mixed> $other
24
             *
25
             * @return Closure(callable(mixed, mixed): Closure(mixed, mixed): bool): Closure(iterable<mixed, mixed>): Generator<int, bool>
26
             */
27 46
            static fn (iterable $other): Closure =>
28
                /**
29
                 * @param callable(mixed, mixed): (Closure(mixed, mixed): bool) $comparatorCallback
30
                 *
31
                 * @return Closure(iterable<mixed, mixed>): Generator<int, bool>
32
                 */
33 46
                static fn (callable $comparatorCallback): Closure =>
34
                    /**
35
                     * @param iterable<mixed, mixed> $iterable
36
                     *
37
                     * @return Generator<int, bool>
38
                     */
39 46
                    static function (iterable $iterable) use ($other, $comparatorCallback): Generator {
40 46
                        $otherAggregate = new IterableIteratorAggregate($other);
41 46
                        $iteratorAggregate = new IterableIteratorAggregate($iterable);
42
43 46
                        $iterator = $iteratorAggregate->getIterator();
44 46
                        $other = $otherAggregate->getIterator();
45
46 46
                        while ($iterator->valid() && $other->valid()) {
47 40
                            if (!$comparatorCallback($iterator->current(), $iterator->key())($other->current(), $other->key())) {
48 20
                                return yield false;
49
                            }
50
51 22
                            $iterator->next();
52 22
                            $other->next();
53
                        }
54
55 26
                        yield $iterator->valid() === $other->valid();
56 46
                    };
57
    }
58
}
59