Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed null return values from aggregation methods.

  • Loading branch information...
commit 0c7cb91b8366c6ffadf6766c836f136279a87bd6 1 parent 26912a4
@hype- hype- authored
View
21 library/Xi/Collections/Collection.php
@@ -1,6 +1,11 @@
<?php
+
namespace Xi\Collections;
+use Traversable;
+use Closure;
+use UnderflowException;
+
/**
* Extends the Enumerable collection operations to a superset that includes
* operations that yield other collections in return. This means Collections
@@ -223,28 +228,32 @@ public function add($value, $key = null);
/**
* Returns the minimum value in the collection.
*
- * @return mixed|null
+ * @return mixed
+ * @throws UnderflowException If the collection is empty.
*/
public function min();
/**
* Returns the maximum value in the collection.
*
- * @return mixed|null
+ * @return mixed
+ * @throws UnderflowException If the collection is empty.
*/
public function max();
/**
- * Returns the sum of values in the collection.
+ * Returns the sum of values in the collection. The sum of an empty
+ * collection is 0.
*
- * @return mixed|null
+ * @return mixed
*/
public function sum();
/**
- * Returns the product of values in the collection.
+ * Returns the product of values in the collection. The product of an empty
+ * collection is 1.
*
- * @return mixed|null
+ * @return mixed
*/
public function product();
View
30 library/Xi/Collections/Collection/AbstractCollection.php
@@ -1,9 +1,11 @@
<?php
+
namespace Xi\Collections\Collection;
-use Xi\Collections\Collection,
- Xi\Collections\Util\Functions,
- Xi\Collections\Enumerable\AbstractEnumerable;
+use Xi\Collections\Collection;
+use Xi\Collections\Util\Functions;
+use Xi\Collections\Enumerable\AbstractEnumerable;
+use UnderflowException;
/**
* Provides a trivial implementation of a Collection
@@ -204,6 +206,12 @@ public function add($value, $key = null)
*/
public function min()
{
+ if ($this->isEmpty()) {
+ throw new UnderflowException(
+ 'Can not get a minimum value on an empty collection.'
+ );
+ }
+
$min = null;
foreach ($this as $value) {
@@ -220,6 +228,12 @@ public function min()
*/
public function max()
{
+ if ($this->isEmpty()) {
+ throw new UnderflowException(
+ 'Can not get a maximum value on an empty collection.'
+ );
+ }
+
$max = null;
foreach ($this as $value) {
@@ -236,7 +250,7 @@ public function max()
*/
public function sum()
{
- $sum = null;
+ $sum = 0;
foreach ($this as $value) {
$sum += $value;
@@ -250,14 +264,10 @@ public function sum()
*/
public function product()
{
- $product = null;
+ $product = 1;
foreach ($this as $value) {
- if ($product === null) {
- $product = $value;
- } else {
- $product *= $value;
- }
+ $product *= $value;
}
return $product;
View
36 library/Xi/Collections/Collection/ArrayCollection.php
@@ -1,9 +1,11 @@
<?php
+
namespace Xi\Collections\Collection;
-use Xi\Collections\Collection,
- Xi\Collections\Util\Functions,
- Xi\Collections\Enumerable\ArrayEnumerable;
+use Xi\Collections\Collection;
+use Xi\Collections\Util\Functions;
+use Xi\Collections\Enumerable\ArrayEnumerable;
+use UnderflowException;
/**
* Implements the Collection operations with native array functions wherever
@@ -236,7 +238,13 @@ public function add($value, $key = null)
*/
public function min()
{
- return $this->applyOrNull('min');
+ if ($this->isEmpty()) {
+ throw new UnderflowException(
+ 'Can not get a minimum value on an empty collection.'
+ );
+ }
+
+ return min($this->_elements);
}
/**
@@ -244,7 +252,13 @@ public function min()
*/
public function max()
{
- return $this->applyOrNull('max');
+ if ($this->isEmpty()) {
+ throw new UnderflowException(
+ 'Can not get a maximum value on an empty collection.'
+ );
+ }
+
+ return max($this->_elements);
}
/**
@@ -252,7 +266,7 @@ public function max()
*/
public function sum()
{
- return $this->applyOrNull('array_sum');
+ return $this->isEmpty() ? 0 : array_sum($this->_elements);
}
/**
@@ -260,15 +274,7 @@ public function sum()
*/
public function product()
{
- return $this->applyOrNull('array_product');
- }
-
- /**
- * {@inheritdoc}
- */
- private function applyOrNull($callback)
- {
- return !empty($this->_elements) ? $callback($this->_elements) : null;
+ return $this->isEmpty() ? 1 : array_product($this->_elements);
}
/**
View
36 tests/Xi/Collections/Collection/AbstractCollectionTest.php
@@ -641,7 +641,6 @@ public function shouldBeAbleToGetMinimumOfValues($elements, $expected)
public function minimumProvider()
{
return array(
- array(array(), null),
array(array(1, 2, 3), 1),
array(array(5, 2), 2),
);
@@ -649,6 +648,21 @@ public function minimumProvider()
/**
* @test
+ */
+ public function minimumOfValuesOnAnEmptyCollectionShouldThrowAnException()
+ {
+ $collection = $this->getCollection();
+
+ $this->setExpectedException(
+ 'UnderflowException',
+ 'Can not get a minimum value on an empty collection.'
+ );
+
+ $collection->min();
+ }
+
+ /**
+ * @test
* @dataProvider maximumProvider
*/
public function shouldBeAbleToGetMaximumOfValues($elements, $expected)
@@ -664,7 +678,6 @@ public function shouldBeAbleToGetMaximumOfValues($elements, $expected)
public function maximumProvider()
{
return array(
- array(array(), null),
array(array(1, 2, 3), 3),
array(array(5, 2), 5),
);
@@ -672,6 +685,21 @@ public function maximumProvider()
/**
* @test
+ */
+ public function maximumOfValuesOnAnEmptyCollectionShouldThrowAnException()
+ {
+ $collection = $this->getCollection();
+
+ $this->setExpectedException(
+ 'UnderflowException',
+ 'Can not get a maximum value on an empty collection.'
+ );
+
+ $collection->max();
+ }
+
+ /**
+ * @test
* @dataProvider sumProvider
*/
public function shouldBeAbleToGetSumOfValues($elements, $expected)
@@ -687,7 +715,7 @@ public function shouldBeAbleToGetSumOfValues($elements, $expected)
public function sumProvider()
{
return array(
- array(array(), null),
+ array(array(), 0),
array(array(0), 0),
array(array(1, 2), 3),
array(array(2, 4, 6), 12),
@@ -711,7 +739,7 @@ public function shouldBeAbleToGetProductOfValues($elements, $expected)
public function productProvider()
{
return array(
- array(array(), null),
+ array(array(), 1),
array(array(0), 0),
array(array(1, 2), 2),
array(array(2, 3, 5), 30),
Please sign in to comment.
Something went wrong with that request. Please try again.