Passed
Push — master ( e76d90...348fca )
by Smoren
02:26
created

partialIntersection()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

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