From d3754c13110dc0850e6b4e5e0cf7ca8404d809ce Mon Sep 17 00:00:00 2001 From: Jakob Steinn Date: Thu, 1 Sep 2016 18:22:43 +0200 Subject: [PATCH 1/4] Return early when chunk size is less than zero --- src/Illuminate/Support/Collection.php | 4 ++++ tests/Support/SupportCollectionTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 63acd685839b..37cd08cfa0b4 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -928,6 +928,10 @@ public function slice($offset, $length = null) */ public function chunk($size) { + if ($size == 0) { + return $this; + } + $chunks = []; foreach (array_chunk($this->items, $size, true) as $chunk) { diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 89a878753224..64b78446ff20 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -665,6 +665,14 @@ public function testChunk() $this->assertEquals([9 => 10], $data[3]->toArray()); } + public function testChunkWhenGivenZeroAsSize() + { + $data = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + $chunkedData = $data->chunk(0); + + $this->assertEquals($data, $chunkedData); + } + public function testEvery() { $data = new Collection([ From 4627bb081e820748f551dec0e969c6bc1d32fd48 Mon Sep 17 00:00:00 2001 From: Jakob Steinn Date: Thu, 1 Sep 2016 18:22:43 +0200 Subject: [PATCH 2/4] Return early when chunk size is less than zero --- src/Illuminate/Support/Collection.php | 4 ++++ tests/Support/SupportCollectionTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index fad4175fbefe..7651b3c1aeb9 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -955,6 +955,10 @@ public function split($numberOfGroups) */ public function chunk($size) { + if ($size == 0) { + return $this; + } + $chunks = []; foreach (array_chunk($this->items, $size, true) as $chunk) { diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 2e0562232004..6c9c973b91c8 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -682,6 +682,14 @@ public function testChunk() $this->assertEquals([9 => 10], $data[3]->toArray()); } + public function testChunkWhenGivenZeroAsSize() + { + $data = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + $chunkedData = $data->chunk(0); + + $this->assertEquals($data, $chunkedData); + } + public function testEvery() { $data = new Collection([ From a9e381a2f842004885965ebb2bd1804c8595cf00 Mon Sep 17 00:00:00 2001 From: jstoone Date: Tue, 1 Nov 2016 16:25:33 +0100 Subject: [PATCH 3/4] Return a new collection instance instead of the current one. --- src/Illuminate/Support/Collection.php | 2 +- tests/Support/SupportCollectionTest.php | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 7651b3c1aeb9..a129c9c618b2 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -956,7 +956,7 @@ public function split($numberOfGroups) public function chunk($size) { if ($size == 0) { - return $this; + return new static; } $chunks = []; diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 6c9c973b91c8..cbc809abd910 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -684,10 +684,12 @@ public function testChunk() public function testChunkWhenGivenZeroAsSize() { - $data = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); - $chunkedData = $data->chunk(0); + $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); - $this->assertEquals($data, $chunkedData); + $this->assertEquals( + [], + $collection->chunk(0)->toArray() + ); } public function testEvery() From 5e6de194c6e56d381da3a13c55ae0311cd2280c2 Mon Sep 17 00:00:00 2001 From: jstoone Date: Tue, 1 Nov 2016 16:34:02 +0100 Subject: [PATCH 4/4] Throw invalid argument exception if value below zero is given --- src/Illuminate/Support/Collection.php | 6 ++++++ tests/Support/SupportCollectionTest.php | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index a129c9c618b2..dde214e8f1fa 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -955,6 +955,12 @@ public function split($numberOfGroups) */ public function chunk($size) { + if ($size < 0) { + throw new InvalidArgumentException( + 'Size parameter expected to be greater than 0' + ); + } + if ($size == 0) { return new static; } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index cbc809abd910..d6af9d730dd4 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -692,6 +692,17 @@ public function testChunkWhenGivenZeroAsSize() ); } + public function testChunkWhenGivenLessThanZero() + { + $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + + $this->expectException(InvalidArgumentException::class); + $this->assertEquals( + [], + $collection->chunk(-1)->toArray() + ); + } + public function testEvery() { $data = new Collection([