Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, static>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might even reflect that in the return type:

Suggested change
* @return static<int, static>
* @return $size is positive-int ? $step is positive-int ? static<int, static> : never : never

*
* @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));
Expand Down
12 changes: 10 additions & 2 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, 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();

Expand Down
32 changes: 32 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down