refactor(Php): 重构编译器基础类和类定义结构

- 移除 ClassDef.php 中的多余空行
- 将 isSuperGlobal 方法从 CompilerBase.php 的中间位置移动到文件末尾附近
- 统一注释中的引号格式,使用单引号替代双引号
- 改进 parseExit 方法,添加对空表达式的处理逻辑
- 优化 findNativeMethod 方法的参数类型声明
- 添加方法调用时参数数量的验证检查,确保符合函数定义要求
pull/1/head
韩天峰 7 months ago
parent c58a85baa2
commit 1596c58da6
  1. 1
      src/Php/ClassDef.php
  2. 30
      src/Php/CompilerBase.php

@ -50,7 +50,6 @@ class ClassDef extends ClassLikeDef
return isset($this->constants[$name]);
}
public function getProperty($property): PropertyDef
{
return $this->properties[$property];

@ -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)) {

Loading…
Cancel
Save