diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 8c0673e453fd..6365f2ec5779 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1282,12 +1282,20 @@ 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 */ public function sliding($size = 2, $step = 1) { + if ($size < 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; 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..025f87ea10fd 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1215,12 +1215,20 @@ 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 */ public function sliding($size = 2, $step = 1) { + if ($size < 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) { $iterator = $this->getIterator(); diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index fff1be0b7543..e02322db80f2 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 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 value 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 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 value must be at least 1.', $e->getMessage()); + } } #[DataProvider('collectionClassProvider')]