Passed
Push — master ( 22859b...1c8953 )
by Smoren
02:23
created

partialIntersection()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 12
c 1
b 0
f 1
nc 7
nop 3
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\PartialIntersection;
6
7
use Smoren\PartialIntersection\Util\JustifyMultipleIterator;
8
use Smoren\PartialIntersection\Util\NoValueMonad;
9
use Smoren\PartialIntersection\Util\UniqueExtractor;
10
11
class MixedSetIterableImplementation
12
{
13
    /**
14
     * @template T
15
     *
16
     * @param bool $strict
17
     * @param int $m
18
     * @param iterable<T> ...$sets
19
     *
20
     * @return \Generator<T>
21
     */
22 356
    public static function partialIntersection(bool $strict, int $m, iterable ...$sets): \Generator
23
    {
24 356
        $iterator = new JustifyMultipleIterator(...$sets);
25
26 356
        $usageMap = [];
27
28 356
        foreach ($iterator as $values) {
29 300
            foreach ($values as $value) {
30 300
                if ($value instanceof NoValueMonad) {
31 113
                    continue;
32
                }
33
34 300
                $hash = UniqueExtractor::getString($value, $strict);
35
36 300
                if (!isset($usageMap[$hash])) {
37 300
                    $usageMap[$hash] = 0;
38
                }
39
40 300
                $usageMap[$hash]++;
41
42 300
                if ($usageMap[$hash] === $m) {
43 190
                    yield $value;
44
                }
45
            }
46
        }
47
    }
48
}
49