From fa6105ebdf5c81070b2b8156979b6d8721f04ccc Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 2 Apr 2026 12:20:25 +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=BF=BB?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E4=BB=A3=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CompilerBase.php 中引入 MagicMethodDetector trait - 优化类属性声明的代码布局,添加适当的空行分隔 - 使用 checkArgType 方法替换手动类型检查逻辑 - 修复变量重新赋值时的类型检查条件表达式格式 - 更新字符串连接语法使用单引号保持一致性 - 为 Exception 类文件添加标准 PHP 文件头注释 - 从 Translator.php 中移除不再需要的 MagicMethodDetector trait 引用 --- src/Php/CompilerBase.php | 19 ++++++++++++++----- src/Php/Exception/TestError.php | 9 +++++++-- src/Php/Translator.php | 1 - 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index b96b4263..0d0ea582 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -47,6 +47,7 @@ class CompilerBase extends \PhpAot\Core\Translator use ClosureGenerator; use PlaceHolderGenerator; use PropertyPromotion; + use MagicMethodDetector; use Utils; public const string TYPE_VAR = 'php::Var'; @@ -138,6 +139,7 @@ class CompilerBase extends \PhpAot\Core\Translator ]; protected array $localHeaders = []; protected array $internalFunctions = []; + /** * 存储所有函数、类方法的声明,key 是 符号名称,Value 是函数、类方法所在的文件名称 * @var array @@ -178,28 +180,34 @@ class CompilerBase extends \PhpAot\Core\Translator protected string $class = ''; protected string $parentClass = ''; protected string $interface = ''; + /** * @var array */ protected array $interfaces = []; + /** * 存储所有函数、类方法的定义,key 是 native name,命名空间需要转为 `_`,并且必须为小写 * @var array */ protected array $functions = []; + /** * key 类名,包含命名空间 * @var array */ protected array $classes = []; + /** * @var array */ protected array $constants = []; + /** * @var array */ protected array $classesDefineInFile = []; + /** * @var array */ @@ -817,10 +825,10 @@ class CompilerBase extends \PhpAot\Core\Translator if ($returnType !== self::TYPE_VOID) { $this->fatalError($v, 'main function must return void'); } - if ($functionDef->argInfoList[0]->type !== self::TYPE_VAR and $functionDef->argInfoList[0]->type !== self::TYPE_INT) { + if ($this->checkArgType($functionDef->argInfoList[0]->type, self::TYPE_INT)) { $this->fatalError($v, 'The first parameter of the main function must be of type `int`.'); } - if ($functionDef->argInfoList[1]->type !== self::TYPE_VAR and $functionDef->argInfoList[1]->type !== self::TYPE_ARRAY) { + if ($this->checkArgType($functionDef->argInfoList[1]->type, self::TYPE_ARRAY)) { $this->fatalError($v, 'The second parameter of the main function must be of type `array`.'); } } @@ -1398,10 +1406,10 @@ class CompilerBase extends \PhpAot\Core\Translator if (!$this->hasVar($var)) { $this->addLocalVar($var, $type); } else { - if ($this->isTypedObject($var) and $type !== self::TYPE_OBJECT or - $this->getVarType($var) === self::TYPE_ARRAY and ($type !== self::TYPE_ARRAY && $type !== self::TYPE_VAR) + if ($this->isTypedObject($var) and $type !== self::TYPE_OBJECT + or $this->getVarType($var) === self::TYPE_ARRAY and ($type !== self::TYPE_ARRAY && $type !== self::TYPE_VAR) ) { - $this->fatalError($left, "Cannot re-assign variable `\${$var}` from " . $this->getVarType($var) . " to " . $type); + $this->fatalError($left, "Cannot re-assign variable `\${$var}` from " . $this->getVarType($var) . ' to ' . $type); } } } elseif ($this->isPropertyFetch($left) and !$left->getAttribute('nativeProperty')) { @@ -1636,6 +1644,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $this->functions[$this->escapeFunction($name)] = $functionDef; } + /** * @param string $name 必须传入带有完整命名空间的类名,将会自动转义为 native name */ diff --git a/src/Php/Exception/TestError.php b/src/Php/Exception/TestError.php index a107cbc2..af10a122 100644 --- a/src/Php/Exception/TestError.php +++ b/src/Php/Exception/TestError.php @@ -1,8 +1,13 @@