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

G   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 51
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A rewindableRange() 0 4 1
A assertStep() 0 4 2
A range() 0 12 3
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