From 9a208b31d8b1550aacc32f2137ae2a6e785fd346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A4ntz=20Miccoli?= Date: Fri, 1 Feb 2019 15:01:19 +0100 Subject: [PATCH] Fix a SUMIF warning when having different length of arrays provided as input Closes #873 --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Calculation/MathTrig.php | 5 ++-- tests/data/Calculation/MathTrig/SUMIF.php | 26 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cf34683..04a31add 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Fixed - Whitelist `tsv` extension when opening CSV files [#429](https://github.com/PHPOffice/PhpSpreadsheet/issues/429) +- Fix a SUMIF warning with some versions of PHP when having different length of arrays provided as input [#873](https://github.com/PHPOffice/PhpSpreadsheet/pull/873) ## [1.7.0] - 2019-05-26 diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 9170196b..a06f3a38 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1224,11 +1224,12 @@ class MathTrig } $testCondition = '=' . $arg . $condition; + $sumValue = array_key_exists($key, $sumArgs) ? $sumArgs[$key] : 0; - if (is_numeric($sumArgs[$key]) && + if (is_numeric($sumValue) && Calculation::getInstance()->_calculateFormulaValue($testCondition)) { // Is it a value within our criteria and only numeric can be added to the result - $returnValue += $sumArgs[$key]; + $returnValue += $sumValue; } } diff --git a/tests/data/Calculation/MathTrig/SUMIF.php b/tests/data/Calculation/MathTrig/SUMIF.php index 85bec2a5..d2d916dc 100644 --- a/tests/data/Calculation/MathTrig/SUMIF.php +++ b/tests/data/Calculation/MathTrig/SUMIF.php @@ -94,4 +94,30 @@ return [ [1], ], ], + [ + 3, + [ + [1], + [0], + [1] + ], + 1, + [ + [3], + [4] // less elements in sum array + ] + ], + [ + 3, + [ + [1], + [0] // less elements in condition array + ], + 1, + [ + [3], + [4], + [5] + ] + ] ];