Skip to content

Commit

Permalink
feat(math): narrow return type for min(), max(), median() and `…
Browse files Browse the repository at this point in the history
…mean()` (#464)
  • Loading branch information
simPod authored Apr 6, 2024
1 parent 765f499 commit a33de34
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/component/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
- [max](./../../src/Psl/Math/max.php#L19)
- [max_by](./../../src/Psl/Math/max_by.php#L24)
- [maxva](./../../src/Psl/Math/maxva.php#L20)
- [mean](./../../src/Psl/Math/mean.php#L18)
- [median](./../../src/Psl/Math/median.php#L19)
- [mean](./../../src/Psl/Math/mean.php#L20)
- [median](./../../src/Psl/Math/median.php#L21)
- [min](./../../src/Psl/Math/min.php#L19)
- [min_by](./../../src/Psl/Math/min_by.php#L24)
- [minva](./../../src/Psl/Math/minva.php#L20)
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Math/max.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @param list<T> $numbers
*
* @return T|null
* @return ($numbers is non-empty-list<T> ? T : null)
*
* @pure
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Psl/Math/mean.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*
* @param list<int|float> $numbers
*
* @return ($numbers is non-empty-list ? float : null)
*
* @pure
*/
function mean(array $numbers): ?float
function mean(array $numbers): float|null
{
$count = (float) count($numbers);
if (0.0 === $count) {
Expand Down
6 changes: 4 additions & 2 deletions src/Psl/Math/median.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
*
* @param list<int|float> $numbers
*
* @return ($numbers is non-empty-list ? float : null)
*
* @pure
*/
function median(array $numbers): ?float
function median(array $numbers): float|null
{
sort($numbers);
$count = count($numbers);
Expand All @@ -29,7 +31,7 @@ function median(array $numbers): ?float
if (0 === $count % 2) {
return mean(
[$numbers[$middle_index], $numbers[$middle_index - 1]]
) ?? 0.0;
);
}

return (float) $numbers[$middle_index];
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Math/min.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @param list<T> $numbers
*
* @return T|null
* @return ($numbers is non-empty-list<T> ? T : null)
*
* @pure
*/
Expand Down

0 comments on commit a33de34

Please sign in to comment.