Passed
Pull Request — main (#3)
by Pol
03:33
created

RandomIterableAggregate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 52
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A randomize() 0 19 4
A __construct() 0 4 1
A getIterator() 0 5 1
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\iterators;
11
12
use ArrayIterator;
13
use Generator;
14
use Iterator;
15
use IteratorAggregate;
16
use Traversable;
17
18
use const PHP_INT_MAX;
19
use const PHP_INT_MIN;
20
21
/**
22
 * @template TKey
23
 * @template T
24
 *
25
 * @implements IteratorAggregate<TKey, T>
26
 */
27
final class RandomIterableAggregate implements IteratorAggregate
28
{
29
    /**
30
     * @var IteratorAggregate<int, array{0: TKey, 1: T}>
31
     */
32
    private IteratorAggregate $iteratorAggregate;
33
34
    private int $seed;
35
36
    /**
37
     * @param iterable<TKey, T> $iterable
38
     */
39 1
    public function __construct(iterable $iterable, int $seed = 0)
40
    {
41 1
        $this->iteratorAggregate = new PackIterableAggregate($iterable);
42 1
        $this->seed = $seed;
43 1
    }
44
45
    /**
46
     * @return Traversable<TKey, T>
47
     */
48 1
    public function getIterator(): Traversable
49
    {
50 1
        mt_srand($this->seed);
51
52 1
        yield from $this->randomize($this->iteratorAggregate, $this->seed);
53 1
    }
54
55
    /**
56
     * @param Traversable<int, array{0: TKey, 1: T}> $traversable
57
     *
58
     * @return Generator<TKey, T>
59
     */
60 1
    private function randomize(Traversable $traversable, int $seed): Generator
61
    {
62 1
        $isQueueEmpty = true;
63
        /** @var ArrayIterator<int, array{0: TKey, 1: T}> $queue */
64 1
        $queue = new ArrayIterator();
65
66 1
        foreach (new UnpackIterableAggregate($traversable) as $key => $value) {
67 1
            if (mt_rand(0, $seed) === 0) {
68 1
                yield $key => $value;
69
70 1
                continue;
71
            }
72
73 1
            $queue->append([$key, $value]);
74 1
            $isQueueEmpty = false;
75
        }
76
77 1
        if (false === $isQueueEmpty) {
78 1
            yield from $this->randomize($queue, $seed);
79
        }
80 1
    }
81
}
82