Browse Source

Document avg() and median() with 0 rows and null values

Corey Taylor 5 years ago
parent
commit
a58799dd57

+ 6 - 0
src/Collection/CollectionInterface.php

@@ -305,6 +305,9 @@ interface CollectionInterface extends Iterator, JsonSerializable
      * // Total: 2
      * ```
      *
+     * The average of an empty set or 0 rows is `null`. Collections with `null`
+     * values are not considered empty.
+     *
      * @param string|callable|null $path The property name to sum or a function
      * If no value is passed, an identity function will be used.
      * that will return the value of the property to sum.
@@ -335,6 +338,9 @@ interface CollectionInterface extends Iterator, JsonSerializable
      * // Total: 2.5
      * ```
      *
+     * The median of an empty set or 0 rows is `null`. Collections with `null`
+     * values are not considered empty.
+     *
      * @param string|callable|null $path The property name to sum or a function
      * If no value is passed, an identity function will be used.
      * that will return the value of the property to sum.

+ 6 - 0
tests/TestCase/Collection/CollectionTest.php

@@ -87,6 +87,9 @@ class CollectionTest extends TestCase
     {
         $collection = new Collection([]);
         $this->assertNull($collection->avg());
+
+        $collection = new Collection([null, null]);
+        $this->assertSame(0, $collection->avg());
     }
 
     /**
@@ -152,6 +155,9 @@ class CollectionTest extends TestCase
     {
         $collection = new Collection([]);
         $this->assertNull($collection->median());
+
+        $collection = new Collection([null, null]);
+        $this->assertSame(0, $collection->median());
     }
 
     /**