ConstExprEvaluator.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace PhpParser;
  3. use function array_merge;
  4. use PhpParser\Node\Expr;
  5. use PhpParser\Node\Scalar;
  6. /**
  7. * Evaluates constant expressions.
  8. *
  9. * This evaluator is able to evaluate all constant expressions (as defined by PHP), which can be
  10. * evaluated without further context. If a subexpression is not of this type, a user-provided
  11. * fallback evaluator is invoked. To support all constant expressions that are also supported by
  12. * PHP (and not already handled by this class), the fallback evaluator must be able to handle the
  13. * following node types:
  14. *
  15. * * All Scalar\MagicConst\* nodes.
  16. * * Expr\ConstFetch nodes. Only null/false/true are already handled by this class.
  17. * * Expr\ClassConstFetch nodes.
  18. *
  19. * The fallback evaluator should throw ConstExprEvaluationException for nodes it cannot evaluate.
  20. *
  21. * The evaluation is dependent on runtime configuration in two respects: Firstly, floating
  22. * point to string conversions are affected by the precision ini setting. Secondly, they are also
  23. * affected by the LC_NUMERIC locale.
  24. */
  25. class ConstExprEvaluator
  26. {
  27. private $fallbackEvaluator;
  28. /**
  29. * Create a constant expression evaluator.
  30. *
  31. * The provided fallback evaluator is invoked whenever a subexpression cannot be evaluated. See
  32. * class doc comment for more information.
  33. *
  34. * @param callable|null $fallbackEvaluator To call if subexpression cannot be evaluated
  35. */
  36. public function __construct(callable $fallbackEvaluator = null) {
  37. $this->fallbackEvaluator = $fallbackEvaluator ?? function(Expr $expr) {
  38. throw new ConstExprEvaluationException(
  39. "Expression of type {$expr->getType()} cannot be evaluated"
  40. );
  41. };
  42. }
  43. /**
  44. * Silently evaluates a constant expression into a PHP value.
  45. *
  46. * Thrown Errors, warnings or notices will be converted into a ConstExprEvaluationException.
  47. * The original source of the exception is available through getPrevious().
  48. *
  49. * If some part of the expression cannot be evaluated, the fallback evaluator passed to the
  50. * constructor will be invoked. By default, if no fallback is provided, an exception of type
  51. * ConstExprEvaluationException is thrown.
  52. *
  53. * See class doc comment for caveats and limitations.
  54. *
  55. * @param Expr $expr Constant expression to evaluate
  56. * @return mixed Result of evaluation
  57. *
  58. * @throws ConstExprEvaluationException if the expression cannot be evaluated or an error occurred
  59. */
  60. public function evaluateSilently(Expr $expr) {
  61. set_error_handler(function($num, $str, $file, $line) {
  62. throw new \ErrorException($str, 0, $num, $file, $line);
  63. });
  64. try {
  65. return $this->evaluate($expr);
  66. } catch (\Throwable $e) {
  67. if (!$e instanceof ConstExprEvaluationException) {
  68. $e = new ConstExprEvaluationException(
  69. "An error occurred during constant expression evaluation", 0, $e);
  70. }
  71. throw $e;
  72. } finally {
  73. restore_error_handler();
  74. }
  75. }
  76. /**
  77. * Directly evaluates a constant expression into a PHP value.
  78. *
  79. * May generate Error exceptions, warnings or notices. Use evaluateSilently() to convert these
  80. * into a ConstExprEvaluationException.
  81. *
  82. * If some part of the expression cannot be evaluated, the fallback evaluator passed to the
  83. * constructor will be invoked. By default, if no fallback is provided, an exception of type
  84. * ConstExprEvaluationException is thrown.
  85. *
  86. * See class doc comment for caveats and limitations.
  87. *
  88. * @param Expr $expr Constant expression to evaluate
  89. * @return mixed Result of evaluation
  90. *
  91. * @throws ConstExprEvaluationException if the expression cannot be evaluated
  92. */
  93. public function evaluateDirectly(Expr $expr) {
  94. return $this->evaluate($expr);
  95. }
  96. private function evaluate(Expr $expr) {
  97. if ($expr instanceof Scalar\LNumber
  98. || $expr instanceof Scalar\DNumber
  99. || $expr instanceof Scalar\String_
  100. ) {
  101. return $expr->value;
  102. }
  103. if ($expr instanceof Expr\Array_) {
  104. return $this->evaluateArray($expr);
  105. }
  106. // Unary operators
  107. if ($expr instanceof Expr\UnaryPlus) {
  108. return +$this->evaluate($expr->expr);
  109. }
  110. if ($expr instanceof Expr\UnaryMinus) {
  111. return -$this->evaluate($expr->expr);
  112. }
  113. if ($expr instanceof Expr\BooleanNot) {
  114. return !$this->evaluate($expr->expr);
  115. }
  116. if ($expr instanceof Expr\BitwiseNot) {
  117. return ~$this->evaluate($expr->expr);
  118. }
  119. if ($expr instanceof Expr\BinaryOp) {
  120. return $this->evaluateBinaryOp($expr);
  121. }
  122. if ($expr instanceof Expr\Ternary) {
  123. return $this->evaluateTernary($expr);
  124. }
  125. if ($expr instanceof Expr\ArrayDimFetch && null !== $expr->dim) {
  126. return $this->evaluate($expr->var)[$this->evaluate($expr->dim)];
  127. }
  128. if ($expr instanceof Expr\ConstFetch) {
  129. return $this->evaluateConstFetch($expr);
  130. }
  131. return ($this->fallbackEvaluator)($expr);
  132. }
  133. private function evaluateArray(Expr\Array_ $expr) {
  134. $array = [];
  135. foreach ($expr->items as $item) {
  136. if (null !== $item->key) {
  137. $array[$this->evaluate($item->key)] = $this->evaluate($item->value);
  138. } elseif ($item->unpack) {
  139. $array = array_merge($array, $this->evaluate($item->value));
  140. } else {
  141. $array[] = $this->evaluate($item->value);
  142. }
  143. }
  144. return $array;
  145. }
  146. private function evaluateTernary(Expr\Ternary $expr) {
  147. if (null === $expr->if) {
  148. return $this->evaluate($expr->cond) ?: $this->evaluate($expr->else);
  149. }
  150. return $this->evaluate($expr->cond)
  151. ? $this->evaluate($expr->if)
  152. : $this->evaluate($expr->else);
  153. }
  154. private function evaluateBinaryOp(Expr\BinaryOp $expr) {
  155. if ($expr instanceof Expr\BinaryOp\Coalesce
  156. && $expr->left instanceof Expr\ArrayDimFetch
  157. ) {
  158. // This needs to be special cased to respect BP_VAR_IS fetch semantics
  159. return $this->evaluate($expr->left->var)[$this->evaluate($expr->left->dim)]
  160. ?? $this->evaluate($expr->right);
  161. }
  162. // The evaluate() calls are repeated in each branch, because some of the operators are
  163. // short-circuiting and evaluating the RHS in advance may be illegal in that case
  164. $l = $expr->left;
  165. $r = $expr->right;
  166. switch ($expr->getOperatorSigil()) {
  167. case '&': return $this->evaluate($l) & $this->evaluate($r);
  168. case '|': return $this->evaluate($l) | $this->evaluate($r);
  169. case '^': return $this->evaluate($l) ^ $this->evaluate($r);
  170. case '&&': return $this->evaluate($l) && $this->evaluate($r);
  171. case '||': return $this->evaluate($l) || $this->evaluate($r);
  172. case '??': return $this->evaluate($l) ?? $this->evaluate($r);
  173. case '.': return $this->evaluate($l) . $this->evaluate($r);
  174. case '/': return $this->evaluate($l) / $this->evaluate($r);
  175. case '==': return $this->evaluate($l) == $this->evaluate($r);
  176. case '>': return $this->evaluate($l) > $this->evaluate($r);
  177. case '>=': return $this->evaluate($l) >= $this->evaluate($r);
  178. case '===': return $this->evaluate($l) === $this->evaluate($r);
  179. case 'and': return $this->evaluate($l) and $this->evaluate($r);
  180. case 'or': return $this->evaluate($l) or $this->evaluate($r);
  181. case 'xor': return $this->evaluate($l) xor $this->evaluate($r);
  182. case '-': return $this->evaluate($l) - $this->evaluate($r);
  183. case '%': return $this->evaluate($l) % $this->evaluate($r);
  184. case '*': return $this->evaluate($l) * $this->evaluate($r);
  185. case '!=': return $this->evaluate($l) != $this->evaluate($r);
  186. case '!==': return $this->evaluate($l) !== $this->evaluate($r);
  187. case '+': return $this->evaluate($l) + $this->evaluate($r);
  188. case '**': return $this->evaluate($l) ** $this->evaluate($r);
  189. case '<<': return $this->evaluate($l) << $this->evaluate($r);
  190. case '>>': return $this->evaluate($l) >> $this->evaluate($r);
  191. case '<': return $this->evaluate($l) < $this->evaluate($r);
  192. case '<=': return $this->evaluate($l) <= $this->evaluate($r);
  193. case '<=>': return $this->evaluate($l) <=> $this->evaluate($r);
  194. }
  195. throw new \Exception('Should not happen');
  196. }
  197. private function evaluateConstFetch(Expr\ConstFetch $expr) {
  198. $name = $expr->name->toLowerString();
  199. switch ($name) {
  200. case 'null': return null;
  201. case 'false': return false;
  202. case 'true': return true;
  203. }
  204. return ($this->fallbackEvaluator)($expr);
  205. }
  206. }