Skip to content

Commit

Permalink
refactor: Update and simplify typing information.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 3, 2021
1 parent 68d7bdc commit 6ab1622
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/Operation/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use EmptyIterator;
use Generator;
use Iterator;

/**
Expand All @@ -16,17 +17,17 @@
final class Head extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>):Generator<TKey, T, mixed, EmptyIterator|mixed>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Iterator<TKey, T>
* @psalm-return Generator<TKey, T, mixed, EmptyIterator|void>
*/
static function (Iterator $iterator): Iterator {
static function (Iterator $iterator): Generator {
$isEmpty = true;

foreach ($iterator as $key => $current) {
Expand All @@ -39,6 +40,10 @@ static function (Iterator $iterator): Iterator {
return new EmptyIterator();
}

/**
* @psalm-var TKey $key
* @psalm-var T $current
*/
return yield $key => $current;
};
}
Expand Down
10 changes: 4 additions & 6 deletions src/Operation/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ public function __invoke(): Closure
*/
static function (Iterator $iterator): Generator {
$cacheIterator = new CachingIterator($iterator, CachingIterator::FULL_CACHE);
$cacheIterator->next();

for (; $iterator->valid(); $cacheIterator->next()) {
/** @psalm-var TKey $key */
$key = $cacheIterator->key();
/** @psalm-var T $current */
$current = $cacheIterator->current();
foreach ($cacheIterator as $key => $current) {
if (false === $iterator->valid()) {
break;
}

yield $key => $current;
}
Expand Down

0 comments on commit 6ab1622

Please sign in to comment.