From 4ce8c247654411445b261b3fd65501060debeaa3 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 2 Feb 2026 16:10:15 +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=E4=B8=AD=E7=9A=84=E8=B5=8B=E5=80=BC=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除重复的变量检查函数,简化右关联赋值解析 - 重构赋值表达式中的临时变量生成逻辑 - 改进数组维度获取的变量验证流程 - 修复变量作用域管理中的状态恢复问题 - 优化未定义变量错误处理与本地变量添加逻辑 --- src/Php/CompilerBase.php | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 8f300203..00c5c8a9 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -953,27 +953,13 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseRightAssociativeAssign(NodeAbstract $left, Node\Expr\Assign $right): string { - $checkVarFn = function ($var) { - if ($this->isArrayDimFetch($var)) { - $array = $this->parseIdentifier($var->var); - if ($this->isVarExpr($var->var)) { - if (!$this->hasVar($array)) { - $this->addLocalVar($array, self::TYPE_ARRAY); - } - } - } - }; - - $checkVarFn($left); $chain[] = $left; $next = $right; while ($next->getType() === 'Expr_Assign') { $var = $next->var; - $checkVarFn($var); $chain[] = $var; $next = $next->expr; } - $tmpVar = $this->genTmpVarName(); $this->addLocalVar($tmpVar, self::TYPE_VAR); @@ -1041,9 +1027,10 @@ class CompilerBase extends \PhpAot\Core\Translator return $code . '}'; } + $oriInAssignExpr = $this->inAssignExpr; $this->inAssignExpr = true; $var = $this->parseIdentifier($left); - $this->inAssignExpr = false; + $this->inAssignExpr = $oriInAssignExpr; if ($var === 'this_') { $this->fatalError($left, 'Cannot re-assign $this'); } @@ -1677,7 +1664,11 @@ class CompilerBase extends \PhpAot\Core\Translator $var = $this->parseIdentifier($node->var); if ($this->isVarExpr($node->var)) { if (!$this->hasVar($var)) { - $this->fatalError($node->var, "The variable `{$node->var->name}` is undefined"); + if ($write) { + $this->addLocalVar($var, self::TYPE_ARRAY); + } else { + $this->fatalError($node->var, "The variable `{$node->var->name}` is undefined"); + } } if ($var === 'GLOBALS') { if ($node->dim === null) {