Skip to content

Commit

Permalink
refactor: Update CacheIterator
Browse files Browse the repository at this point in the history
Signed-off-by: Pol Dellaiera <pol.dellaiera@protonmail.com>
  • Loading branch information
drupol committed Oct 27, 2020
1 parent 7386746 commit 02dc0a4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
26 changes: 7 additions & 19 deletions src/Iterator/CacheIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace loophp\collection\Iterator;

use Iterator;
use OuterIterator;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

Expand All @@ -14,21 +13,15 @@
* @psalm-template TKey of array-key
* @psalm-template T
*
* @implements Iterator<TKey, T>
* @extends ProxyIterator<TKey, T>
*/
final class CacheIterator implements Iterator, OuterIterator
final class CacheIterator extends ProxyIterator
{
/**
* @var CacheItemPoolInterface
*/
private $cache;

/**
* @var Iterator
* @psalm-var Iterator<TKey, T>
*/
private $inner;

/**
* @var int
*/
Expand All @@ -39,7 +32,7 @@ final class CacheIterator implements Iterator, OuterIterator
*/
public function __construct(Iterator $iterator, CacheItemPoolInterface $cache)
{
$this->inner = $iterator;
$this->iterator = $iterator;
$this->cache = $cache;
$this->key = 0;
}
Expand All @@ -55,11 +48,6 @@ public function current()
return $data[1];
}

public function getInnerIterator(): Iterator
{
return $this->inner;
}

/**
* @psalm-return TKey
*/
Expand All @@ -74,7 +62,7 @@ public function key()
public function next(): void
{
++$this->key;
$this->inner->next();
parent::next();
}

public function rewind(): void
Expand All @@ -84,7 +72,7 @@ public function rewind(): void

public function valid(): bool
{
return $this->cache->hasItem((string) $this->key) || $this->inner->valid();
return $this->cache->hasItem((string) $this->key) || parent::valid();
}

private function getItemOrSave(string $key): CacheItemInterface
Expand All @@ -93,8 +81,8 @@ private function getItemOrSave(string $key): CacheItemInterface

if (false === $item->isHit()) {
$item->set([
$this->inner->key(),
$this->inner->current(),
parent::key(),
parent::current(),
]);

$this->cache->save($item);
Expand Down
3 changes: 1 addition & 2 deletions src/Iterator/ProxyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
* @psalm-template TKey
* @psalm-template T
*
* @implements Iterator<TKey, T>
* @implements OuterIterator<TKey, T>
*/
abstract class ProxyIterator implements Iterator, OuterIterator
abstract class ProxyIterator implements OuterIterator
{
/**
* @psalm-var Generator<TKey, T>|Iterator<TKey, T>
Expand Down
44 changes: 19 additions & 25 deletions src/Iterator/RandomIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,59 @@

use ArrayIterator;
use Iterator;
use OuterIterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T of string
*
* @implements Iterator<TKey, T>
* @extends ProxyIterator<TKey, T>
*/
final class RandomIterator implements Iterator, OuterIterator
final class RandomIterator extends ProxyIterator
{
/**
* @var array<int, int>
*/
private $indexes;

/**
* @var Iterator
* @psalm-var Iterator<TKey, T>
*/
private $inner;
protected $iterator;

/**
* @var ArrayIterator
* @psalm-var ArrayIterator<int, array{0: TKey, 1: T}>
* @var array<int, int>
*/
private $iterator;
private $indexes;

/**
* @var int
*/
private $key;

/**
* @var ArrayIterator
* @psalm-var ArrayIterator<int, array{0: TKey, 1: T}>
*/
private $wrappedIterator;

/**
* @psalm-param Iterator<TKey, T> $iterator
*/
public function __construct(Iterator $iterator)
{
$this->inner = $iterator;
$this->iterator = $this->buildArrayIterator($iterator);
$this->indexes = array_keys($this->iterator->getArrayCopy());
$this->iterator = $iterator;
$this->wrappedIterator = $this->buildArrayIterator($iterator);
$this->indexes = array_keys($this->wrappedIterator->getArrayCopy());
$this->key = array_rand($this->indexes);
}

public function current()
{
$value = $this->iterator[$this->key];
$value = $this->wrappedIterator[$this->key];

return $value[1];
}

public function getInnerIterator()
{
return $this->inner;
}

public function key()
{
$value = $this->iterator[$this->key];
$value = $this->wrappedIterator[$this->key];

return $value[0];
}
Expand All @@ -78,10 +72,10 @@ public function next(): void
}
}

public function rewind()
public function rewind(): void
{
$this->indexes = array_keys($this->iterator->getArrayCopy());
$this->iterator->rewind();
parent::rewind();
$this->indexes = array_keys($this->wrappedIterator->getArrayCopy());
}

public function valid(): bool
Expand Down

0 comments on commit 02dc0a4

Please sign in to comment.