Total Complexity | 5 |
Total Lines | 36 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
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 |
|
48 | } |
||
49 | } |
||
50 |