Skip to content

Commit 98be693

Browse files
[12.x] Add parameter validation to Collection::sliding() method. (#57875)
* [12.x] Add parameter validation to Collection::sliding() method * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 5fb531d commit 98be693

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,12 +1282,20 @@ public function shuffle()
12821282
/**
12831283
* Create chunks representing a "sliding window" view of the items in the collection.
12841284
*
1285-
* @param int $size
1286-
* @param int $step
1285+
* @param positive-int $size
1286+
* @param positive-int $step
12871287
* @return static<int, static>
1288+
*
1289+
* @throws \InvalidArgumentException
12881290
*/
12891291
public function sliding($size = 2, $step = 1)
12901292
{
1293+
if ($size < 1) {
1294+
throw new InvalidArgumentException('Size value must be at least 1.');
1295+
} elseif ($step < 1) {
1296+
throw new InvalidArgumentException('Step value must be at least 1.');
1297+
}
1298+
12911299
$chunks = floor(($this->count() - $size) / $step) + 1;
12921300

12931301
return static::times($chunks, fn ($number) => $this->slice(($number - 1) * $step, $size));

src/Illuminate/Collections/LazyCollection.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,12 +1215,20 @@ public function shuffle()
12151215
/**
12161216
* Create chunks representing a "sliding window" view of the items in the collection.
12171217
*
1218-
* @param int $size
1219-
* @param int $step
1218+
* @param positive-int $size
1219+
* @param positive-int $step
12201220
* @return static<int, static>
1221+
*
1222+
* @throws \InvalidArgumentException
12211223
*/
12221224
public function sliding($size = 2, $step = 1)
12231225
{
1226+
if ($size < 1) {
1227+
throw new InvalidArgumentException('Size value must be at least 1.');
1228+
} elseif ($step < 1) {
1229+
throw new InvalidArgumentException('Step value must be at least 1.');
1230+
}
1231+
12241232
return new static(function () use ($size, $step) {
12251233
$iterator = $this->getIterator();
12261234

tests/Support/SupportCollectionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,38 @@ public function testSliding($collection)
447447
$this->assertInstanceOf($collection, $chunks);
448448
$this->assertInstanceOf($collection, $chunks->first());
449449
$this->assertInstanceOf($collection, $chunks->skip(1)->first());
450+
451+
// Test invalid size parameter (size must be at least 1)
452+
// instead of throwing an error. Now it throws InvalidArgumentException.
453+
try {
454+
$collection::times(5)->sliding(0, 1)->toArray();
455+
$this->fail('Expected InvalidArgumentException for size = 0');
456+
} catch (\InvalidArgumentException $e) {
457+
$this->assertSame('Size value must be at least 1.', $e->getMessage());
458+
}
459+
460+
try {
461+
$collection::times(5)->sliding(-1, 1)->toArray();
462+
$this->fail('Expected InvalidArgumentException for size = -1');
463+
} catch (\InvalidArgumentException $e) {
464+
$this->assertSame('Size value must be at least 1.', $e->getMessage());
465+
}
466+
467+
// Test invalid step parameter (step must be at least 1)
468+
// Now it throws InvalidArgumentException with an error message.
469+
try {
470+
$collection::times(5)->sliding(2, 0)->toArray();
471+
$this->fail('Expected InvalidArgumentException for step = 0');
472+
} catch (\InvalidArgumentException $e) {
473+
$this->assertSame('Step value must be at least 1.', $e->getMessage());
474+
}
475+
476+
try {
477+
$collection::times(5)->sliding(2, -1)->toArray();
478+
$this->fail('Expected InvalidArgumentException for step = -1');
479+
} catch (\InvalidArgumentException $e) {
480+
$this->assertSame('Step value must be at least 1.', $e->getMessage());
481+
}
450482
}
451483

452484
#[DataProvider('collectionClassProvider')]

0 commit comments

Comments
 (0)