refactor(php): 重构父类解析逻辑以支持完全限定名称

- 添加 getParentClass 方法来处理 NodeAbstract 类型的 extends 参数
- 在 Preprocessor 中使用新的 getParentClass 方法替代直接调用 getNamespacedClassName
- 在 Translator 中使用新的 getParentClass 方法替代直接调用 getNamespacedClassName
- 支持对 FullyQualified 名称类型的正确解析
- 保持对普通名称类型的向后兼容性
pull/1/head
韩天峰 5 months ago
parent afa770bcab
commit 41e46c2479
  1. 12
      src/Php/Preprocessor.php
  2. 2
      src/Php/Translator.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;
}

@ -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) {

Loading…
Cancel
Save