Skip to content

Commit ea7d447

Browse files
committed
Fix branch pruning resolution of non boolean conditions
1 parent 9d6736d commit ea7d447

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1313

1414
### Fixed
1515

16-
- ...
16+
- Fix branch pruning handling of non boolean conditions
1717

1818
## [1.9.0] - 2019-08-17
1919

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3804,13 +3804,19 @@ private function processTokenStack($tokens, $cellID = null, Cell $pCell = null)
38043804
if ($this->branchPruningEnabled && isset($tokenData['onlyIf'])) {
38053805
$onlyIfStoreKey = $tokenData['onlyIf'];
38063806
$storeValue = $branchStore[$onlyIfStoreKey] ?? null;
3807+
$storeValueAsBool = ($storeValue === null) ?
3808+
true : (bool) Functions::flattenSingleValue($storeValue);
38073809
if (is_array($storeValue)) {
38083810
$wrappedItem = end($storeValue);
38093811
$storeValue = end($wrappedItem);
38103812
}
38113813

3812-
if (isset($storeValue) && (($storeValue !== true)
3813-
|| ($storeValue === 'Pruned branch'))
3814+
if (isset($storeValue)
3815+
&& (
3816+
!$storeValueAsBool
3817+
|| Functions::isError($storeValue)
3818+
|| ($storeValue === 'Pruned branch')
3819+
)
38143820
) {
38153821
// If branching value is not true, we don't need to compute
38163822
if (!isset($fakedForBranchPruning['onlyIf-' . $onlyIfStoreKey])) {
@@ -3833,12 +3839,17 @@ private function processTokenStack($tokens, $cellID = null, Cell $pCell = null)
38333839
if ($this->branchPruningEnabled && isset($tokenData['onlyIfNot'])) {
38343840
$onlyIfNotStoreKey = $tokenData['onlyIfNot'];
38353841
$storeValue = $branchStore[$onlyIfNotStoreKey] ?? null;
3842+
$storeValueAsBool = ($storeValue === null) ?
3843+
true : (bool) Functions::flattenSingleValue($storeValue);
38363844
if (is_array($storeValue)) {
38373845
$wrappedItem = end($storeValue);
38383846
$storeValue = end($wrappedItem);
38393847
}
3840-
if (isset($storeValue) && ($storeValue
3841-
|| ($storeValue === 'Pruned branch'))
3848+
if (isset($storeValue)
3849+
&& (
3850+
$storeValueAsBool
3851+
|| Functions::isError($storeValue)
3852+
|| ($storeValue === 'Pruned branch'))
38423853
) {
38433854
// If branching value is true, we don't need to compute
38443855
if (!isset($fakedForBranchPruning['onlyIfNot-' . $onlyIfNotStoreKey])) {

tests/data/Calculation/Calculation.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ function calculationTestDataGenerator()
5555
$formula3 = '=IF(A4="take A", A3, B3)';
5656
$set8 = [4, $dataArray5, $formula3, 'E5', ['A3'], ['B3']];
5757

58-
return [$set0, $set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8];
58+
$dataArray6 = [
59+
['=IF(22,"a","b")']
60+
];
61+
$set9 = ['a', $dataArray6, '=A1', 'A2'];
62+
63+
return [
64+
$set0, $set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8, $set9
65+
];
5966
}
6067

6168
return calculationTestDataGenerator();

0 commit comments

Comments
 (0)