From 995e2239151638fdb8e2a68ea734585bfde5bfc4 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sat, 11 Jul 2026 18:38:14 +0800 Subject: [PATCH] refactor(compiler): remove unused imports and move global headers to translator - Removed unused TypePhp\Exception\PlaceHolder import - Removed unused TypePhp\Resolver\InstancePropertyFetchTarget import - Removed unused TypePhp\Resolver\PropertyAssignTypeInfo, PropertyWriteTarget, StaticPropertyFetchResolution, and StaticPropertyFetchTarget imports - Removed unused PhpParser\Node\Stmt\Foreach_ import - Removed unused TypePhp\Platform\Linux import - Removed unused getTargetFileName method from NativeBuildConfigurationTrait - Moved globalHeaders array from CompilerBase to Translator class - Moved classCeList and classCeInfo arrays from CompilerBase to Translator class - Updated ABI version retrieval to use constant() function instead of direct constants - Added proper PHPDoc comments for functions and classes arrays in SymbolRepository --- src/Build/NativeBuildConfigurationTrait.php | 18 ---------- src/CompilerBase.php | 40 --------------------- src/Symbol/SymbolRepository.php | 10 ++++-- src/Translator.php | 28 ++++++++++----- 4 files changed, 27 insertions(+), 69 deletions(-) diff --git a/src/Build/NativeBuildConfigurationTrait.php b/src/Build/NativeBuildConfigurationTrait.php index 054a1709..3e67d90c 100644 --- a/src/Build/NativeBuildConfigurationTrait.php +++ b/src/Build/NativeBuildConfigurationTrait.php @@ -7,7 +7,6 @@ namespace TypePhp\Build; -use TypePhp\Platform\Linux; use TypePhp\Platform\Windows; trait NativeBuildConfigurationTrait @@ -162,21 +161,4 @@ trait NativeBuildConfigurationTrait return $this->getPlatform()->getLibraryFlags($this->getLibraries()); } - protected function getTargetFileName(): string - { - $targetFile = $this->targetName; - $extension = $this->getPlatform()->getTargetExtension($this->buildMode); - - if ($extension !== '' && !str_ends_with($targetFile, $extension)) { - $targetFile .= $extension; - } - - if ($this->outputDir !== '') { - $targetFile = rtrim($this->outputDir, '/\\') . '/' . $targetFile; - } - - return $targetFile; - } - } - diff --git a/src/CompilerBase.php b/src/CompilerBase.php index 206274a8..4d3df31f 100644 --- a/src/CompilerBase.php +++ b/src/CompilerBase.php @@ -26,7 +26,6 @@ use TypePhp\Entity\InterfaceDef; use TypePhp\Entity\MethodDef; use TypePhp\Entity\PropertyDef; use TypePhp\Exception\DynamicCall; -use TypePhp\Exception\PlaceHolder; use TypePhp\Exception\Redo; use TypePhp\Generator\AnonClassGenerator; use TypePhp\Generator\CallArgumentGenerator; @@ -67,7 +66,6 @@ use TypePhp\Platform\Macos; use TypePhp\Platform\PlatformBase; use TypePhp\Platform\PlatformFactory; use TypePhp\Platform\Windows; -use TypePhp\Resolver\InstancePropertyFetchTarget; use TypePhp\Resolver\DeclarationSymbolTrait; use TypePhp\Resolver\MagicMethodDetector; use TypePhp\Resolver\PropertyAccessContext; @@ -75,11 +73,7 @@ use TypePhp\Resolver\NativePropertyAccess; use TypePhp\Resolver\NameResolutionTrait; use TypePhp\Resolver\PropertyAccessResult; use TypePhp\Resolver\PropertyAccessResolver; -use TypePhp\Resolver\PropertyAssignTypeInfo; -use TypePhp\Resolver\PropertyWriteTarget; use TypePhp\Resolver\Reflection; -use TypePhp\Resolver\StaticPropertyFetchResolution; -use TypePhp\Resolver\StaticPropertyFetchTarget; use TypePhp\Symbol\SymbolRepository; use TypePhp\TypeSystem\CompositeTypeCheckerTrait; use TypePhp\TypeSystem\NativeTypeCompatibilityTrait; @@ -90,7 +84,6 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\Variable; use PhpParser\Node\FunctionLike; -use PhpParser\Node\Stmt\Foreach_; use PhpParser\NodeAbstract; use PhpParser\NodeFinder; use PhpParser\Parser; @@ -270,16 +263,6 @@ class CompilerBase implements PropertyAccessContext 'decimal' => Type::DECIMAL, 'box' => Type::BOX, ]; - protected array $globalHeaders = [ - 'phpx.h', - 'phpx_helper.h', - 'phpx_big_int.h', - 'phpx_big_float.h', - 'phpx_decimal.h', - 'typephp_helper.h', - 'typephp_fiber_generator.h', - 'phpx_std.h', - ]; protected array $localHeaders = []; protected array $internalFunctions = []; protected array $internalConstants = []; @@ -338,46 +321,23 @@ class CompilerBase implements PropertyAccessContext protected string $class = ''; protected string $parentClass = ''; protected string $interface = ''; - - /** - * @var array - */ - - /** - * 存储所有函数、类方法的定义,key 是 native name,命名空间需要转为 `_`,并且必须为小写 - * @var array - */ - - /** - * key 类名,包含命名空间 - * @var array - */ - /** * @var array */ protected array $constants = []; - /** * @var array */ protected array $classesDefineInFile = []; - /** * @var array */ protected array $interfacesDefineInFile = []; - /** * @var array */ protected array $functionDefineInFile = []; - /** - * @var array - */ - protected array $classCeList = []; - protected array $classCeInfo = []; protected ?FunctionDef $functionDef = null; protected ?ClassDef $classDef = null; protected ?MethodDef $methodDef = null; diff --git a/src/Symbol/SymbolRepository.php b/src/Symbol/SymbolRepository.php index 56af9386..7001b85a 100644 --- a/src/Symbol/SymbolRepository.php +++ b/src/Symbol/SymbolRepository.php @@ -8,9 +8,15 @@ use TypePhp\Entity\InterfaceDef; final class SymbolRepository { - /** @var array */ + /** + * 存储所有函数、类方法的定义,key 是 native name,命名空间需要转为 `_`,并且必须为小写 + * @var array + */ private array $functions = []; - /** @var array */ + /** + * key 类名,包含命名空间 + * @var array + */ private array $classes = []; /** @var array */ private array $interfaces = []; diff --git a/src/Translator.php b/src/Translator.php index 146a5473..0055f24e 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -27,8 +27,6 @@ use TypePhp\Entity\MethodDef; use TypePhp\Entity\PropertyDef; use TypePhp\Exception\Redo; use TypePhp\Exception\Skip; -use TypePhp\Exception\SyntaxError; -use TypePhp\Exception\Unsupported; use TypePhp\Generator\DefaultArgumentGenerator; use TypePhp\Generator\Symbol; use TypePhp\Metadata\Constants; @@ -39,7 +37,6 @@ use TypePhp\Resolver\ClassConstantValueTrait; use TypePhp\Transform\Visitor; use PhpParser\Modifiers; use PhpParser\Node; -use PhpParser\Node\ArrayItem; use PhpParser\Node\Stmt\Foreach_; use PhpParser\NodeAbstract; use PhpParser\NodeTraverser; @@ -54,6 +51,8 @@ class Translator extends Preprocessor public const string VERSION = '0.3.0'; public const string APP_NAME = 'TypePHP Compiler (AOT)'; + protected const string MODULE_NAME_PREFIX = 'app_'; + protected string $targetName = 'app'; protected bool $hasExplicitOutput = false; protected ?string $explicitOutputExtension = null; @@ -69,17 +68,28 @@ class Translator extends Preprocessor // Windows 资源文件配置(图标、版本信息等) protected array $resourceConfig = []; - protected bool $useRegisterSymbolsFn = false; - - protected const string MODULE_NAME_PREFIX = 'app_'; + protected array $globalHeaders = [ + 'phpx.h', + 'phpx_helper.h', + 'phpx_big_int.h', + 'phpx_big_float.h', + 'phpx_decimal.h', + 'typephp_helper.h', + 'typephp_fiber_generator.h', + 'phpx_std.h', + ]; + /** + * @var array + */ + protected array $classCeList = []; + protected array $classCeInfo = []; protected function isConstructorNativeFunction(FunctionDef $func): bool { return $func->method && str_ends_with($func->name, self::NAMESPACE_SEPARATOR . '__construct'); } - public function __construct(string $rootPath) { parent::__construct($rootPath); @@ -1074,8 +1084,8 @@ CODE; { $abi = [ 'php_version_id' => PHP_VERSION_ID, - 'php_api_version' => defined('PHP_API_VERSION') ? PHP_API_VERSION : null, - 'zend_module_api' => defined('ZEND_MODULE_API_NO') ? ZEND_MODULE_API_NO : null, + 'php_api_version' => defined('PHP_API_VERSION') ? constant('PHP_API_VERSION') : null, + 'zend_module_api' => defined('ZEND_MODULE_API_NO') ? constant('ZEND_MODULE_API_NO') : null, 'php_zts' => defined('PHP_ZTS') ? PHP_ZTS : null, 'php_debug' => defined('PHP_DEBUG') ? PHP_DEBUG : null, 'integer_size' => PHP_INT_SIZE,