From 4390145c21a002e424ac4b1cc07726ecc1d8a4fb Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 20 Dec 2020 18:09:50 +0100 Subject: [PATCH] refactor: Use EmptyIterator. --- src/Operation/Chunk.php | 3 ++- src/Operation/Drop.php | 3 ++- src/Operation/Head.php | 3 ++- src/Operation/Last.php | 3 ++- src/Operation/Times.php | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Operation/Chunk.php b/src/Operation/Chunk.php index 3e81c7543..1983f001c 100644 --- a/src/Operation/Chunk.php +++ b/src/Operation/Chunk.php @@ -6,6 +6,7 @@ use ArrayIterator; use Closure; +use EmptyIterator; use Generator; use Iterator; @@ -42,7 +43,7 @@ static function (Iterator $iterator) use ($sizes): Generator { foreach ($iterator as $value) { if (0 >= $sizesIterator->current()) { - return yield from []; + return new EmptyIterator(); } if (count($values) !== $sizesIterator->current()) { diff --git a/src/Operation/Drop.php b/src/Operation/Drop.php index 6c2f700a2..aba55efac 100644 --- a/src/Operation/Drop.php +++ b/src/Operation/Drop.php @@ -5,6 +5,7 @@ namespace loophp\collection\Operation; use Closure; +use EmptyIterator; use Generator; use Iterator; use LimitIterator; @@ -33,7 +34,7 @@ public function __invoke(): Closure */ static function (Iterator $iterator) use ($offsets): Generator { if (!$iterator->valid()) { - return yield from []; + return new EmptyIterator(); } return yield from new LimitIterator($iterator, array_sum($offsets)); diff --git a/src/Operation/Head.php b/src/Operation/Head.php index 0bcab8811..f9dfe7eac 100644 --- a/src/Operation/Head.php +++ b/src/Operation/Head.php @@ -5,6 +5,7 @@ namespace loophp\collection\Operation; use Closure; +use EmptyIterator; use Generator; use Iterator; @@ -28,7 +29,7 @@ public function __invoke(): Closure */ static function (Iterator $iterator): Generator { if (!$iterator->valid()) { - return yield from []; + return new EmptyIterator(); } return yield $iterator->key() => $iterator->current(); diff --git a/src/Operation/Last.php b/src/Operation/Last.php index 8d0fa188d..0d8d5ffe0 100644 --- a/src/Operation/Last.php +++ b/src/Operation/Last.php @@ -6,6 +6,7 @@ use CachingIterator; use Closure; +use EmptyIterator; use Generator; use Iterator; @@ -29,7 +30,7 @@ public function __invoke(): Closure */ static function (Iterator $iterator): Generator { if (!$iterator->valid()) { - return yield from []; + return new EmptyIterator(); } $cachingIterator = new CachingIterator($iterator, CachingIterator::FULL_CACHE); diff --git a/src/Operation/Times.php b/src/Operation/Times.php index d3f56b93c..031dc0f06 100644 --- a/src/Operation/Times.php +++ b/src/Operation/Times.php @@ -5,6 +5,7 @@ namespace loophp\collection\Operation; use Closure; +use EmptyIterator; use Generator; use Iterator; @@ -39,7 +40,7 @@ public function __invoke(): Closure */ static function (?Iterator $iterator = null) use ($number, $callback): Generator { if (1 > $number) { - yield from []; + return new EmptyIterator(); } $callback ??= static fn (int $value): int => $value;