Completed
Push — master ( 4eee22...078778 )
by Alec
07:22 queued 04:22
created

G::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlecRabbit;
4
5
class G
6
{
7
    /**
8
     * G constructor.
9
     */
10
    private function __construct()
11
    {
12
    }
13
14
    /**
15
     * @param int $start
16
     * @param int $stop
17
     * @param int|float $step
18
     * @return \Generator
19
     */
20 24
    public static function range(int $start, int $stop, $step = 1): \Generator
21
    {
22 24
        static::assertStep($step);
23 20
        $i = $start;
24 20
        $direction = $stop <=> $start;
25 20
        $step = $direction * $step;
26 20
        $halt = false;
27 20
        while (!$halt) {
28 20
            yield $i;
29 20
            $i += $step;
30 20
            if ((($i - $stop) <=> 0) === $direction) {
31 20
                $halt = true;
32
            }
33
        }
34 20
    }
35
36
    /**
37
     * @param int |float $step
38
     */
39 24
    protected static function assertStep($step): void
40
    {
41 24
        if ($step <= 0) {
42 4
            throw new \LogicException('Step has to be greater than zero');
43
        }
44 20
    }
45
46
    /**
47
     * @param int $start
48
     * @param int $stop
49
     * @param int|float $step
50
     * @return Rewindable
51
     */
52 12
    public static function rewindableRange(int $start, int $stop, $step = 1): Rewindable
53
    {
54
        return
55 12
            new Rewindable([__CLASS__, 'range'], $start, $stop, $step);
56
    }
57
58
59
}
60