From 625604d375774ced8e7bf07b4bdb0c6f6081dbbd Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 31 Jan 2026 18:25:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E5=9F=BA=E7=A1=80=E7=B1=BB=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E8=B5=8B=E5=80=BC=E5=92=8C=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E7=BB=B4=E5=BA=A6=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 简化 Expr_Assign 和 Expr_AssignRef 的返回逻辑 - 在 checkVarFn 回调函数中添加对 isVarExpr 的检查以确保变量存在 - 移除 parseAssign 函数中关于数组维度获取和静态属性获取的特殊处理逻辑 - 在 parseArrayDimFetch 方法中添加对未定义变量的错误检查 - 添加对全局常量的处理支持 - 更新主函数中的全局变量声明以包含 definedConstants - 在启动时初始化 definedConstants 变量 --- bin/gen_stub.php | 16 +++++++++++++++- src/Php/CompilerBase.php | 34 ++++++++++++++++------------------ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/bin/gen_stub.php b/bin/gen_stub.php index 7ddaeae7..dd7d7a08 100755 --- a/bin/gen_stub.php +++ b/bin/gen_stub.php @@ -2329,6 +2329,18 @@ class EvaluatedValue return null; } + global $definedConstants; + if (isset($definedConstants[$constName])) { + $constValue = $definedConstants[$constName]; + if (is_scalar($constValue)) { + if (is_string($constValue)) { + return '"' . $constValue . '"'; + } else { + return $constValue; + } + } + } + throw new Exception("Constant " . $constName . " cannot be found"); } ); @@ -6098,7 +6110,7 @@ function initPhpParser() { function main() { - global $argv, $argc, $translator; + global $argv, $argc, $translator, $definedConstants; error_reporting(E_ALL & ~E_DEPRECATED); ini_set("precision", "-1"); @@ -6108,6 +6120,8 @@ function main() $translator->setIndent("\t"); $translator->setIndentLevel(1); + $definedConstants = get_defined_constants(); + $opt_index = 0; $options = getopt( "fh", diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index ae5820d5..8f300203 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -249,13 +249,9 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Expr_Empty': return $this->parseEmpty($expr); case 'Expr_Assign': - $result = $this->parseAssign($expr); - - return $result; + return $this->parseAssign($expr); case 'Expr_AssignRef': - $result = $this->parseAssignRef($expr); - - return $result; + return $this->parseAssignRef($expr); case 'Expr_Print': return $this->parsePrint($expr); case 'Expr_BinaryOp_Equal': @@ -960,8 +956,10 @@ class CompilerBase extends \PhpAot\Core\Translator $checkVarFn = function ($var) { if ($this->isArrayDimFetch($var)) { $array = $this->parseIdentifier($var->var); - if (!$this->hasVar($array)) { - $this->addLocalVar($array, self::TYPE_ARRAY); + if ($this->isVarExpr($var->var)) { + if (!$this->hasVar($array)) { + $this->addLocalVar($array, self::TYPE_ARRAY); + } } } }; @@ -1012,12 +1010,6 @@ class CompilerBase extends \PhpAot\Core\Translator if ($right->getType() === 'Expr_Assign') { return $this->parseRightAssociativeAssign($left, $right); } - if ($this->isArrayDimFetch($left)) { - return $this->parseAssignArrayDim($left, $right); - } - if ($this->isStaticPropertyFetch($left)) { - return $this->parseAssignStaticProperty($left, $right); - } return $this->parseFinallyAssign($left, $right); } @@ -1683,12 +1675,18 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseArrayDimFetch(Node\Expr\ArrayDimFetch $node, bool $write): string { $var = $this->parseIdentifier($node->var); - if ($this->isVarExpr($node->var) and $var === 'GLOBALS') { - if ($node->dim === null) { - $this->fatalError($node, 'Cannot use [] for GLOBALS'); + if ($this->isVarExpr($node->var)) { + if (!$this->hasVar($var)) { + $this->fatalError($node->var, "The variable `{$node->var->name}` is undefined"); + } + if ($var === 'GLOBALS') { + if ($node->dim === null) { + $this->fatalError($node, 'Cannot use [] for GLOBALS'); + } + return 'php::global(' . $this->parseIdentifier($node->dim) . ')'; } - return 'php::global(' . $this->parseIdentifier($node->dim) . ')'; } + if ($node->dim === null) { if (!$write) { $this->fatalError($node, 'Cannot use [] for reading');