Skip to content

Commit

Permalink
Support for PHP 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Jul 31, 2020
1 parent cfdab1f commit c2c04dc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.8.0 - UPCOMING

* Support for PHP 8.0.

## 2.5.0 - 2019-12-30

* Full support for PHP 7.0-7.4.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
],

"require": {
"php": ">=5.4.0",
"php": "^5.4 || ^7.0 || ^8.0",
"symfony/polyfill-mbstring": "^1.17"
},

"require-dev": {
"composer/xdebug-handler": "^1.4",
"phpunit/phpunit": "^4.8.36|^7.5.15"
"phpunit/phpunit": "^4.8.36 || ^7.5.15"
},

"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions src/FnDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private function fn_avg(array $args)
{
$this->validate('avg', $args, [['array']]);
$sum = $this->reduce('avg:0', $args[0], ['number'], function ($a, $b) {
return $a + $b;
return Utils::add($a, $b);
});
return $args[0] ? ($sum / count($args[0])) : null;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ private function fn_sum(array $args)
{
$this->validate('sum', $args, [['array']]);
$fn = function ($a, $b) {
return $a + $b;
return Utils::add($a, $b);
};
return $this->reduce('sum:0', $args[0], ['number'], $fn);
}
Expand Down
25 changes: 25 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@ public static function isEqual($a, $b)
}
}

/**
* Safely add together two values.
*
* @param mixed $a First value to add
* @param mixed $b Second value to add
*
* @return int|float
*/
public static function add($a, $b)
{
if (is_numeric($a)) {
if (is_numeric($b)) {
return $a + $b;
} else {
return $a;
}
} else {
if (is_numeric($b)) {
return $b;
} else {
return 0;
}
}
}

/**
* JMESPath requires a stable sorting algorithm, so here we'll implement
* a simple Schwartzian transform that uses array index positions as tie
Expand Down

0 comments on commit c2c04dc

Please sign in to comment.