Total Complexity | 6 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
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 |
|
54 | } |
||
55 | } |
||
56 |