chengkun
2025-09-15 3c9050e82e582414dc7b208c8283fe47be37eeba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
 
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine;
 
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
 
class BranchPruner
{
    protected bool $branchPruningEnabled;
 
    /**
     * Used to generate unique store keys.
     */
    private int $branchStoreKeyCounter = 0;
 
    /**
     * currently pending storeKey (last item of the storeKeysStack.
     */
    protected ?string $pendingStoreKey = null;
 
    /**
     * @var string[]
     */
    protected array $storeKeysStack = [];
 
    /**
     * @var bool[]
     */
    protected array $conditionMap = [];
 
    /**
     * @var bool[]
     */
    protected array $thenMap = [];
 
    /**
     * @var bool[]
     */
    protected array $elseMap = [];
 
    /**
     * @var int[]
     */
    protected array $braceDepthMap = [];
 
    protected ?string $currentCondition = null;
 
    protected ?string $currentOnlyIf = null;
 
    protected ?string $currentOnlyIfNot = null;
 
    protected ?string $previousStoreKey = null;
 
    public function __construct(bool $branchPruningEnabled)
    {
        $this->branchPruningEnabled = $branchPruningEnabled;
    }
 
    public function clearBranchStore(): void
    {
        $this->branchStoreKeyCounter = 0;
    }
 
    public function initialiseForLoop(): void
    {
        $this->currentCondition = null;
        $this->currentOnlyIf = null;
        $this->currentOnlyIfNot = null;
        $this->previousStoreKey = null;
        $this->pendingStoreKey = empty($this->storeKeysStack) ? null : end($this->storeKeysStack);
 
        if ($this->branchPruningEnabled) {
            $this->initialiseCondition();
            $this->initialiseThen();
            $this->initialiseElse();
        }
    }
 
    private function initialiseCondition(): void
    {
        if (isset($this->conditionMap[$this->pendingStoreKey]) && $this->conditionMap[$this->pendingStoreKey]) {
            $this->currentCondition = $this->pendingStoreKey;
            $stackDepth = count($this->storeKeysStack);
            if ($stackDepth > 1) {
                // nested if
                $this->previousStoreKey = $this->storeKeysStack[$stackDepth - 2];
            }
        }
    }
 
    private function initialiseThen(): void
    {
        if (isset($this->thenMap[$this->pendingStoreKey]) && $this->thenMap[$this->pendingStoreKey]) {
            $this->currentOnlyIf = $this->pendingStoreKey;
        } elseif (
            isset($this->previousStoreKey, $this->thenMap[$this->previousStoreKey])
            && $this->thenMap[$this->previousStoreKey]
        ) {
            $this->currentOnlyIf = $this->previousStoreKey;
        }
    }
 
    private function initialiseElse(): void
    {
        if (isset($this->elseMap[$this->pendingStoreKey]) && $this->elseMap[$this->pendingStoreKey]) {
            $this->currentOnlyIfNot = $this->pendingStoreKey;
        } elseif (
            isset($this->previousStoreKey, $this->elseMap[$this->previousStoreKey])
            && $this->elseMap[$this->previousStoreKey]
        ) {
            $this->currentOnlyIfNot = $this->previousStoreKey;
        }
    }
 
    public function decrementDepth(): void
    {
        if (!empty($this->pendingStoreKey)) {
            --$this->braceDepthMap[$this->pendingStoreKey];
        }
    }
 
    public function incrementDepth(): void
    {
        if (!empty($this->pendingStoreKey)) {
            ++$this->braceDepthMap[$this->pendingStoreKey];
        }
    }
 
    public function functionCall(string $functionName): void
    {
        if ($this->branchPruningEnabled && ($functionName === 'IF(')) {
            // we handle a new if
            $this->pendingStoreKey = $this->getUnusedBranchStoreKey();
            $this->storeKeysStack[] = $this->pendingStoreKey;
            $this->conditionMap[$this->pendingStoreKey] = true;
            $this->braceDepthMap[$this->pendingStoreKey] = 0;
        } elseif (!empty($this->pendingStoreKey) && array_key_exists($this->pendingStoreKey, $this->braceDepthMap)) {
            // this is not an if but we go deeper
            ++$this->braceDepthMap[$this->pendingStoreKey];
        }
    }
 
    public function argumentSeparator(): void
    {
        if (!empty($this->pendingStoreKey) && $this->braceDepthMap[$this->pendingStoreKey] === 0) {
            // We must go to the IF next argument
            if ($this->conditionMap[$this->pendingStoreKey]) {
                $this->conditionMap[$this->pendingStoreKey] = false;
                $this->thenMap[$this->pendingStoreKey] = true;
            } elseif ($this->thenMap[$this->pendingStoreKey]) {
                $this->thenMap[$this->pendingStoreKey] = false;
                $this->elseMap[$this->pendingStoreKey] = true;
            } elseif ($this->elseMap[$this->pendingStoreKey]) {
                throw new Exception('Reaching fourth argument of an IF');
            }
        }
    }
 
    public function closingBrace(mixed $value): void
    {
        if (!empty($this->pendingStoreKey) && $this->braceDepthMap[$this->pendingStoreKey] === -1) {
            // we are closing an IF(
            if ($value !== 'IF(') {
                throw new Exception('Parser bug we should be in an "IF("');
            }
 
            if ($this->conditionMap[$this->pendingStoreKey]) {
                throw new Exception('We should not be expecting a condition');
            }
 
            $this->thenMap[$this->pendingStoreKey] = false;
            $this->elseMap[$this->pendingStoreKey] = false;
            --$this->braceDepthMap[$this->pendingStoreKey];
            array_pop($this->storeKeysStack);
            $this->pendingStoreKey = null;
        }
    }
 
    public function currentCondition(): ?string
    {
        return $this->currentCondition;
    }
 
    public function currentOnlyIf(): ?string
    {
        return $this->currentOnlyIf;
    }
 
    public function currentOnlyIfNot(): ?string
    {
        return $this->currentOnlyIfNot;
    }
 
    private function getUnusedBranchStoreKey(): string
    {
        $storeKeyValue = 'storeKey-' . $this->branchStoreKeyCounter;
        ++$this->branchStoreKeyCounter;
 
        return $storeKeyValue;
    }
}