From 5430786b927bcef59f2d3c9e13b2e025aa1d894d Mon Sep 17 00:00:00 2001 From: Button99 Date: Sun, 23 Nov 2025 22:23:48 +0200 Subject: [PATCH 1/2] [12.x] Add parameter validation to Collection::sliding() method --- src/Illuminate/Collections/Collection.php | 10 ++++++ src/Illuminate/Collections/LazyCollection.php | 10 ++++++ tests/Support/SupportCollectionTest.php | 32 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 8c0673e453fd..5bc25ab6c50b 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1285,9 +1285,19 @@ public function shuffle() * @param int $size * @param int $step * @return static + * + * @throws \InvalidArgumentException */ public function sliding($size = 2, $step = 1) { + if ($size < 1) { + throw new InvalidArgumentException('Size must be at least 1.'); + } + + if ($step < 1) { + throw new InvalidArgumentException('Step must be at least 1.'); + } + $chunks = floor(($this->count() - $size) / $step) + 1; return static::times($chunks, fn ($number) => $this->slice(($number - 1) * $step, $size)); diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 95b61720afc4..83197fb43d77 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1218,9 +1218,19 @@ public function shuffle() * @param int $size * @param int $step * @return static + * + * @throws \InvalidArgumentException */ public function sliding($size = 2, $step = 1) { + if ($size < 1) { + throw new InvalidArgumentException('Size must be at least 1.'); + } + + if ($step < 1) { + throw new InvalidArgumentException('Step must be at least 1.'); + } + return new static(function () use ($size, $step) { $iterator = $this->getIterator(); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index fff1be0b7543..508ac56d61df 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -447,6 +447,38 @@ public function testSliding($collection) $this->assertInstanceOf($collection, $chunks); $this->assertInstanceOf($collection, $chunks->first()); $this->assertInstanceOf($collection, $chunks->skip(1)->first()); + + // Test invalid size parameter (size must be at least 1) + // instead of throwing an error. Now it throws InvalidArgumentException. + try { + $collection::times(5)->sliding(0, 1)->toArray(); + $this->fail('Expected InvalidArgumentException for size = 0'); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Size must be at least 1.', $e->getMessage()); + } + + try { + $collection::times(5)->sliding(-1, 1)->toArray(); + $this->fail('Expected InvalidArgumentException for size = -1'); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Size must be at least 1.', $e->getMessage()); + } + + // Test invalid step parameter (step must be at least 1) + // Now it throws InvalidArgumentException with an error message. + try { + $collection::times(5)->sliding(2, 0)->toArray(); + $this->fail('Expected InvalidArgumentException for step = 0'); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Step must be at least 1.', $e->getMessage()); + } + + try { + $collection::times(5)->sliding(2, -1)->toArray(); + $this->fail('Expected InvalidArgumentException for step = -1'); + } catch (\InvalidArgumentException $e) { + $this->assertSame('Step must be at least 1.', $e->getMessage()); + } } #[DataProvider('collectionClassProvider')] From 12f581e0019911ecf2c4591a9681bbe3c87137ed Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 24 Nov 2025 08:13:30 -0600 Subject: [PATCH 2/2] formatting --- src/Illuminate/Collections/Collection.php | 12 +++++------- src/Illuminate/Collections/LazyCollection.php | 12 +++++------- tests/Support/SupportCollectionTest.php | 8 ++++---- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 5bc25ab6c50b..6365f2ec5779 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1282,8 +1282,8 @@ public function shuffle() /** * Create chunks representing a "sliding window" view of the items in the collection. * - * @param int $size - * @param int $step + * @param positive-int $size + * @param positive-int $step * @return static * * @throws \InvalidArgumentException @@ -1291,11 +1291,9 @@ public function shuffle() public function sliding($size = 2, $step = 1) { if ($size < 1) { - throw new InvalidArgumentException('Size must be at least 1.'); - } - - if ($step < 1) { - throw new InvalidArgumentException('Step must be at least 1.'); + throw new InvalidArgumentException('Size value must be at least 1.'); + } elseif ($step < 1) { + throw new InvalidArgumentException('Step value must be at least 1.'); } $chunks = floor(($this->count() - $size) / $step) + 1; diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 83197fb43d77..025f87ea10fd 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1215,8 +1215,8 @@ public function shuffle() /** * Create chunks representing a "sliding window" view of the items in the collection. * - * @param int $size - * @param int $step + * @param positive-int $size + * @param positive-int $step * @return static * * @throws \InvalidArgumentException @@ -1224,11 +1224,9 @@ public function shuffle() public function sliding($size = 2, $step = 1) { if ($size < 1) { - throw new InvalidArgumentException('Size must be at least 1.'); - } - - if ($step < 1) { - throw new InvalidArgumentException('Step must be at least 1.'); + throw new InvalidArgumentException('Size value must be at least 1.'); + } elseif ($step < 1) { + throw new InvalidArgumentException('Step value must be at least 1.'); } return new static(function () use ($size, $step) { diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 508ac56d61df..e02322db80f2 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -454,14 +454,14 @@ public function testSliding($collection) $collection::times(5)->sliding(0, 1)->toArray(); $this->fail('Expected InvalidArgumentException for size = 0'); } catch (\InvalidArgumentException $e) { - $this->assertSame('Size must be at least 1.', $e->getMessage()); + $this->assertSame('Size value must be at least 1.', $e->getMessage()); } try { $collection::times(5)->sliding(-1, 1)->toArray(); $this->fail('Expected InvalidArgumentException for size = -1'); } catch (\InvalidArgumentException $e) { - $this->assertSame('Size must be at least 1.', $e->getMessage()); + $this->assertSame('Size value must be at least 1.', $e->getMessage()); } // Test invalid step parameter (step must be at least 1) @@ -470,14 +470,14 @@ public function testSliding($collection) $collection::times(5)->sliding(2, 0)->toArray(); $this->fail('Expected InvalidArgumentException for step = 0'); } catch (\InvalidArgumentException $e) { - $this->assertSame('Step must be at least 1.', $e->getMessage()); + $this->assertSame('Step value must be at least 1.', $e->getMessage()); } try { $collection::times(5)->sliding(2, -1)->toArray(); $this->fail('Expected InvalidArgumentException for step = -1'); } catch (\InvalidArgumentException $e) { - $this->assertSame('Step must be at least 1.', $e->getMessage()); + $this->assertSame('Step value must be at least 1.', $e->getMessage()); } }