Skip to content

Commit

Permalink
fix: Simple fix which should fix the issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 30, 2020
1 parent f09ae23 commit 804b4de
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/Operation/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace loophp\collection\Operation;

use CachingIterator;
use Closure;
use EmptyIterator;
use Generator;
Expand All @@ -29,20 +28,15 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator): Generator {
if (!$iterator->valid()) {
return new EmptyIterator();
}

$cachingIterator = new CachingIterator($iterator, CachingIterator::FULL_CACHE);
$isEmpty = 1;

while ($iterator->valid()) {
$cachingIterator->next();
foreach ($iterator as $key => $current) {
$isEmpty = 0;
}

/** @psalm-var TKey $key */
$key = $cachingIterator->key();
/** @psalm-var T $current */
$current = $cachingIterator->current();
if (1 === $isEmpty) {
return new EmptyIterator();
}

return yield $key => $current;
};
Expand Down

0 comments on commit 804b4de

Please sign in to comment.