refactor(php): 重构编译器基础类和翻译器代码结构

- 在 CompilerBase.php 中引入 MagicMethodDetector trait
- 优化类属性声明的代码布局,添加适当的空行分隔
- 使用 checkArgType 方法替换手动类型检查逻辑
- 修复变量重新赋值时的类型检查条件表达式格式
- 更新字符串连接语法使用单引号保持一致性
- 为 Exception 类文件添加标准 PHP 文件头注释
- 从 Translator.php 中移除不再需要的 MagicMethodDetector trait 引用
pull/1/head
韩天峰 5 months ago
parent 8670df2a3e
commit fa6105ebdf
  1. 19
      src/Php/CompilerBase.php
  2. 9
      src/Php/Exception/TestError.php
  3. 1
      src/Php/Translator.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<string, string>
@ -178,28 +180,34 @@ class CompilerBase extends \PhpAot\Core\Translator
protected string $class = '';
protected string $parentClass = '';
protected string $interface = '';
/**
* @var array<string, InterfaceDef>
*/
protected array $interfaces = [];
/**
* 存储所有函数、类方法的定义,key 是 native name,命名空间需要转为 `_`,并且必须为小写
* @var array<string, FunctionDef>
*/
protected array $functions = [];
/**
* key 类名,包含命名空间
* @var array<string, ClassDef>
*/
protected array $classes = [];
/**
* @var array<string, ConstantDef>
*/
protected array $constants = [];
/**
* @var array<string, ClassDef>
*/
protected array $classesDefineInFile = [];
/**
* @var array<string, InterfaceDef>
*/
@ -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
*/

@ -1,8 +1,13 @@
<?php
/**
* This file is part of Swoole-Compiler(AOT).
*
* @link https://www.swoole.com/
* @contact service@swoole.com
*/
namespace PhpAot\Php\Exception;
class TestError extends \RuntimeException
{
}
}

@ -26,7 +26,6 @@ use Symfony\Component\Yaml\Yaml;
class Translator extends Preprocessor
{
use MagicMethodDetector;
protected string $targetName = 'app';
protected array $sourceDirs = [];
protected bool $verbose = false;

Loading…
Cancel
Save