Skip to content

Potential issue with collection split method #22090

@helmut

Description

@helmut
  • Laravel Version: 5.5
  • PHP Version: 7.01

Description:

Noticed a bit of an issue with the collection split method that we may want to fix. Of course this might not be an issue but my understanding of the method might be wrong.

I've got a collection with 4 items and when I run split(3) I expect to get back 3 groups. Instead I get back 2 groups.

Steps To Reproduce:

collect(['a', 'b', 'c', 'd'])->split(3);

// expected 
[['a', 'b'], ['c'], ['d']]

// actual
[['a', 'b'], ['c', 'd']]

Does anyone else agree with my view that when you split into 3 groups and you have at least 3 items in the collection it should return 3 groups?

These work as expected:

collect(['a', 'b', 'c'])->split(3);

// returns
[['a'], ['b'], ['c']]
collect(['a', 'b', 'c', 'd', 'e'])->split(3);

// returns
[['a', 'b'], ['c', 'd'], ['e']]

Here is a failing test if that helps:

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions