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