Skip to content

Commit

Permalink
Fixed inconsistent string handling in SUM() implementations, issue #3652
Browse files Browse the repository at this point in the history
 (#3653)

* Fixed inconsistent string handling in SUM() implementations

* Use assertEqual instead of assertSame to allow ints and floats with the
same value

* Removewd special handling of empty strings and use default casts

* Eliminate Phpstan errors

* More Test Cases

* More New Tests

---------

Co-authored-by: oleibman <[email protected]>
  • Loading branch information
betterphp and oleibman authored Aug 15, 2023
1 parent 6f5ef03 commit dd97b8f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/PhpSpreadsheet/Calculation/MathTrig/Sum.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Sum
*
* @param mixed ...$args Data values
*
* @return float|string
* @return float|int|string
*/
public static function sumIgnoringStrings(...$args)
{
Expand Down Expand Up @@ -47,7 +47,7 @@ public static function sumIgnoringStrings(...$args)
*
* @param mixed ...$args Data values
*
* @return float|string
* @return float|int|string
*/
public static function sumErroringStrings(...$args)
{
Expand All @@ -56,10 +56,7 @@ public static function sumErroringStrings(...$args)
$aArgs = Functions::flattenArrayIndexed($args);
foreach ($aArgs as $k => $arg) {
// Is it a numeric value?
if (is_numeric($arg) || empty($arg)) {
if (is_string($arg)) {
$arg = (int) $arg;
}
if (is_numeric($arg)) {
$returnValue += $arg;
} elseif (is_bool($arg)) {
$returnValue += (int) $arg;
Expand Down
2 changes: 2 additions & 0 deletions tests/data/Calculation/MathTrig/SUM.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[52, 5, 15, 30, 2],
[53.1, 5.7, 15, 30, 2.4],
[52.1, 5.7, '14', 30, 2.4],
[52.2, 5.7, '14.1', 30, 2.4],
[38.1, 5.7, 'X', 30, 2.4], // error if entered in formula, but not in cell
[38.1, 5.7, '', 30, 2.4], // error if entered in formula, but not in cell
[38.1, 5.7, null, 30, 2.4],
[38.1, 5.7, false, 30, 2.4],
[39.1, 5.7, true, 30, 2.4],
Expand Down
2 changes: 2 additions & 0 deletions tests/data/Calculation/MathTrig/SUMLITERALS.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[52, '5, 15, 30, 2'],
[53.1, '5.7, 15, 30, 2.4'],
[52.1, '5.7, "14", 30, 2.4'],
[52.2, '5.7, "14.1", 30, 2.4'],
['#VALUE!', '5.7, "X", 30, 2.4'], // error if entered in formula, but not in cell
['#VALUE!', '5.7, "", 30, 2.4'], // error if entered in formula, but not in cell
[38.1, '5.7, , 30, 2.4'],
[38.1, '5.7, false, 30, 2.4'],
[39.1, '5.7, true, 30, 2.4'],
Expand Down

0 comments on commit dd97b8f

Please sign in to comment.