Skip to content

Commit 6720b53

Browse files
authored
Merge pull request #5055 from HawkForge/fix-branch-pruning-nested-if-condition
Fix branch pruning for IF nested in the condition of a pruned IF
2 parents 753d715 + 4ecc20f commit 6720b53

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Some earlier branches remain supported and security fixes are applied to them; i
6060
- `removeColumn()` no longer throws "Invalid column index 0" when it removes column A and every column up to the highest data column, e.g. `removeColumn('A')` on an empty sheet. [Issue #5049](https://github.com/PHPOffice/PhpSpreadsheet/issues/5049) [PR #5050](https://github.com/PHPOffice/PhpSpreadsheet/pull/5050)
6161
- `removeColumn()` and `removeRow()` take the removed columns/rows out of data validation ranges: a validation that lies only on them is deleted, and a range that ends on them shrinks, instead of covering the column/row that slides into their place. [Issue #5047](https://github.com/PHPOffice/PhpSpreadsheet/issues/5047) [PR #5048](https://github.com/PHPOffice/PhpSpreadsheet/pull/5048)
6262
- Minor cleanup in Ods Reader to fix test which was not working in Windows. [PR #5052](https://github.com/PHPOffice/PhpSpreadsheet/pull/5052)
63+
- Branch pruning no longer throws "internal error" for an IF nested in the condition of another IF inside a pruned branch. [Issue #3755](https://github.com/PHPOffice/PhpSpreadsheet/issues/3755) [Issue #3790](https://github.com/PHPOffice/PhpSpreadsheet/issues/3790) [PR #5055](https://github.com/PHPOffice/PhpSpreadsheet/pull/5055)
6364
- Xls Writer no longer loses EMF and WMF images (they are written as PNG); the resulting file could not be read back (`Call to a member function getBlipType() on null`). [Issue #274](https://github.com/PHPOffice/PhpSpreadsheet/issues/274)
6465
- Xls Reader reads EMF and WMF pictures (as PNG `MemoryDrawing`); they were ignored. [Issue #274](https://github.com/PHPOffice/PhpSpreadsheet/issues/274)
6566
- Xlsx Writer declares WMF and EMF images as `image/x-wmf` and `image/x-emf` instead of `application/octet-stream`, and Ods Writer does the same in its manifest when it stores them as is.

‎src/PhpSpreadsheet/Calculation/Engine/BranchPruner.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,15 @@ private function initialiseCondition(): void
8080
{
8181
if (isset($this->pendingStoreKey, $this->conditionMap[$this->pendingStoreKey]) && $this->conditionMap[$this->pendingStoreKey]) {
8282
$this->currentCondition = $this->pendingStoreKey;
83-
$stackDepth = count($this->storeKeysStack);
84-
if ($stackDepth > 1) {
85-
// nested if
86-
$this->previousStoreKey = $this->storeKeysStack[$stackDepth - 2];
83+
// nested if: find the closest enclosing IF whose then/else branch we are in,
84+
// skipping any enclosing IF where we are only part of its condition
85+
for ($index = count($this->storeKeysStack) - 2; $index >= 0; --$index) {
86+
$storeKey = $this->storeKeysStack[$index];
87+
if (!empty($this->thenMap[$storeKey]) || !empty($this->elseMap[$storeKey])) {
88+
$this->previousStoreKey = $storeKey;
89+
90+
break;
91+
}
8792
}
8893
}
8994
}

‎tests/data/Calculation/Calculation.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,19 @@ function calculationTestDataGenerator(): array
6363
];
6464
$set9 = ['a', $dataArray6, '=A1', 'A2'];
6565

66+
// Issues 3755 and 3790 - IF nested in the condition of an IF within a pruned branch
67+
$set10 = [3, [], '=IF(FALSE, IF(IF(TRUE,6,2),1,2), 3)', 'E5'];
68+
$formula4 = '=IF(FALSE, IF( LEFT( "0", 3 ) = "16-", 1, IF( OR( 1 = IF( "Le L" = "Le L", 6.89, 2 ), AND( 0.735 = 1, "0" <> "" ) ), "", 1 ) ), 3)';
69+
$set11 = [3, [], $formula4, 'E5'];
70+
$dataArray7 = [
71+
[2],
72+
];
73+
$formula5 = '=IF(A1=1,IF(AND(IF(1>0,1,IF(0=0,(0*0),0))>0,IF(0>0,0,IF(0=0,(0*0),0))<1),1,ROUND(IF(0>0,0,IF(1=0,(1*1),1)),0)),0)';
74+
$set12 = [0, $dataArray7, $formula5, 'E5'];
75+
6676
return [
6777
$set0, $set1, $set2, $set3, $set4, $set5, $set6, $set7, $set8, $set9,
78+
$set10, $set11, $set12,
6879
];
6980
}
7081

0 commit comments

Comments
 (0)