Passed
Push — master ( a2af8a...1f5936 )
by Alec
07:10 queued 03:35
created

G::rewindableRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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