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

[8.x] Collection prevent null being added to resulting collection with pop and shift #38265

24 changes: 18 additions & 6 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,15 +788,21 @@ public function only($keys)
/**
* Get and remove the last N items from the collection.
*
* @param int $count
* @param int|null $count
* @return mixed
*/
public function pop($count = 1)
public function pop($count = null)
Copy link
Member

Choose a reason for hiding this comment

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

This is a breaking change.

Copy link
Author

Choose a reason for hiding this comment

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

wow! that was a fast review!!!!

{
if ($count === 1) {
if (is_null($count)) {
return array_pop($this->items);
}

if ($count < 1 || $this->count() === 0) {
return new static([]);
}

$count = $count > $this->count() ? $this->count() : $count;

$results = [];

foreach (range(1, $count) as $item) {
Expand Down Expand Up @@ -952,17 +958,23 @@ public function search($value, $strict = false)
/**
* Get and remove the first N items from the collection.
*
* @param int $count
* @param int|null $count
* @return mixed
*/
public function shift($count = 1)
public function shift($count = null)
Copy link
Member

Choose a reason for hiding this comment

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

This is a breaking change.

{
if ($count === 1) {
if (is_null($count)) {
return array_shift($this->items);
}

if ($count < 1 || $this->count() === 0) {
return new static([]);
}

$results = [];

$count = $count > $this->count() ? $this->count() : $count;

foreach (range(1, $count) as $item) {
array_push($results, array_shift($this->items));
}
Expand Down
43 changes: 43 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,28 @@ public function testPopReturnsAndRemovesLastXItemsInCollection()
$this->assertSame('foo', $c->first());
}

public function testPopOnlyReturnsCollectionItemsWhenParamGreaterThanCollectionLength()
{
$c = new Collection(['foo', 'bar']);
$this->assertEquals(new Collection(['bar', 'foo']), $c->pop(3));
$this->assertEmpty($c);
}

public function testPopReturnsEmptyCollectionWhenParameterLessThanOne()
{
$c = new Collection(['foo', 'bar', 'baz']);

$this->assertEquals(new Collection([]), $c->pop(0));
$this->assertSame('foo', $c->first());
}

public function testPopReturnsNullWhenNoItemsInCollectionAndParamSupplied()
{
$c = new Collection([]);
$this->assertEquals(new Collection([]), $c->pop(2));
$this->assertCount(0, $c);
}

public function testShiftReturnsAndRemovesFirstItemInCollection()
{
$data = new Collection(['Taylor', 'Otwell']);
Expand All @@ -251,6 +273,27 @@ public function testShiftReturnsAndRemovesFirstXItemsInCollection()
$this->assertSame('baz', $data->first());
}

public function testShiftOnlyReturnsCollectionItemsWhenParamGreaterThanCollectionLength()
{
$c = new Collection(['foo', 'bar']);
$this->assertEquals(new Collection(['foo', 'bar']), $c->shift(3));
$this->assertEmpty($c);
}

public function testShiftReturnsEmptyColeectionWhenParameterLessThanOne()
{
$c = new Collection(['foo', 'bar']);
$this->assertEquals(new Collection([]), $c->shift(0));
$this->assertCount(2, $c);
}

public function testShiftReturnsNullWhenNoItemsInCollectionAndParamSupplied()
{
$c = new Collection([]);
$this->assertEquals(new Collection([]), $c->shift(2));
$this->assertCount(0, $c);
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down