From 1596c58da660f1f12f4aff0bfa1b42ac01854f5e Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 31 Jan 2026 12:44:23 +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=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=92=8C=E7=B1=BB?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 ClassDef.php 中的多余空行 - 将 isSuperGlobal 方法从 CompilerBase.php 的中间位置移动到文件末尾附近 - 统一注释中的引号格式,使用单引号替代双引号 - 改进 parseExit 方法,添加对空表达式的处理逻辑 - 优化 findNativeMethod 方法的参数类型声明 - 添加方法调用时参数数量的验证检查,确保符合函数定义要求 --- src/Php/ClassDef.php | 1 - src/Php/CompilerBase.php | 30 +++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Php/ClassDef.php b/src/Php/ClassDef.php index 9854a796..c767cb70 100644 --- a/src/Php/ClassDef.php +++ b/src/Php/ClassDef.php @@ -50,7 +50,6 @@ class ClassDef extends ClassLikeDef return isset($this->constants[$name]); } - public function getProperty($property): PropertyDef { return $this->properties[$property]; diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index b028cf63..ec823850 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -236,14 +236,6 @@ class CompilerBase extends \PhpAot\Core\Translator return isset($this->objects[$object]); } - protected function isSuperGlobal(string $var): bool - { - if (isset($this->superGlobalVars[$var])) { - return true; - } - return false; - } - public function parseExpr(mixed $expr) { $type = $expr->getType(); @@ -489,6 +481,14 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->buildDir; } + protected function isSuperGlobal(string $var): bool + { + if (isset($this->superGlobalVars[$var])) { + return true; + } + return false; + } + protected function removeCommonPrefix(string $short, string $long): string { $len = min(strlen($short), strlen($long)); @@ -1771,7 +1771,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $code; } $fn = $this->getFuncPtr($name); - $this->beforeStmtLines[] = "// Func Call: " . $name; + $this->beforeStmtLines[] = '// Func Call: ' . $name; $call = $silent ? 'CALL_SILENT' : 'CALL'; } else { $tmpVar = $this->genTmpVarName(); @@ -2351,8 +2351,11 @@ class CompilerBase extends \PhpAot\Core\Translator return $expr; } - protected function parseExit(Node $node): string + protected function parseExit(Node\Expr\Exit_ $node): string { + if (!$node->expr) { + return 'php::exit(0)'; + } return 'php::exit(' . $this->parseIdentifier($node->expr) . ')'; } @@ -3189,7 +3192,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $methodDef->flags & Modifiers::PUBLIC; } - protected function findNativeMethod(NodeAbstract $expr, string $object, string $method): string|false + protected function findNativeMethod(Node\Expr\MethodCall $expr, string $object, string $method): string|false { $nativeFunc = ''; if ($object === 'this_') { @@ -3207,6 +3210,11 @@ class CompilerBase extends \PhpAot\Core\Translator if (!$this->checkAccessible($classDef, $methodDef)) { $this->fatalError($expr, 'Method `' . $classDef->getNamespacedName() . '::' . $method . '()` is not accessible'); } + if (count($expr->args) < $methodDef->functionDef->argCountRequired) { + $this->fatalError($expr, 'Method `' . $classDef->getNamespacedName() . '::' . $method . '()` requires ' . $methodDef->functionDef->argCountRequired . ' arguments, ' . count($expr->args) . ' given'); + } elseif (count($expr->args) > count($methodDef->functionDef->argInfoList)) { + $this->fatalError($expr, 'Method `' . $classDef->getNamespacedName() . '::' . $method . '()` accepts ' . count($methodDef->functionDef->argInfoList) . ' arguments, ' . count($expr->args) . ' given'); + } $nativeFunc = $this->getNativeName($method, $classDef->namespace, $classDef->name); } if ($nativeFunc and $this->isNativeFunction($nativeFunc)) {