Skip to content

Commit

Permalink
fix: simplify implementation further
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 30, 2024
1 parent 43280bf commit ef41dfd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 48 deletions.
39 changes: 39 additions & 0 deletions src/RandomIntegerAggregate.php
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

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

InvalidReturnType

src/RandomIntegerAggregate.php:27:16: InvalidReturnType: The declared return type 'Generator<TKey:loophp\iterators\RandomIntegerAggregate as mixed, T:loophp\iterators\RandomIntegerAggregate as mixed, mixed, mixed>' for loophp\iterators\RandomIntegerAggregate::getIterator is incorrect, got 'Generator<int, int<min, max>, mixed, void>' (see https://psalm.dev/011)
*/
public function getIterator(): Generator
{
if (null !== $this->seed) {
mt_srand($this->seed);
}

while (true) {

Check failure on line 35 in src/RandomIntegerAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

While loop condition is always true.

Check failure on line 35 in src/RandomIntegerAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

While loop condition is always true.
yield mt_rand($this->min, $this->max);

Check failure on line 36 in src/RandomIntegerAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

Generator expects key type TKey, int given.

Check failure on line 36 in src/RandomIntegerAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

Generator expects value type T, int given.

Check failure on line 36 in src/RandomIntegerAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

Generator expects key type TKey, int given.

Check failure on line 36 in src/RandomIntegerAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

Generator expects value type T, int given.
}
}
}
6 changes: 2 additions & 4 deletions src/RandomIterableAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ public function __construct(private readonly iterable $iterable, private readonl
*/
public function getIterator(): Generator
{
$cache = new CachingIteratorAggregate((new PackIterableAggregate($this->iterable))->getIterator());

yield from new UnpackIterableAggregate(
new UnpackIterableAggregate(
new SortIterableAggregate(

Check failure on line 30 in src/RandomIterableAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

Parameter #1 $iterable of class loophp\iterators\UnpackIterableAggregate constructor expects iterable<(int|string), array{mixed, mixed}>, loophp\iterators\SortIterableAggregate<array<int, mixed>, array<int, mixed>> given.

Check failure on line 30 in src/RandomIterableAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

InvalidArgument

src/RandomIterableAggregate.php:30:17: InvalidArgument: Argument 1 of loophp\iterators\UnpackIterableAggregate::__construct expects iterable<array-key, array{0: mixed, 1: mixed}>, but loophp\iterators\SortIterableAggregate<array<int, int|mixed>, array<int, (array{0: TKey:loophp\iterators\RandomIterableAggregate as mixed, 1: T:loophp\iterators\RandomIterableAggregate as mixed})|mixed>> provided (see https://psalm.dev/004)

Check failure on line 30 in src/RandomIterableAggregate.php

View workflow job for this annotation

GitHub Actions / Static Analysis (ubuntu-latest, 8.1)

Parameter #1 $iterable of class loophp\iterators\UnpackIterableAggregate constructor expects iterable<(int|string), array{mixed, mixed}>, loophp\iterators\SortIterableAggregate<array<int, mixed>, array<int, mixed>> given.
new MultipleIterableAggregate([
new ShuffleIteratorAggregate(iterator_count($cache), $this->seed),
$cache,
new RandomIntegerAggregate(seed: $this->seed),
new PackIterableAggregate($this->iterable),
]),
static fn (array $a, array $b): int => $a[0] <=> $b[0]
)
Expand Down
44 changes: 0 additions & 44 deletions src/ShuffleIteratorAggregate.php

This file was deleted.

0 comments on commit ef41dfd

Please sign in to comment.