refactor(Php): 重构编译器中的类名解析和参数构建逻辑

- 将 toObject() 方法中的类名解析逻辑提取为独立的 resolveClassNameArg() 方法
- 简化了 UniversalMethodCall 中的参数构建过程
- 提取通用的 buildReceiverArgs() 方法来处理接收者参数构建
- 移除了重复的参数处理代码,提高了代码复用性
- 改进了错误处理的一致性
pull/1/head
韩天峰 3 months ago
parent fc165fea27
commit b04ee5e8b2
  1. 12
      src/Php/CompilerBase.php
  2. 33
      src/Php/UniversalMethodCall.php

@ -5344,16 +5344,8 @@ class CompilerBase extends \PhpAot\Core\Translator
if (empty($expr->args)) { if (empty($expr->args)) {
return 'php::toObject(' . $receiver . ')'; return 'php::toObject(' . $receiver . ')';
} }
$argClass = $expr->args[0]->value; $className = $this->resolveClassNameArg($expr->args[0]->value);
if ($this->isScalarString($argClass)) { if ($className === '') {
$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 {
$this->fatalError($expr, 'The first parameter of toObject() only supports string literals or `ClassName::class` constant'); $this->fatalError($expr, 'The first parameter of toObject() only supports string literals or `ClassName::class` constant');
} }
return 'php::toObject(' . $receiver . ', ' . $this->getClassEntryPtr($className) . ', true)'; return 'php::toObject(' . $receiver . ', ' . $this->getClassEntryPtr($className) . ', true)';

@ -702,28 +702,7 @@ trait UniversalMethodCall
*/ */
protected function genUniversalPhpFn(string $receiver, string $phpFunc, array $args, int $receiverPos = 0, array $constArgs = []): string protected function genUniversalPhpFn(string $receiver, string $phpFunc, array $args, int $receiverPos = 0, array $constArgs = []): string
{ {
$argExprs = []; $argExprs = $this->buildReceiverArgs($receiver, $args, $receiverPos);
$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;
}
}
if ($constArgs) { if ($constArgs) {
$maxPos = max(array_keys($constArgs)); $maxPos = max(array_keys($constArgs));
@ -746,7 +725,12 @@ trait UniversalMethodCall
// Same receiverPos semantics as genUniversalPhpFn. // Same receiverPos semantics as genUniversalPhpFn.
protected function genUniversalCppFn(string $receiver, string $cppFunc, array $args, int $receiverPos = 0): string 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 = []; $userArgs = [];
foreach ($args as $arg) { foreach ($args as $arg) {
$userArgs[] = $this->parseExpr($arg->value); $userArgs[] = $this->parseExpr($arg->value);
@ -758,6 +742,7 @@ trait UniversalMethodCall
$argExprs[] = $ua; $argExprs[] = $ua;
} }
} else { } else {
$argExprs = [];
foreach ($userArgs as $i => $ua) { foreach ($userArgs as $i => $ua) {
if ($i === $receiverPos - 1) { if ($i === $receiverPos - 1) {
$argExprs[] = $receiver; $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 protected function genUniversalPhpFnRef(string $receiver, string $phpFunc, array $args, string $returnType): string

Loading…
Cancel
Save