refactor(php): 优化零值字面量判断逻辑

- 移除多余的变量赋值,直接返回表达式结果
- 统一数值比较操作符的一致性
- 为字符串数值转换添加括号以提高可读性
- 新增对常量和类常量表达式的零值检测支持
- 简化函数中的重复返回语句结构
pull/3/head
韩天峰 2 months ago
parent 215f5cdcac
commit 72cf8e7166
  1. 15
      src/Php/CompilerBase.php

@ -1232,21 +1232,20 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function isZeroLiteral(NodeAbstract $expr): bool
{
if ($expr instanceof Node\Scalar\Int_) {
$result = $expr->value === 0;
return $result;
return $expr->value === 0;
}
if ($expr instanceof Node\Scalar\Float_) {
$result = $expr->value == 0.0;
return $result;
return $expr->value == 0.0;
}
if ($expr instanceof Expr\UnaryMinus || $expr instanceof Expr\UnaryPlus) {
$result = $this->isZeroLiteral($expr->expr);
return $result;
return $this->isZeroLiteral($expr->expr);
}
if ($expr instanceof Node\Scalar\String_) {
$value = trim($expr->value);
$result = $value !== '' && is_numeric($value) && (float) $value == 0.0;
return $result;
return $value !== '' && is_numeric($value) && (float)$value == 0.0;
}
if ($expr instanceof Node\Expr\ConstFetch or $expr instanceof Node\Expr\ClassConstFetch) {
return in_array($this->parseExpr($expr), ['0L', '0LL']);
}
return false;
}

Loading…
Cancel
Save