Passed
Pull Request — master (#314)
by Pol
02:10
created

Plus::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 27
ccs 7
cts 7
cp 1
crap 1
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\iterators\ConcatIterableAggregate;
10
11
/**
12
 * @immutable
13
 *
14
 * @template TKey
15
 * @template T
16
 */
17
final class Plus extends AbstractOperation
18
{
19
    /**
20
     * @template UKey
21
     * @template U
22
     *
23
     * @return Closure(iterable<UKey, U>): Closure(iterable<TKey, T>): iterable<int|TKey|UKey, T|U>
24
     */
25 6
    public function __invoke(): Closure
26
    {
27 6
        $comparatorCallback =
28
            /**
29
             * @param T $left
30
             *
31
             * @return Closure(T): bool
32
             */
33 6
            static fn (mixed $left): Closure =>
34
                /**
35
                 * @param T $right
36
                 */
37 6
                static fn (mixed $right): bool => $left === $right;
38
39 6
        return
40
            /**
41
             * @param iterable<UKey, U> $items
42
             *
43
             * @return Closure(iterable<TKey, T>): iterable<int|TKey|UKey, T|U>
44
             */
45 6
            static fn (iterable $items): Closure =>
46
                /**
47
                 * @param iterable<TKey, T> $iterable
48
                 *
49
                 * @return Generator<int|TKey|UKey, T|U>
50
                 */
51 6
                static fn (iterable $iterable): Generator => yield from (new Distinct())()($comparatorCallback)(static fn (mixed $value, mixed $key): mixed => $key)(new ConcatIterableAggregate([$iterable, $items]));
52
    }
53
}
54