From f9fea879b0d0babaff1e3fde70731011772babd0 Mon Sep 17 00:00:00 2001 From: Fuwasegu Date: Wed, 13 Sep 2023 22:48:49 +0900 Subject: [PATCH] refactor: stop using paththru in take method --- src/Illuminate/Collections/LazyCollection.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 3ccd32edb4bb..0f57bdb33504 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1421,7 +1421,24 @@ public function sortKeysUsing(callable $callback) public function take($limit) { if ($limit < 0) { - return $this->passthru('take', func_get_args()); + return new static(function () use ($limit) { + $queue = []; + $positiveLimit = abs($limit); + + $iterator = $this->getIterator(); + foreach ($iterator as $key => $value) { + $queue[] = [$key, $value]; + + // If the length of tempQueue exceeds abs($limit), remove the first element. + if (count($queue) > $positiveLimit) { + array_shift($queue); + } + } + + foreach ($queue as $item) { + yield $item[0] => $item[1]; + } + }); } return new static(function () use ($limit) {