Skip to content

Commit 840a14b

Browse files
frantzmiccoliPowerKiKi
authored andcommitted
Fix branch pruning resolution of non boolean conditions
Closes PHPOffice#1167
1 parent bdafea2 commit 840a14b

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1818
- Call garbage collector after removing a column to prevent stale cached values
1919
- Trying to remove a column that doesn't exist deletes the latest column
2020
- Keep big integer as integer instead of lossely casting to float [#874](https://github.com/PHPOffice/PhpSpreadsheet/pull/874)
21+
- Fix branch pruning handling of non boolean conditions [#1167](https://github.com/PHPOffice/PhpSpreadsheet/pull/1167)
2122

2223
## [1.9.0] - 2019-08-17
2324

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3809,13 +3809,19 @@ private function processTokenStack($tokens, $cellID = null, Cell $pCell = null)
38093809
if ($this->branchPruningEnabled && isset($tokenData['onlyIf'])) {
38103810
$onlyIfStoreKey = $tokenData['onlyIf'];
38113811
$storeValue = $branchStore[$onlyIfStoreKey] ?? null;
3812+
$storeValueAsBool = ($storeValue === null) ?
3813+
true : (bool) Functions::flattenSingleValue($storeValue);
38123814
if (is_array($storeValue)) {
38133815
$wrappedItem = end($storeValue);
38143816
$storeValue = end($wrappedItem);
38153817
}
38163818

3817-
if (isset($storeValue) && (($storeValue !== true)
3818-
|| ($storeValue === 'Pruned branch'))
3819+
if (isset($storeValue)
3820+
&& (
3821+
!$storeValueAsBool
3822+
|| Functions::isError($storeValue)
3823+
|| ($storeValue === 'Pruned branch')
3824+
)
38193825
) {
38203826
// If branching value is not true, we don't need to compute
38213827
if (!isset($fakedForBranchPruning['onlyIf-' . $onlyIfStoreKey])) {
@@ -3838,12 +3844,17 @@ private function processTokenStack($tokens, $cellID = null, Cell $pCell = null)
38383844
if ($this->branchPruningEnabled && isset($tokenData['onlyIfNot'])) {
38393845
$onlyIfNotStoreKey = $tokenData['onlyIfNot'];
38403846
$storeValue = $branchStore[$onlyIfNotStoreKey] ?? null;
3847+
$storeValueAsBool = ($storeValue === null) ?
3848+
true : (bool) Functions::flattenSingleValue($storeValue);
38413849
if (is_array($storeValue)) {
38423850
$wrappedItem = end($storeValue);
38433851
$storeValue = end($wrappedItem);
38443852
}
3845-
if (isset($storeValue) && ($storeValue
3846-
|| ($storeValue === 'Pruned branch'))
3853+
if (isset($storeValue)
3854+
&& (
3855+
$storeValueAsBool
3856+
|| Functions::isError($storeValue)
3857+
|| ($storeValue === 'Pruned branch'))
38473858
) {
38483859
// If branching value is true, we don't need to compute
38493860
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)