Skip to content

Commit c2c04dc

Browse files
Support for PHP 8.0
1 parent cfdab1f commit c2c04dc

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.8.0 - UPCOMING
4+
5+
* Support for PHP 8.0.
6+
37
## 2.5.0 - 2019-12-30
48

59
* Full support for PHP 7.0-7.4.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
],
1414

1515
"require": {
16-
"php": ">=5.4.0",
16+
"php": "^5.4 || ^7.0 || ^8.0",
1717
"symfony/polyfill-mbstring": "^1.17"
1818
},
1919

2020
"require-dev": {
2121
"composer/xdebug-handler": "^1.4",
22-
"phpunit/phpunit": "^4.8.36|^7.5.15"
22+
"phpunit/phpunit": "^4.8.36 || ^7.5.15"
2323
},
2424

2525
"autoload": {

src/FnDispatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private function fn_avg(array $args)
4545
{
4646
$this->validate('avg', $args, [['array']]);
4747
$sum = $this->reduce('avg:0', $args[0], ['number'], function ($a, $b) {
48-
return $a + $b;
48+
return Utils::add($a, $b);
4949
});
5050
return $args[0] ? ($sum / count($args[0])) : null;
5151
}
@@ -172,7 +172,7 @@ private function fn_sum(array $args)
172172
{
173173
$this->validate('sum', $args, [['array']]);
174174
$fn = function ($a, $b) {
175-
return $a + $b;
175+
return Utils::add($a, $b);
176176
};
177177
return $this->reduce('sum:0', $args[0], ['number'], $fn);
178178
}

src/Utils.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,31 @@ public static function isEqual($a, $b)
126126
}
127127
}
128128

129+
/**
130+
* Safely add together two values.
131+
*
132+
* @param mixed $a First value to add
133+
* @param mixed $b Second value to add
134+
*
135+
* @return int|float
136+
*/
137+
public static function add($a, $b)
138+
{
139+
if (is_numeric($a)) {
140+
if (is_numeric($b)) {
141+
return $a + $b;
142+
} else {
143+
return $a;
144+
}
145+
} else {
146+
if (is_numeric($b)) {
147+
return $b;
148+
} else {
149+
return 0;
150+
}
151+
}
152+
}
153+
129154
/**
130155
* JMESPath requires a stable sorting algorithm, so here we'll implement
131156
* a simple Schwartzian transform that uses array index positions as tie

0 commit comments

Comments
 (0)