BranchPruner.php 6.2 KB

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