Skip to content

Commit b895720

Browse files
authored
Merge pull request PHPOffice#2835 from PHPOffice/Issue-2833_Null-handling-in-PRODUCT()-Function
Filter null values in PRODUCT() function > It all looks good to me. ty
2 parents 9aa1708 + a59b29a commit b895720

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/PhpSpreadsheet/Calculation/MathTrig/Operations.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,27 @@ public static function power($x, $y)
102102
*/
103103
public static function product(...$args)
104104
{
105+
$args = array_filter(
106+
Functions::flattenArray($args),
107+
function ($value) {
108+
return $value !== null;
109+
}
110+
);
111+
105112
// Return value
106-
$returnValue = null;
113+
$returnValue = (count($args) === 0) ? 0.0 : 1.0;
107114

108115
// Loop through arguments
109-
foreach (Functions::flattenArray($args) as $arg) {
116+
foreach ($args as $arg) {
110117
// Is it a numeric value?
111-
if (is_numeric($arg) || $arg === null) {
112-
if ($returnValue === null) {
113-
$returnValue = $arg;
114-
} else {
115-
$returnValue *= $arg;
116-
}
118+
if (is_numeric($arg)) {
119+
$returnValue *= $arg;
117120
} else {
118121
return ExcelError::VALUE();
119122
}
120123
}
121124

122-
// Return
123-
if ($returnValue === null) {
124-
return 0;
125-
}
126-
127-
return $returnValue;
125+
return (float) $returnValue;
128126
}
129127

130128
/**

tests/data/Calculation/MathTrig/PRODUCT.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,29 @@
4949
-2,
5050
],
5151
[
52-
0,
52+
31.25,
5353
12.5,
5454
null,
5555
2.5,
5656
],
5757
[
58-
0,
58+
31.25,
5959
12.5,
6060
2.5,
6161
null,
6262
],
63+
[
64+
12.5,
65+
12.5,
66+
null,
67+
null,
68+
],
69+
[
70+
0.0,
71+
null,
72+
null,
73+
null,
74+
],
6375
['#VALUE!', 1, 'y', 3],
6476
[6, 1, '2', 3],
6577
];

0 commit comments

Comments
 (0)