fix(php): 解决除零运算错误处理问题

- 添加了对除法和取模运算中零值检测的统一处理
- 当遇到除零或模零运算时抛出致命错误而不是返回NaN
- 移除了旧的零除法返回NaN的逻辑
- 统一了除法和取模运算的零值检查条件
pull/2/head
韩天峰 2 months ago
parent 8e5b9e8076
commit 7e52d51b5b
  1. 8
      src/Php/Parser/BinaryOpTrait.php

@ -121,12 +121,12 @@ trait BinaryOpTrait
$leftExpr = $this->convertExprType($leftExpr, $leftType, self::TYPE_FLOAT);
}
if ($op === '%' and !($leftType === self::TYPE_INT and $rightType === self::TYPE_INT)) {
return 'php::fn::mod(' . $leftExpr . ', ' . $rightExpr . ')';
if (($op === '/' or $op === '%') and $this->isZeroLiteral($right)) {
$this->fatalError($right, 'Cannot divide or modulo by zero');
}
if ($op === '/' and $this->isZeroLiteral($right)) {
return self::VALUE_NAN;
if ($op === '%' and !($leftType === self::TYPE_INT and $rightType === self::TYPE_INT)) {
return 'php::fn::mod(' . $leftExpr . ', ' . $rightExpr . ')';
}
return '((' . $leftExpr . ') ' . $op . ' (' . $rightExpr . '))';

Loading…
Cancel
Save