Total Complexity | 7 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
5 | class G |
||
6 | { |
||
7 | /** |
||
8 | * Static class. Private Constructor. |
||
9 | */ |
||
10 | // @codeCoverageIgnoreStart |
||
11 | private function __construct() |
||
13 | } |
||
14 | // @codeCoverageIgnoreEnd |
||
15 | |||
16 | /** |
||
17 | * @param int $start |
||
18 | * @param int $stop |
||
19 | * @param int|float $step |
||
20 | * @return \Generator |
||
21 | */ |
||
22 | 24 | public static function range(int $start, int $stop, $step = 1): \Generator |
|
23 | { |
||
24 | 24 | static::assertStep($step); |
|
25 | 20 | $i = $start; |
|
26 | 20 | $direction = $stop <=> $start; |
|
27 | 20 | $step = $direction * $step; |
|
28 | 20 | $halt = false; |
|
29 | 20 | while (!$halt) { |
|
30 | 20 | yield $i; |
|
31 | 20 | $i += $step; |
|
32 | 20 | if ((($i - $stop) <=> 0) === $direction) { |
|
33 | 20 | $halt = true; |
|
34 | } |
||
35 | } |
||
36 | 20 | } |
|
37 | |||
38 | /** |
||
39 | * @param int |float $step |
||
40 | */ |
||
41 | 24 | protected static function assertStep($step): void |
|
42 | { |
||
43 | 24 | if ($step <= 0) { |
|
44 | 4 | throw new \LogicException('Step has to be greater than zero'); |
|
45 | } |
||
46 | 20 | } |
|
47 | |||
48 | /** |
||
49 | * @param int $start |
||
50 | * @param int $stop |
||
51 | * @param int|float $step |
||
52 | * @return Rewindable |
||
53 | */ |
||
54 | 12 | public static function rewindableRange(int $start, int $stop, $step = 1): Rewindable |
|
58 | } |
||
59 | } |
||
60 |