Skip to content

Commit

Permalink
refactor: Update Iterator annotations.
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 90c732c commit dc9a311
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
27 changes: 14 additions & 13 deletions src/Iterator/ClosureIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,50 @@

use Closure;
use Generator;
use Iterator;
use OuterIterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
* @implements Iterator<TKey, T>
*/
final class ClosureIterator extends ProxyIterator implements Iterator, OuterIterator
final class ClosureIterator extends ProxyIterator
{
/**
* @var array<int, mixed>
* @psalm-var list<T>
* @psalm-var list<mixed>
*/
private $arguments;

/**
* @var callable
* @psalm-var callable(T...):(Generator<TKey, T>)
* @psalm-var callable(mixed...):(Generator<TKey, T>)
*/
private $callable;

/**
* @var Closure
* @psalm-var Closure(callable(T...):Generator<TKey, T>, list<T>):(Generator<TKey, T>)
* @psalm-var Closure(callable(mixed...):Generator<TKey, T>, list<T>):(Generator<TKey, T>)
*/
private $generator;

/**
* @param mixed ...$arguments
* @psalm-param T ...$arguments
* @psalm-param callable(T...):(Generator<TKey,T>) $callable
* @psalm-param mixed ...$arguments
* @psalm-param callable(mixed...):(Generator<TKey, T>) $callable
*/
public function __construct(callable $callable, ...$arguments)
{
$this->callable = $callable;
$this->arguments = $arguments;
$this->generator = static function (callable $callable, array $arguments): Generator {
return yield from ($callable)(...$arguments);
};
$this->generator =
/**
* @psalm-param callable(T...):Generator<TKey, T> $callable
* @psalm-param list<T> $arguments
*/
static function (callable $callable, array $arguments): Generator {
return yield from ($callable)(...$arguments);
};

$this->iterator = $this->getGenerator();
}
Expand Down
6 changes: 1 addition & 5 deletions src/Iterator/IterableIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
namespace loophp\collection\Iterator;

use Generator;
use Iterator;
use OuterIterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
* @implements Iterator<TKey, T>
*/
final class IterableIterator extends ProxyIterator implements Iterator, OuterIterator
final class IterableIterator extends ProxyIterator
{
/**
* @param iterable<mixed> $iterable
Expand Down
7 changes: 5 additions & 2 deletions src/Iterator/ProxyIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use Generator;
use Iterator;
use OuterIterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* @implements Iterator<TKey, T>
* @implements OuterIterator<TKey, T>
*/
abstract class ProxyIterator
abstract class ProxyIterator implements Iterator, OuterIterator
{
/**
* @psalm-var Generator<TKey, T>|Iterator<TKey, T>
Expand Down
7 changes: 2 additions & 5 deletions src/Iterator/ResourceIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
namespace loophp\collection\Iterator;

use Generator;
use Iterator;
use OuterIterator;

/**
* @psalm-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
* @implements Iterator<TKey, T>
*/
final class ResourceIterator extends ProxyIterator implements Iterator, OuterIterator
final class ResourceIterator extends ProxyIterator
{
/**
* @param resource $resource
Expand All @@ -27,7 +24,7 @@ public function __construct($resource)
/**
* @param resource $resource
*
* @psalm-return Generator<int, T>
* @psalm-return Generator<int, string>
*/
static function ($resource): Generator {
while (false !== $chunk = fgetc($resource)) {
Expand Down

0 comments on commit dc9a311

Please sign in to comment.