Skip to content

Commit

Permalink
refactor: Use more IteratorIterator.
Browse files Browse the repository at this point in the history
* Remove ClosureIterator.
  • Loading branch information
drupol committed Dec 15, 2020
1 parent 54d0f8e commit 0127f40
Show file tree
Hide file tree
Showing 25 changed files with 184 additions and 337 deletions.
15 changes: 8 additions & 7 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpSpec\ObjectBehavior;
use stdClass;
use const INF;
use const PHP_EOL;

class CollectionSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -2385,20 +2386,20 @@ public function it_can_unlines(): void
{
$lines = [
'The quick brow fox jumps over the lazy dog.',
'',
'This is another sentence.',
];

$string = <<<'EOF'
The quick brow fox jumps over the lazy dog.
This is another sentence.
EOF;
$string = sprintf(
'%s%s%s',
'The quick brow fox jumps over the lazy dog.',
PHP_EOL,
'This is another sentence.'
);

$this::fromIterable($lines)
->unlines()
->shouldIterateAs([
2 => $string,
1 => $string,
]);
}

Expand Down
3 changes: 2 additions & 1 deletion spec/loophp/collection/Iterator/CacheIteratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public function it_can_cache_data(CacheItemPoolInterface $cache)
->hasItem(0)
->willReturn(true);

$this->rewind();

$this
->current()
->shouldReturn('a');

$cache
->getItem(0)
->shouldHaveBeenCalledOnce();

$this->rewind();

$this
Expand Down
103 changes: 0 additions & 103 deletions spec/loophp/collection/Iterator/ClosureIteratorSpec.php

This file was deleted.

20 changes: 12 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

use Closure;
use Generator;
use Iterator;
use IteratorIterator;
use loophp\collection\Contract\Collection as CollectionInterface;
use loophp\collection\Contract\Operation;
use loophp\collection\Iterator\ClosureIterator;
use loophp\collection\Iterator\IterableIterator;
use loophp\collection\Iterator\ResourceIterator;
use loophp\collection\Iterator\StringIterator;
Expand Down Expand Up @@ -361,7 +362,7 @@ public static function fromCallable(callable $callable, ...$parameters): self
*
* @psalm-return Generator<TKey, T>
*/
static fn (callable $callable, array $parameters): Generator => yield from new ClosureIterator($callable, ...$parameters),
static fn (callable $callable, array $parameters): Iterator => new IteratorIterator($callable(...$parameters)),
$callable,
$parameters
);
Expand All @@ -370,7 +371,7 @@ public static function fromCallable(callable $callable, ...$parameters): self
public static function fromFile(string $filepath): self
{
return new self(
static fn (string $filepath): Generator => yield from new ResourceIterator(fopen($filepath, 'rb')),
static fn (string $filepath): Iterator => new ResourceIterator(fopen($filepath, 'rb')),
$filepath
);
}
Expand All @@ -383,7 +384,7 @@ public static function fromIterable(iterable $iterable): self
*
* @psalm-return Generator<TKey, T>
*/
static fn (iterable $iterable): Generator => yield from new IterableIterator($iterable),
static fn (iterable $iterable): Iterator => new IterableIterator($iterable),
$iterable
);
}
Expand All @@ -397,7 +398,7 @@ public static function fromResource($resource): self
*
* @psalm-return Generator<int, string>
*/
static fn ($resource): Generator => yield from new ResourceIterator($resource),
static fn ($resource): Iterator => new ResourceIterator($resource),
$resource
);
}
Expand All @@ -408,7 +409,7 @@ public static function fromString(string $string, string $delimiter = ''): self
/**
* @psalm-return Generator<int, string>
*/
static fn (string $string, string $delimiter): Generator => yield from new StringIterator($string, $delimiter),
static fn (string $string, string $delimiter): Iterator => new StringIterator($string, $delimiter),
$string,
$delimiter
);
Expand All @@ -419,9 +420,12 @@ public function get($key, $default = null): CollectionInterface
return new self(Get::of()($key)($default), $this->getIterator());
}

public function getIterator(): ClosureIterator
public function getIterator(): Iterator
{
return new ClosureIterator($this->source, ...$this->parameters);
$iterator = new IteratorIterator(($this->source)(...$this->parameters));
$iterator->rewind();

return $iterator;
}

public function group(): CollectionInterface
Expand Down
6 changes: 3 additions & 3 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace loophp\collection\Contract;

use Iterator;
use IteratorAggregate;
use JsonSerializable;
use loophp\collection\Contract\Operation\Allable;
Expand Down Expand Up @@ -105,7 +106,6 @@
use loophp\collection\Contract\Operation\Wordsable;
use loophp\collection\Contract\Operation\Wrapable;
use loophp\collection\Contract\Operation\Zipable;
use loophp\collection\Iterator\ClosureIterator;

/**
* @psalm-template TKey
Expand Down Expand Up @@ -351,7 +351,7 @@ public static function fromResource($resource): self;
public static function fromString(string $string, string $delimiter = ''): self;

/**
* @psalm-return \loophp\collection\Iterator\ClosureIterator<TKey, T>
* @psalm-return \Iterator<TKey, T>
*/
public function getIterator(): ClosureIterator;
public function getIterator(): Iterator;
}
29 changes: 9 additions & 20 deletions src/Iterator/CacheIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace loophp\collection\Iterator;

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

Expand All @@ -13,22 +14,19 @@
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
* @extends IteratorIterator<TKey, T>
*/
final class CacheIterator extends ProxyIterator
{
private CacheItemPoolInterface $cache;

private int $key;

/**
* @psalm-param Iterator<TKey, T> $iterator
*/
public function __construct(Iterator $iterator, CacheItemPoolInterface $cache)
{
$this->iterator = $iterator;
$this->cache = $cache;
$this->key = 0;
}

/**
Expand All @@ -37,7 +35,7 @@ public function __construct(Iterator $iterator, CacheItemPoolInterface $cache)
public function current()
{
/** @psalm-var array{TKey, T} $data */
$data = $this->getItemOrSave((string) $this->key)->get();
$data = $this->getItemOrSave((string) $this->iterator->key())->get();

return $data[1];
}
Expand All @@ -48,25 +46,16 @@ public function current()
public function key()
{
/** @psalm-var array{TKey, T} $data */
$data = $this->getItemOrSave((string) $this->key)->get();
$data = $this->getItemOrSave((string) $this->iterator->key())->get();

return $data[0];
}

public function next(): void
{
++$this->key;
parent::next();
}

public function rewind(): void
{
$this->key = 0;
}

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

return '' !== $key && ($this->iterator->valid() || $this->cache->hasItem($key));
}

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

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

$this->cache->save($item);
Expand Down

0 comments on commit 0127f40

Please sign in to comment.