refactor(php): 优化PHP编译器基础组件结构

- 在CompilerBase中添加类继承关系存储数组并增加注释说明
- 重命名FileSorter中的变量名提高代码可读性
- 简化Preprocessor中局部变量声明方式
- 限定prepareFunction方法参数类型增强类型安全
- 添加关于父类方法覆盖的注释说明
pull/1/head
韩天峰 5 months ago
parent 3743a32eb8
commit bb52f8ab10
  1. 4
      src/Php/CompilerBase.php
  2. 8
      src/Php/FileSorter.php
  3. 5
      src/Php/Preprocessor.php

@ -245,6 +245,10 @@ class CompilerBase extends \PhpAot\Core\Translator
* @var array<string, bool> * @var array<string, bool>
*/ */
protected array $classMethodOverride = []; protected array $classMethodOverride = [];
/**
* 存储所有类继承关系,类名必须全部为小写
* @var array<string, string>
*/
protected array $classExtends = []; protected array $classExtends = [];
public function __construct(string $rootPath) public function __construct(string $rootPath)

@ -66,11 +66,11 @@ class FileSorter
$dependencies = []; $dependencies = [];
foreach ($this->symbolCallInFile as $call) { foreach ($this->symbolCallInFile as $call) {
$callerFile = $call['file']; $callerFile = $call['file'];
$functionName = $call['name']; $symbolName = $call['name'];
if (isset($this->symbolDeclInFile[$functionName])) { if (isset($this->symbolDeclInFile[$symbolName])) {
$declFile = $this->symbolDeclInFile[$functionName]; $declFile = $this->symbolDeclInFile[$symbolName];
if ($callerFile !== $declFile) { if ($callerFile !== $declFile) {
if (!isset($dependencies[$callerFile])) { if (!isset($dependencies[$callerFile])) {

@ -115,7 +115,7 @@ class Preprocessor extends CompilerBase
} }
} }
$nodeFinder = new NodeFinder(); $nodeFinder = new NodeFinder();
$functionCalls = $nodeFinder->findInstanceOf($ast, Node\Expr\FuncCall::class); $functionCalls = $nodeFinder->findInstanceOf($ast, Node\Expr\FuncCall::class);
foreach ($functionCalls as $call) { foreach ($functionCalls as $call) {
@ -158,7 +158,7 @@ class Preprocessor extends CompilerBase
$this->resetNamespace(); $this->resetNamespace();
} }
protected function prepareFunction(Node $v): void protected function prepareFunction(Node\Stmt\ClassMethod|Node\Stmt\Function_ $v): void
{ {
$name = $this->getFunctionName($v); $name = $this->getFunctionName($v);
if ($this->stubFile) { if ($this->stubFile) {
@ -232,6 +232,7 @@ class Preprocessor extends CompilerBase
while (isset($this->classExtends[$fullClassNameLower])) { while (isset($this->classExtends[$fullClassNameLower])) {
$parentClass = $this->classExtends[$fullClassNameLower]; $parentClass = $this->classExtends[$fullClassNameLower];
$parentMethodLower = strtolower($parentClass . '::' . $v->name); $parentMethodLower = strtolower($parentClass . '::' . $v->name);
// 父类有同名方法,子类覆盖了父类方法,这种情况不能直接使用 C++ 函数,而是使用 ZendVM 动态调用
if (isset($this->classMethodOverride[$parentMethodLower])) { if (isset($this->classMethodOverride[$parentMethodLower])) {
$this->classMethodOverride[$parentMethodLower] = true; $this->classMethodOverride[$parentMethodLower] = true;
} }

Loading…
Cancel
Save