Total Complexity | 7 |
Total Lines | 51 |
Duplicated Lines | 0 % |
Coverage | 90% |
Changes | 0 |
1 | <?php |
||
5 | class G |
||
6 | { |
||
7 | /** |
||
8 | * G constructor. |
||
9 | */ |
||
10 | private function __construct() |
||
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 |
|
56 | } |
||
57 | |||
58 | |||
60 |