From ea34eaaf89daabcd6768bb18263029d1376dd3eb Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 8 Apr 2026 16:31:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=E9=9D=99?= =?UTF-8?q?=E6=80=81=E5=8F=98=E9=87=8F=E6=B7=BB=E5=8A=A0=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81=E9=87=8D=E5=A4=8D=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=92=8C=E8=BF=94=E5=9B=9E=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 addStaticVar 方法参数,接受 Variable 对象并返回全局变量名 - 在 addStaticVar 中添加重复变量检查和错误报告功能 - 将静态变量处理逻辑集中到 addStaticVar 方法中 - 简化静态变量列表处理代码,直接调用重构后的方法 - 移除重复的变量检查和全局变量添加代码 --- src/Php/CompilerBase.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index bd63c898..cd16c9fc 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1626,9 +1626,16 @@ class CompilerBase extends \PhpAot\Core\Translator return $var; } - protected function addStaticVar(string $name, string $type): void + protected function addStaticVar(Variable $var, string $name, string $type): string { + if ($this->hasVar($name)) { + $this->fatalError($var, 'Duplicate variable `$' . $var->name . '`'); + } $this->context->staticVars[$name] = $type; + // 静态变量实际上是一个全局变量的引用 + $globalVar = $this->escapeStaticVar($name); + $this->addGlobalVar($globalVar, $type); + return $globalVar; } protected function hasArgument(string $name): bool @@ -3600,15 +3607,8 @@ class CompilerBase extends \PhpAot\Core\Translator $list = []; foreach ($v->vars as $var) { $varName = $this->escapeVarName($var->var->name); - if ($this->hasVar($varName)) { - $this->error('Duplicate variable `$' . $var->var->name . '`'); - } - $type = $var->default ? $this->detectExprType($var->default) : self::TYPE_VAR; - $this->addStaticVar($varName, $type); - // 静态变量实际上是一个全局变量的引用 - $globalVar = $this->escapeStaticVar($varName); - $this->addGlobalVar($globalVar, $type); + $globalVar = $this->addStaticVar($var->var, $varName, $type); $list[] = self::TYPE_VAR . ' &' . $varName . ' = ' . $this->escapeGlobalVar($globalVar) . ';'; if ($var->default) {