Skip to content
Browse files

Removed null return values from aggregation methods.

  • Loading branch information...
1 parent 26912a4 commit 0c7cb91b8366c6ffadf6766c836f136279a87bd6 @hype- hype- committed Jan 23, 2013
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,39 +238,43 @@ 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);
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function sum()
{
- return $this->applyOrNull('array_sum');
+ return $this->isEmpty() ? 0 : array_sum($this->_elements);
}
/**
* {@inheritdoc}
*/
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,14 +641,28 @@ public function shouldBeAbleToGetMinimumOfValues($elements, $expected)
public function minimumProvider()
{
return array(
- array(array(), null),
array(array(1, 2, 3), 1),
array(array(5, 2), 2),
);
}
/**
* @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,14 +678,28 @@ public function shouldBeAbleToGetMaximumOfValues($elements, $expected)
public function maximumProvider()
{
return array(
- array(array(), null),
array(array(1, 2, 3), 3),
array(array(5, 2), 5),
);
}
/**
* @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),

0 comments on commit 0c7cb91

Please sign in to comment.
Something went wrong with that request. Please try again.