Skip to content

Commit

Permalink
refactor: Update UniqueIterableAggregate.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 16, 2022
1 parent b736ca3 commit c356e63
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The missing PHP iterators.
* `ClosureIteratorAggregate`: `ClosureIteratorAggregate(callable $callable, array $arguments = [])`
* `ConcatIterableAggregate`
* `FilterIterableAggregate`
* `InterruptableIterableAggregate`: `InterruptableIterableAggregate(iterable $iterable)`
* `InterruptableIterableIteratorAggregate`: `InterruptableIterableIteratorAggregate(iterable $iterable)`
* `IterableIterator`: `IterableIterator(iterable $iterable)`
* `IterableIteratorAggregate`: `IterableIteratorAggregate(iterable $iterable)`
* `MapIterableAggregate`
Expand Down Expand Up @@ -102,7 +102,7 @@ $iterator = (new FilterIterableAggregate(
foreach ($iterator as $filteredValue) {} // 0, 2, 4
```

### InterruptableIterableAggregate
### InterruptableIterableIteratorAggregate

Let you break the iterator at anytime.

Expand All @@ -120,13 +120,13 @@ $naturals = static function () {
}
};

$iterator = new InterruptableIterableAggregate($generator());
$iterator = new InterruptableIterableIteratorAggregate($generator());

foreach ($iterator as $generator => [$key, $value]) {
var_dump($value);

if (10 === $value) {
$generator->send(InterruptableIterableAggregate::BREAK);
$generator->send(InterruptableIterableIteratorAggregate::BREAK);
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.23.0@f1fe6ff483bf325c803df9f510d09a03fd796f88">
<file src="src/InterruptableIteratorAggregate.php">
<file src="src/InterruptableIterableIteratorAggregate.php">
<ImplementedReturnTypeMismatch occurrences="1">
<code>Generator&lt;Generator&lt;TKey, T&gt;, array{0: TKey, 1: T}&gt;</code>
</ImplementedReturnTypeMismatch>
Expand Down
1 change: 1 addition & 0 deletions src/ChunkIterableAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Generator;
use IteratorAggregate;

use function count;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @implements IteratorAggregate<TKey, T>
*/
final class InterruptableIteratorAggregate implements IteratorAggregate
final class InterruptableIterableIteratorAggregate implements IteratorAggregate
{
public const BREAK = 'break';

Expand Down
13 changes: 6 additions & 7 deletions src/UniqueIterableAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Generator;
use IteratorAggregate;
use Traversable;

use function in_array;

Expand All @@ -26,9 +25,9 @@
final class UniqueIterableAggregate implements IteratorAggregate
{
/**
* @var iterable<TKey, T>
* @var InterruptableIterableIteratorAggregate<TKey, T>
*/
private iterable $iterable;
private InterruptableIterableIteratorAggregate $iterable;

private int $retries;

Expand All @@ -37,21 +36,21 @@ final class UniqueIterableAggregate implements IteratorAggregate
*/
public function __construct(iterable $iterable, int $retries = PHP_INT_MAX)
{
$this->iterable = $iterable;
$this->iterable = new InterruptableIterableIteratorAggregate($iterable);
$this->retries = $retries;
}

/**
* @return Generator<TKey, T>
*/
public function getIterator(): Traversable
public function getIterator(): Generator
{
$retries = $this->retries;
$seen = [];

foreach ($this->iterable as $key => $value) {
foreach ($this->iterable as $generator => [$key, $value]) {
if (0 === $retries) {
break;
$generator->send(InterruptableIterableIteratorAggregate::BREAK);
}

if (in_array($value, $seen, true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

use Generator;
use IteratorAggregate;
use loophp\iterators\InterruptableIteratorAggregate;
use loophp\iterators\InterruptableIterableIteratorAggregate;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversDefaultClass \loophp\iterators
*/
final class InterruptableIteratorAggregateTest extends TestCase
final class InterruptableIterableIteratorAggregateTest extends TestCase
{
public function testBreakInterruption()
{
Expand All @@ -30,7 +30,7 @@ public function testBreakInterruption()
}
};

$subject = new InterruptableIteratorAggregate($naturals());
$subject = new InterruptableIterableIteratorAggregate($naturals());

self::assertSame(
range(0, 10),
Expand All @@ -44,7 +44,7 @@ private function getSubjectGenerator(IteratorAggregate $subject): Generator
yield $key => $value;

if (10 === $value) {
$generator->send(InterruptableIteratorAggregate::BREAK);
$generator->send(InterruptableIterableIteratorAggregate::BREAK);
}
}
}
Expand Down

0 comments on commit c356e63

Please sign in to comment.