From bb52f8ab10207e377f28ccefbc42bf36ad2ef1ff Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 25 Mar 2026 16:13:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96PHP=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=99=A8=E5=9F=BA=E7=A1=80=E7=BB=84=E4=BB=B6=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在CompilerBase中添加类继承关系存储数组并增加注释说明 - 重命名FileSorter中的变量名提高代码可读性 - 简化Preprocessor中局部变量声明方式 - 限定prepareFunction方法参数类型增强类型安全 - 添加关于父类方法覆盖的注释说明 --- src/Php/CompilerBase.php | 4 ++++ src/Php/FileSorter.php | 8 ++++---- src/Php/Preprocessor.php | 5 +++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 0f88bf23..b04ccf55 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -245,6 +245,10 @@ class CompilerBase extends \PhpAot\Core\Translator * @var array */ protected array $classMethodOverride = []; + /** + * 存储所有类继承关系,类名必须全部为小写 + * @var array + */ protected array $classExtends = []; public function __construct(string $rootPath) diff --git a/src/Php/FileSorter.php b/src/Php/FileSorter.php index 829e577f..686d14af 100644 --- a/src/Php/FileSorter.php +++ b/src/Php/FileSorter.php @@ -66,11 +66,11 @@ class FileSorter $dependencies = []; foreach ($this->symbolCallInFile as $call) { - $callerFile = $call['file']; - $functionName = $call['name']; + $callerFile = $call['file']; + $symbolName = $call['name']; - if (isset($this->symbolDeclInFile[$functionName])) { - $declFile = $this->symbolDeclInFile[$functionName]; + if (isset($this->symbolDeclInFile[$symbolName])) { + $declFile = $this->symbolDeclInFile[$symbolName]; if ($callerFile !== $declFile) { if (!isset($dependencies[$callerFile])) { diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index e74e2839..82c66a82 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -115,7 +115,7 @@ class Preprocessor extends CompilerBase } } - $nodeFinder = new NodeFinder(); + $nodeFinder = new NodeFinder(); $functionCalls = $nodeFinder->findInstanceOf($ast, Node\Expr\FuncCall::class); foreach ($functionCalls as $call) { @@ -158,7 +158,7 @@ class Preprocessor extends CompilerBase $this->resetNamespace(); } - protected function prepareFunction(Node $v): void + protected function prepareFunction(Node\Stmt\ClassMethod|Node\Stmt\Function_ $v): void { $name = $this->getFunctionName($v); if ($this->stubFile) { @@ -232,6 +232,7 @@ class Preprocessor extends CompilerBase while (isset($this->classExtends[$fullClassNameLower])) { $parentClass = $this->classExtends[$fullClassNameLower]; $parentMethodLower = strtolower($parentClass . '::' . $v->name); + // 父类有同名方法,子类覆盖了父类方法,这种情况不能直接使用 C++ 函数,而是使用 ZendVM 动态调用 if (isset($this->classMethodOverride[$parentMethodLower])) { $this->classMethodOverride[$parentMethodLower] = true; }