From b04ee5e8b24e9cc78b6467bef9d36df9046421a9 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 2 Jun 2026 17:50:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(Php):=20=E9=87=8D=E6=9E=84=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E4=B8=AD=E7=9A=84=E7=B1=BB=E5=90=8D=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=92=8C=E5=8F=82=E6=95=B0=E6=9E=84=E5=BB=BA=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 toObject() 方法中的类名解析逻辑提取为独立的 resolveClassNameArg() 方法 - 简化了 UniversalMethodCall 中的参数构建过程 - 提取通用的 buildReceiverArgs() 方法来处理接收者参数构建 - 移除了重复的参数处理代码,提高了代码复用性 - 改进了错误处理的一致性 --- src/Php/CompilerBase.php | 12 ++---------- src/Php/UniversalMethodCall.php | 33 +++++++++------------------------ 2 files changed, 11 insertions(+), 34 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 8d469c32..102ef7d4 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -5344,16 +5344,8 @@ class CompilerBase extends \PhpAot\Core\Translator if (empty($expr->args)) { return 'php::toObject(' . $receiver . ')'; } - $argClass = $expr->args[0]->value; - if ($this->isScalarString($argClass)) { - $className = $this->getNamespacedClassName($argClass->value); - } elseif ($this->isClassConstFetch($argClass)) { - if ($this->isNameExpr($argClass->class) and $this->isIdExpr($argClass->name) and $this->parseIdentifier($argClass->name) === 'class') { - $className = $this->getNamespacedClassName($this->parseIdentifier($argClass->class)); - } else { - $this->fatalError($expr, 'The first parameter of toObject() only supports string literals or `ClassName::class` constant'); - } - } else { + $className = $this->resolveClassNameArg($expr->args[0]->value); + if ($className === '') { $this->fatalError($expr, 'The first parameter of toObject() only supports string literals or `ClassName::class` constant'); } return 'php::toObject(' . $receiver . ', ' . $this->getClassEntryPtr($className) . ', true)'; diff --git a/src/Php/UniversalMethodCall.php b/src/Php/UniversalMethodCall.php index f713afb8..029aa3e7 100644 --- a/src/Php/UniversalMethodCall.php +++ b/src/Php/UniversalMethodCall.php @@ -702,28 +702,7 @@ trait UniversalMethodCall */ protected function genUniversalPhpFn(string $receiver, string $phpFunc, array $args, int $receiverPos = 0, array $constArgs = []): string { - $argExprs = []; - $userArgs = []; - foreach ($args as $arg) { - $userArgs[] = $this->parseExpr($arg->value); - } - - if ($receiverPos === 0) { - $argExprs = [$receiver]; - foreach ($userArgs as $ua) { - $argExprs[] = $ua; - } - } else { - foreach ($userArgs as $i => $ua) { - if ($i === $receiverPos - 1) { - $argExprs[] = $receiver; - } - $argExprs[] = $ua; - } - if ($receiverPos > count($userArgs)) { - $argExprs[] = $receiver; - } - } + $argExprs = $this->buildReceiverArgs($receiver, $args, $receiverPos); if ($constArgs) { $maxPos = max(array_keys($constArgs)); @@ -746,7 +725,12 @@ trait UniversalMethodCall // Same receiverPos semantics as genUniversalPhpFn. protected function genUniversalCppFn(string $receiver, string $cppFunc, array $args, int $receiverPos = 0): string { - $argExprs = []; + $argExprs = $this->buildReceiverArgs($receiver, $args, $receiverPos); + return $cppFunc . '(' . implode(', ', $argExprs) . ')'; + } + + private function buildReceiverArgs(string $receiver, array $args, int $receiverPos): array + { $userArgs = []; foreach ($args as $arg) { $userArgs[] = $this->parseExpr($arg->value); @@ -758,6 +742,7 @@ trait UniversalMethodCall $argExprs[] = $ua; } } else { + $argExprs = []; foreach ($userArgs as $i => $ua) { if ($i === $receiverPos - 1) { $argExprs[] = $receiver; @@ -769,7 +754,7 @@ trait UniversalMethodCall } } - return $cppFunc . '(' . implode(', ', $argExprs) . ')'; + return $argExprs; } protected function genUniversalPhpFnRef(string $receiver, string $phpFunc, array $args, string $returnType): string