From 1b443bc0be062314f4f16fa5c9f1601af05ef9d0 Mon Sep 17 00:00:00 2001 From: Jacek Kuzemczak Date: Thu, 27 Jul 2023 11:27:06 +0100 Subject: [PATCH 1/6] Fixed inconsistent string handling in SUM() implementations --- src/PhpSpreadsheet/Calculation/MathTrig/Sum.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php b/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php index 7b1ffa9eba..56da8305fe 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php @@ -28,7 +28,7 @@ public static function sumIgnoringStrings(...$args) foreach (Functions::flattenArray($args) as $arg) { // Is it a numeric value? if (is_numeric($arg)) { - $returnValue += $arg; + $returnValue += (float) $arg; } elseif (ErrorValue::isError($arg)) { return $arg; } @@ -58,7 +58,7 @@ public static function sumErroringStrings(...$args) // Is it a numeric value? if (is_numeric($arg) || empty($arg)) { if (is_string($arg)) { - $arg = (int) $arg; + $arg = (float) $arg; } $returnValue += $arg; } elseif (is_bool($arg)) { From 0100427f7ed8390441ed05d00c31d089fed1c0b5 Mon Sep 17 00:00:00 2001 From: Jacek Kuzemczak Date: Thu, 27 Jul 2023 11:42:42 +0100 Subject: [PATCH 2/6] Use assertEqual instead of assertSame to allow ints and floats with the same value --- tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php b/tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php index e9f45104c6..832b0fe1f7 100644 --- a/tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php @@ -31,7 +31,7 @@ public function testRefError($expected, string $formula): void $sheet1->getCell('C2')->setValue('=Sheet2!A1'); $sheet1->getCell('C3')->setValue('=Sheet2!A2'); $sheet1->getCell('H1')->setValue($formula); - self::assertSame($expected, $sheet1->getCell('H1')->getCalculatedValue()); + self::assertEquals($expected, $sheet1->getCell('H1')->getCalculatedValue()); $spreadsheet->disconnectWorksheets(); } From 01663cd64c10e3733e001d20e5cb4892eae16b7e Mon Sep 17 00:00:00 2001 From: Jacek Kuzemczak Date: Mon, 31 Jul 2023 09:57:44 +0100 Subject: [PATCH 3/6] Removewd special handling of empty strings and use default casts --- src/PhpSpreadsheet/Calculation/MathTrig/Sum.php | 11 ++++------- .../PhpSpreadsheetTests/Calculation/RefErrorTest.php | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php b/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php index 56da8305fe..16aaba3ca5 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php @@ -18,7 +18,7 @@ class Sum * * @param mixed ...$args Data values * - * @return float|string + * @return float|integer|string */ public static function sumIgnoringStrings(...$args) { @@ -28,7 +28,7 @@ public static function sumIgnoringStrings(...$args) foreach (Functions::flattenArray($args) as $arg) { // Is it a numeric value? if (is_numeric($arg)) { - $returnValue += (float) $arg; + $returnValue += $arg; } elseif (ErrorValue::isError($arg)) { return $arg; } @@ -47,7 +47,7 @@ public static function sumIgnoringStrings(...$args) * * @param mixed ...$args Data values * - * @return float|string + * @return float|integer|string */ public static function sumErroringStrings(...$args) { @@ -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 = (float) $arg; - } + if (is_numeric($arg)) { $returnValue += $arg; } elseif (is_bool($arg)) { $returnValue += (int) $arg; diff --git a/tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php b/tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php index 832b0fe1f7..e9f45104c6 100644 --- a/tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/RefErrorTest.php @@ -31,7 +31,7 @@ public function testRefError($expected, string $formula): void $sheet1->getCell('C2')->setValue('=Sheet2!A1'); $sheet1->getCell('C3')->setValue('=Sheet2!A2'); $sheet1->getCell('H1')->setValue($formula); - self::assertEquals($expected, $sheet1->getCell('H1')->getCalculatedValue()); + self::assertSame($expected, $sheet1->getCell('H1')->getCalculatedValue()); $spreadsheet->disconnectWorksheets(); } From 489b95995672a7ddd6897a00e1d92e8a84b591b8 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Thu, 10 Aug 2023 17:09:16 -0700 Subject: [PATCH 4/6] Eliminate Phpstan errors --- src/PhpSpreadsheet/Calculation/MathTrig/Sum.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php b/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php index 16aaba3ca5..37070e2434 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Sum.php @@ -18,7 +18,7 @@ class Sum * * @param mixed ...$args Data values * - * @return float|integer|string + * @return float|int|string */ public static function sumIgnoringStrings(...$args) { @@ -47,7 +47,7 @@ public static function sumIgnoringStrings(...$args) * * @param mixed ...$args Data values * - * @return float|integer|string + * @return float|int|string */ public static function sumErroringStrings(...$args) { From 4238c9d436a116e25c52801251f879403494374c Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Thu, 10 Aug 2023 17:14:02 -0700 Subject: [PATCH 5/6] More Test Cases --- tests/data/Calculation/MathTrig/SUM.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/data/Calculation/MathTrig/SUM.php b/tests/data/Calculation/MathTrig/SUM.php index 0c54613e2a..ad2c78e0ce 100644 --- a/tests/data/Calculation/MathTrig/SUM.php +++ b/tests/data/Calculation/MathTrig/SUM.php @@ -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], From c80edfbe3057995de823ea27e4682723fe295e27 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Thu, 10 Aug 2023 17:17:08 -0700 Subject: [PATCH 6/6] More New Tests --- tests/data/Calculation/MathTrig/SUMLITERALS.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/data/Calculation/MathTrig/SUMLITERALS.php b/tests/data/Calculation/MathTrig/SUMLITERALS.php index fd184ebd50..0ea5938617 100644 --- a/tests/data/Calculation/MathTrig/SUMLITERALS.php +++ b/tests/data/Calculation/MathTrig/SUMLITERALS.php @@ -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'],