From 215c0772919bca68b2c4fb281a4ac46030d714bc Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 2 Jun 2026 17:17:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96=E7=B1=BB?= =?UTF-8?q?=E5=90=8D=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0objval=E5=87=BD=E6=95=B0=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提取类名参数解析逻辑到独立的resolveClassNameArg方法 - 简化toObject方法中的类名解析实现 - 添加对objval函数调用的类名解析支持 - 在polyfills中定义objval函数以支持新的解析逻辑 --- src/Php/CompilerBase.php | 29 ++++++++++++++++++++--------- src/polyfills.php | 5 +++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 8822bb52..ee07e228 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -2073,18 +2073,16 @@ class CompilerBase extends \PhpAot\Core\Translator $attr = $expr->getAttribute('stdContainerDimFetch'); return $this->context->stdContainers[$attr['var']]['class'] ?? ''; } + if ($this->isFuncCallExpr($expr) and $this->isNameExpr($expr->name)) { + $fn = $this->parseIdentifier($expr->name); + if (count($expr->args) === 2 and $fn === 'objval') { + return $this->resolveClassNameArg($expr->args[1]->value); + } + } if ($this->isMethodCall($expr) and $this->isNamedMethod($expr->name)) { $method = $this->parseIdentifier($expr->name); if ($method === 'toObject' and !empty($expr->args)) { - $argClass = $expr->args[0]->value; - if ($this->isScalarString($argClass)) { - return $this->getNamespacedClassName($argClass->value); - } - if ($this->isClassConstFetch($argClass)) { - if ($this->isNameExpr($argClass->class) and $this->isIdExpr($argClass->name) and $this->parseIdentifier($argClass->name) === 'class') { - return $this->getNamespacedClassName($this->parseIdentifier($argClass->class)); - } - } + return $this->resolveClassNameArg($expr->args[0]->value); } if ($this->isVarExpr($expr->var)) { $object = $this->parseVariable($expr->var); @@ -2114,6 +2112,19 @@ class CompilerBase extends \PhpAot\Core\Translator return ''; } + protected function resolveClassNameArg(NodeAbstract $arg): string + { + if ($this->isScalarString($arg)) { + return $this->getNamespacedClassName($arg->value); + } + if ($this->isClassConstFetch($arg)) { + if ($this->isNameExpr($arg->class) and $this->isIdExpr($arg->name) and $this->parseIdentifier($arg->name) === 'class') { + return $this->getNamespacedClassName($this->parseIdentifier($arg->class)); + } + } + return ''; + } + protected function parseReturn(Node\Stmt\Return_ $v): string { if ($v->expr === null) { diff --git a/src/polyfills.php b/src/polyfills.php index fb16acdf..5ba32f53 100644 --- a/src/polyfills.php +++ b/src/polyfills.php @@ -89,3 +89,8 @@ function any(mixed $var): mixed return $var; } +function objval(mixed $var, string $className): mixed +{ + return $var; +} +