MersenneTwisterRNGIteratorAggregate::withSeed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 3
c 2
b 1
f 1
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
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 Generator;
13
use IteratorAggregate;
14
15
use const PHP_INT_MAX;
16
use const PHP_INT_MIN;
17
18
/**
19
 * @implements IteratorAggregate<int, int>
20
 */
21
final class MersenneTwisterRNGIteratorAggregate implements IteratorAggregate
22
{
23
    private int $max = PHP_INT_MAX;
24
25
    private int $min = PHP_INT_MIN;
26
27
    private int $seed = 0;
28
29
    /**
30
     * @return Generator<int, int>
31
     */
32 3
    public function getIterator(): Generator
33
    {
34 3
        if (0 !== $this->seed) {
35 2
            mt_srand($this->seed);
36
        }
37
38
        // @phpstan-ignore-next-line
39 3
        while (true) {
40 3
            yield mt_rand($this->min, $this->max);
41
        }
42
    }
43
44 3
    public function withMax(int $max): self
45
    {
46 3
        $clone = clone $this;
47 3
        $clone->max = $max;
48
49 3
        return $clone;
50
    }
51
52 3
    public function withMin(int $min): self
53
    {
54 3
        $clone = clone $this;
55 3
        $clone->min = $min;
56
57 3
        return $clone;
58
    }
59
60 2
    public function withSeed(int $seed): self
61
    {
62 2
        $clone = clone $this;
63 2
        $clone->seed = $seed;
64
65 2
        return $clone;
66
    }
67
}
68