From b57ff37b1898d4a0e876a892c1645533a61890b3 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 31 Jan 2026 17:54:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84PHP=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E4=B8=AD=E7=9A=84=E9=9D=99=E6=80=81=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E8=B5=8B=E5=80=BC=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将静态属性赋值解析提取为独立的parseAssignStaticProperty方法 - 修改parseAssign方法以使用新的辅助方法进行静态属性检查 - 添加函数定义存在性验证以避免未定义函数的致命错误 - 使用独立的函数名存在性检查替代直接数组访问 - 改进了代码结构和可读性 --- src/Php/CompilerBase.php | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index ec823850..ae5820d5 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -992,6 +992,18 @@ class CompilerBase extends \PhpAot\Core\Translator return implode(";\n" . $this->getIndent(), $list); } + protected function parseAssignStaticProperty($left, $right) + { + $value = $this->trimBrackets($this->parseExpr($right)); + $native = $this->parseNativeStaticPropertyFetch($left); + if ($native) { + return $native . ' = ' . $value; + } + $class = $this->identifierToStr($left->class); + $propName = $this->identifierToStr($left->name); + return "php::setStaticProperty({$class}, {$propName}, {$value})"; + } + protected function parseAssign(Node\Expr\Assign $v): string { $left = $v->var; @@ -1000,20 +1012,12 @@ class CompilerBase extends \PhpAot\Core\Translator if ($right->getType() === 'Expr_Assign') { return $this->parseRightAssociativeAssign($left, $right); } - if ($left->getType() === self::EXPR_ARRAY_DIM_FETCH) { + if ($this->isArrayDimFetch($left)) { return $this->parseAssignArrayDim($left, $right); } - if ($left->getType() === 'Expr_StaticPropertyFetch') { - $value = $this->trimBrackets($this->parseExpr($right)); - $native = $this->parseNativeStaticPropertyFetch($left); - if ($native) { - return $native . ' = ' . $value; - } - $class = $this->identifierToStr($left->class); - $propName = $this->identifierToStr($left->name); - return "php::setStaticProperty({$class}, {$propName}, {$value})"; + if ($this->isStaticPropertyFetch($left)) { + return $this->parseAssignStaticProperty($left, $right); } - return $this->parseFinallyAssign($left, $right); } @@ -2315,6 +2319,9 @@ class CompilerBase extends \PhpAot\Core\Translator protected function getArgInfo(Node $arg, string $funcName, int $index): ArgInfo { + if (!isset($this->nativeFunctions[$funcName])) { + $this->fatalError($arg, "Function `{$funcName}` is undefined, you must adjust the order of function definition"); + } $funcDef = $this->nativeFunctions[$funcName]; if (!array_key_exists($index, $funcDef->argInfoList)) { $this->fatalError($arg, "Argument `{$index}` of function `{$funcName}` not found");