Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Add split method to collection class #15302

Merged
merged 6 commits into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,23 @@ public function slice($offset, $length = null)
return new static(array_slice($this->items, $offset, $length, true));
}

/**
Copy link
Member

Choose a reason for hiding this comment

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

This whole block is indented by one too many spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honest mistake, fixed now (using the patch file from style ci)

Copy link
Member

Choose a reason for hiding this comment

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

Still incorrect.

Copy link
Member

Choose a reason for hiding this comment

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

StyleCI aligned it to your method definition which is also indented incorrectly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wut? Can StyleCI be incorrect? 😄

Copy link
Member

Choose a reason for hiding this comment

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

StyleCI is correct according to Laravel's rules. The phpdoc must be aligned to match the method. There is no fixer to change the alignment of the actual method definition yet.

* Split a collection into a certain number of groups.
*
* @param int $numberOfGroups
Copy link
Member

Choose a reason for hiding this comment

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

missing space after int

* @return static
*/
public function split($numberOfGroups)
{
if ($this->isEmpty()) {
return $this;
}

$groupSize = ceil($this->count() / $numberOfGroups);

return $this->chunk($groupSize);
}

/**
* Chunk the underlying collection array.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,54 @@ public function testCollectonFromTraversableWithKeys()
$collection = new Collection(new \ArrayObject(['foo' => 1, 'bar' => 2, 'baz' => 3]));
$this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $collection->toArray());
}

public function testSplitCollectionWithADivisableCount()
{
$collection = new Collection(['a', 'b', 'c', 'd']);

$this->assertEquals(
[['a', 'b'], ['c', 'd']],
$collection->split(2)->map(function (Collection $chunk) {
return $chunk->values()->toArray();
})->toArray()
);
}

public function testSplitCollectionWithAnUndivisableCount()
{
$collection = new Collection(['a', 'b', 'c']);

$this->assertEquals(
[['a', 'b'], ['c']],
$collection->split(2)->map(function (Collection $chunk) {
return $chunk->values()->toArray();
})->toArray()
);
}

public function testSplitCollectionWithCountLessThenDivisor()
{
$collection = new Collection(['a']);

$this->assertEquals(
[['a']],
$collection->split(2)->map(function (Collection $chunk) {
return $chunk->values()->toArray();
})->toArray()
);
}

public function testSplitEmptyCollection()
{
$collection = new Collection();

$this->assertEquals(
[],
$collection->split(2)->map(function (Collection $chunk) {
return $chunk->values()->toArray();
})->toArray()
);
}
}

class TestAccessorEloquentTestStub
Expand Down