Fix branch pruning resolution of non boolean conditions

Closes #1167
This commit is contained in:
Fräntz Miccoli 2019-09-23 00:05:04 +02:00 committed by Adrien Crivelli
parent 5441b2fa73
commit 75dfcb5a36
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
3 changed files with 24 additions and 5 deletions

View File

@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Call garbage collector after removing a column to prevent stale cached values - Call garbage collector after removing a column to prevent stale cached values
- Trying to remove a column that doesn't exist deletes the latest column - Trying to remove a column that doesn't exist deletes the latest column
- Keep big integer as integer instead of lossely casting to float [#874](https://github.com/PHPOffice/PhpSpreadsheet/pull/874) - Keep big integer as integer instead of lossely casting to float [#874](https://github.com/PHPOffice/PhpSpreadsheet/pull/874)
- Fix branch pruning handling of non boolean conditions [#1167](https://github.com/PHPOffice/PhpSpreadsheet/pull/1167)
## [1.9.0] - 2019-08-17 ## [1.9.0] - 2019-08-17

View File

@ -3809,13 +3809,19 @@ class Calculation
if ($this->branchPruningEnabled && isset($tokenData['onlyIf'])) { if ($this->branchPruningEnabled && isset($tokenData['onlyIf'])) {
$onlyIfStoreKey = $tokenData['onlyIf']; $onlyIfStoreKey = $tokenData['onlyIf'];
$storeValue = $branchStore[$onlyIfStoreKey] ?? null; $storeValue = $branchStore[$onlyIfStoreKey] ?? null;
$storeValueAsBool = ($storeValue === null) ?
true : (bool) Functions::flattenSingleValue($storeValue);
if (is_array($storeValue)) { if (is_array($storeValue)) {
$wrappedItem = end($storeValue); $wrappedItem = end($storeValue);
$storeValue = end($wrappedItem); $storeValue = end($wrappedItem);
} }
if (isset($storeValue) && (($storeValue !== true) if (isset($storeValue)
|| ($storeValue === 'Pruned branch')) && (
!$storeValueAsBool
|| Functions::isError($storeValue)
|| ($storeValue === 'Pruned branch')
)
) { ) {
// If branching value is not true, we don't need to compute // If branching value is not true, we don't need to compute
if (!isset($fakedForBranchPruning['onlyIf-' . $onlyIfStoreKey])) { if (!isset($fakedForBranchPruning['onlyIf-' . $onlyIfStoreKey])) {
@ -3838,11 +3844,16 @@ class Calculation
if ($this->branchPruningEnabled && isset($tokenData['onlyIfNot'])) { if ($this->branchPruningEnabled && isset($tokenData['onlyIfNot'])) {
$onlyIfNotStoreKey = $tokenData['onlyIfNot']; $onlyIfNotStoreKey = $tokenData['onlyIfNot'];
$storeValue = $branchStore[$onlyIfNotStoreKey] ?? null; $storeValue = $branchStore[$onlyIfNotStoreKey] ?? null;
$storeValueAsBool = ($storeValue === null) ?
true : (bool) Functions::flattenSingleValue($storeValue);
if (is_array($storeValue)) { if (is_array($storeValue)) {
$wrappedItem = end($storeValue); $wrappedItem = end($storeValue);
$storeValue = end($wrappedItem); $storeValue = end($wrappedItem);
} }
if (isset($storeValue) && ($storeValue if (isset($storeValue)
&& (
$storeValueAsBool
|| Functions::isError($storeValue)
|| ($storeValue === 'Pruned branch')) || ($storeValue === 'Pruned branch'))
) { ) {
// If branching value is true, we don't need to compute // If branching value is true, we don't need to compute

View File

@ -55,7 +55,14 @@ function calculationTestDataGenerator()
$formula3 = '=IF(A4="take A", A3, B3)'; $formula3 = '=IF(A4="take A", A3, B3)';
$set8 = [4, $dataArray5, $formula3, 'E5', ['A3'], ['B3']]; $set8 = [4, $dataArray5, $formula3, 'E5', ['A3'], ['B3']];
return [$set0, $set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8]; $dataArray6 = [
['=IF(22,"a","b")']
];
$set9 = ['a', $dataArray6, '=A1', 'A2'];
return [
$set0, $set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8, $set9
];
} }
return calculationTestDataGenerator(); return calculationTestDataGenerator();