Passed
Push — master ( 757597...984cbe )
by Alec
06:39
created

G   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 44
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rewindableRange() 0 4 1
A assertStep() 0 4 2
A range() 0 12 3
1
<?php
2
/**
3
 * User: alec
4
 * Date: 29.11.18
5
 * Time: 9:58
6
 */
7
8
namespace AlecRabbit;
9
10
class G
11
{
12
    /**
13
     * @param int $start
14
     * @param int $stop
15
     * @param int|float $step
16
     * @return \Generator
17
     */
18 24
    public static function range(int $start, int $stop, $step = 1): \Generator
19
    {
20 24
        static::assertStep($step);
21 20
        $i = $start;
22 20
        $direction = $stop <=> $start;
23 20
        $step = $direction * $step;
24 20
        $halt = false;
25 20
        while (!$halt) {
26 20
            yield $i;
27 20
            $i += $step;
28 20
            if ((($i - $stop) <=> 0) === $direction) {
29 20
                $halt = true;
30
            }
31
        }
32 20
    }
33
34
    /**
35
     * @param int |float $step
36
     */
37 24
    protected static function assertStep($step): void
38
    {
39 24
        if ($step <= 0) {
40 4
            throw new \LogicException('Step has to be greater than zero');
41
        }
42 20
    }
43
44
    /**
45
     * @param int $start
46
     * @param int $stop
47
     * @param int|float $step
48
     * @return Rewindable
49
     */
50 12
    public static function rewindableRange(int $start, int $stop, $step = 1): Rewindable
51
    {
52
        return
53 12
            new Rewindable([__CLASS__, 'range'], $start, $stop, $step);
54
    }
55
}
56