Skip to content

Commit

Permalink
Collection::min() incorrectly excludes 0 when calculating minimum (#1…
Browse files Browse the repository at this point in the history
…6821)

* Add failing test

* Fix BC break in min()
  • Loading branch information
thecrypticace authored and taylorotwell committed Dec 15, 2016
1 parent 33edf4d commit 2463e41
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,9 @@ public function max($callback = null)
{
$callback = $this->valueRetriever($callback);

return $this->filter()->reduce(function ($result, $item) use ($callback) {
return $this->filter(function ($value) {
return ! is_null($value);
})->reduce(function ($result, $item) use ($callback) {
$value = $callback($item);

return is_null($result) || $value > $result ? $value : $result;
Expand Down Expand Up @@ -699,7 +701,9 @@ public function min($callback = null)
{
$callback = $this->valueRetriever($callback);

return $this->filter()->reduce(function ($result, $item) use ($callback) {
return $this->filter(function ($value) {
return ! is_null($value);
})->reduce(function ($result, $item) use ($callback) {
$value = $callback($item);

return is_null($result) || $value < $result ? $value : $result;
Expand Down
3 changes: 3 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,9 @@ public function testGettingMinItemsFromCollection()
$c = new Collection([1, null, 3, 4, 5]);
$this->assertEquals(1, $c->min());

$c = new Collection([0, 1, 2, 3, 4]);
$this->assertEquals(0, $c->min());

$c = new Collection();
$this->assertNull($c->min());
}
Expand Down

0 comments on commit 2463e41

Please sign in to comment.