diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 6a225dce5c25..d7369d686591 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1134,7 +1134,11 @@ public function shift($count = 1) throw new InvalidArgumentException('Number of shifted items may not be less than zero.'); } - if ($count === 0 || $this->isEmpty()) { + if ($this->isEmpty()) { + return null; + } + + if ($count === 0) { return new static; } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 8f72a9ca1e5e..e1e0a2d89cdb 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -404,6 +404,23 @@ public function testShiftReturnsAndRemovesFirstXItemsInCollection() (new Collection(['foo', 'bar', 'baz']))->shift(-2); } + public function testShiftReturnsNullOnEmptyCollection() + { + $itemFoo = new \stdClass(); + $itemFoo->text = 'f'; + $itemBar = new \stdClass(); + $itemBar->text = 'x'; + + $items = collect([$itemFoo, $itemBar]); + + $foo = $items->shift(); + $bar = $items->shift(); + + $this->assertSame('f', $foo?->text); + $this->assertSame('x', $bar?->text); + $this->assertNull($items->shift()); + } + /** * @dataProvider collectionClassProvider */