From 41e46c247943a653e46800f13f0876dd641d0b4b Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 17 Mar 2026 17:35:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E9=87=8D=E6=9E=84=E7=88=B6?= =?UTF-8?q?=E7=B1=BB=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=AE=8C=E5=85=A8=E9=99=90=E5=AE=9A=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 getParentClass 方法来处理 NodeAbstract 类型的 extends 参数 - 在 Preprocessor 中使用新的 getParentClass 方法替代直接调用 getNamespacedClassName - 在 Translator 中使用新的 getParentClass 方法替代直接调用 getNamespacedClassName - 支持对 FullyQualified 名称类型的正确解析 - 保持对普通名称类型的向后兼容性 --- src/Php/Preprocessor.php | 12 +++++++++++- src/Php/Translator.php | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index 0a57dd6e..51f06075 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -10,6 +10,7 @@ namespace PhpAot\Php; use PhpAot\Php\Exception\SyntaxError; use PhpParser\Node; +use PhpParser\NodeAbstract; use PhpParser\NodeFinder; use PhpParser\NodeTraverser; @@ -167,11 +168,20 @@ class Preprocessor extends CompilerBase } } + protected function getParentClass(NodeAbstract $extends): string + { + if ($extends instanceof Node\Name\FullyQualified) { + return $this->parseIdentifier($extends); + } else { + return $this->getNamespacedClassName($this->parseIdentifier($extends)); + } + } + protected function prepareClass(Node\Stmt\Class_|Node\Stmt\Trait_|Node\Stmt\Enum_ $class): string { $this->class = $this->parseIdentifier($class->name); if ($class->extends) { - $this->parentClass = $this->getNamespacedClassName($this->parseIdentifier($class->extends)); + $this->parentClass = $this->getParentClass($class->extends); $fullClassName = $this->getNamespacedClassName($this->class); $this->classExtends[$fullClassName] = $this->parentClass; } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index a4e5da78..17857140 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -818,7 +818,7 @@ class Translator extends Preprocessor } if ($extends) { - $this->classDef->extends = $this->getNamespacedClassName($this->parseIdentifier($class->extends)); + $this->classDef->extends = $this->getParentClass($class->extends); if (isset($this->classes[$this->classDef->extends])) { $parent = $this->classes[$this->classDef->extends]; if ($parent->flags & Modifiers::FINAL) {