-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: simplify implementation further
- Loading branch information
Showing
3 changed files
with
41 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\iterators; | ||
|
||
use Generator; | ||
use IteratorAggregate; | ||
|
||
use const PHP_INT_MAX; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
* | ||
* @implements IteratorAggregate<TKey, T> | ||
*/ | ||
final class RandomIntegerAggregate implements IteratorAggregate | ||
{ | ||
public function __construct( | ||
private readonly ?int $seed = null, | ||
private readonly int $min = 0, | ||
private readonly int $max = PHP_INT_MAX, | ||
) {} | ||
|
||
/** | ||
* @return Generator<TKey, T> | ||
Check failure on line 27 in src/RandomIntegerAggregate.php GitHub Actions / Static Analysis (ubuntu-latest, 8.1)InvalidReturnType
|
||
*/ | ||
public function getIterator(): Generator | ||
{ | ||
if (null !== $this->seed) { | ||
mt_srand($this->seed); | ||
} | ||
|
||
while (true) { | ||
Check failure on line 35 in src/RandomIntegerAggregate.php GitHub Actions / Static Analysis (ubuntu-latest, 8.1)
|
||
yield mt_rand($this->min, $this->max); | ||
Check failure on line 36 in src/RandomIntegerAggregate.php GitHub Actions / Static Analysis (ubuntu-latest, 8.1)
Check failure on line 36 in src/RandomIntegerAggregate.php GitHub Actions / Static Analysis (ubuntu-latest, 8.1)
Check failure on line 36 in src/RandomIntegerAggregate.php GitHub Actions / Static Analysis (ubuntu-latest, 8.1)
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.