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

MixedSetIterableImplementation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 15
c 1
b 0
f 1
dl 0
loc 35
ccs 15
cts 15
cp 1
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B partialIntersection() 0 26 7
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