Passed
Push — main ( 5e7573...aa3a49 )
by Pol
03:13
created

RandomIterableAggregate::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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 IteratorAggregate;
15
use Traversable;
16
17
/**
18
 * @template TKey
19
 * @template T
20
 *
21
 * @implements IteratorAggregate<TKey, T>
22
 */
23
final class RandomIterableAggregate implements IteratorAggregate
24
{
25
    /**
26
     * @var IteratorAggregate<int, array{0: TKey, 1: T}>
27
     */
28
    private IteratorAggregate $iteratorAggregate;
29
30
    private int $seed;
31
32
    /**
33
     * @param iterable<TKey, T> $iterable
34
     */
35 1
    public function __construct(iterable $iterable, int $seed = 0)
36
    {
37 1
        $this->iteratorAggregate = new PackIterableAggregate($iterable);
38 1
        $this->seed = $seed;
39 1
    }
40
41
    /**
42
     * @return Traversable<TKey, T>
43
     */
44 1
    public function getIterator(): Traversable
45
    {
46 1
        mt_srand($this->seed);
47
48 1
        yield from $this->randomize($this->iteratorAggregate, $this->seed);
49 1
    }
50
51
    /**
52
     * @param Traversable<int, array{0: TKey, 1: T}> $traversable
53
     *
54
     * @return Generator<TKey, T>
55
     */
56 1
    private function randomize(Traversable $traversable, int $seed): Generator
57
    {
58 1
        $isQueueEmpty = true;
59
        /** @var ArrayIterator<int, array{0: TKey, 1: T}> $queue */
60 1
        $queue = new ArrayIterator();
61
62 1
        foreach (new UnpackIterableAggregate($traversable) as $key => $value) {
63 1
            if (mt_rand(0, $seed) === 0) {
64 1
                yield $key => $value;
65
66 1
                continue;
67
            }
68
69 1
            $queue->append([$key, $value]);
70 1
            $isQueueEmpty = false;
71
        }
72
73 1
        if (false === $isQueueEmpty) {
74 1
            yield from $this->randomize($queue, $seed);
75
        }
76 1
    }
77
}
78