refactor(parser): 优化类信息管理逻辑

- 将 ClassInfo::$currentClass 从对象引用改为字符串存储
- 移除 getCurrentClassName 方法并直接使用 $currentClass 属性
- 在类解析过程中正确设置当前类名字符串值
- 统一常量访问时的类名获取方式
- 简化类名字符串的存储和访问流程
pull/1/head
韩天峰 4 months ago
parent 7ee03288ac
commit 489020d5a3
  1. 17
      src/gen_stub.php

@ -226,7 +226,7 @@ class SimpleType {
} }
if ($node->toLowerString() === 'self') { if ($node->toLowerString() === 'self') {
return new SimpleType(ClassInfo::getCurrentClassName(), false); return new SimpleType(ClassInfo::$currentClass, false);
} }
$class = $node->isFullyQualified() ? $node->toString() : getTranslator()->getNamespacedClassName($node->toString()); $class = $node->isFullyQualified() ? $node->toString() : getTranslator()->getNamespacedClassName($node->toString());
@ -2304,11 +2304,11 @@ class EvaluatedValue
if ($expr instanceof Expr\ClassConstFetch) { if ($expr instanceof Expr\ClassConstFetch) {
$class = $expr->class->toString(); $class = $expr->class->toString();
if ($class === 'self') { if ($class === 'self') {
$constName = ClassInfo::$currentClass->name->toString() . "::" . $expr->name->__toString(); $constName = ClassInfo::$currentClass . "::" . $expr->name->__toString();
if (isset($allConstInfos[$constName])) { if (isset($allConstInfos[$constName])) {
return $allConstInfos[$constName]->getValue($allConstInfos)->value; return $allConstInfos[$constName]->getValue($allConstInfos)->value;
} else { } else {
return getTranslator()->getClassConstValue($expr, ClassInfo::$currentClass->name->toString(), $expr->name->toString()); return getTranslator()->getClassConstValue($expr, ClassInfo::$currentClass, $expr->name->toString());
} }
} elseif ($expr->name->__toString() === 'class') { } elseif ($expr->name->__toString() === 'class') {
return $class; return $class;
@ -3444,7 +3444,7 @@ class AttributeInfo {
} }
class ClassInfo { class ClassInfo {
static public ?self $currentClass = null; static public string $currentClass = '';
public /* readonly */ Name $name; public /* readonly */ Name $name;
private int $flags; private int $flags;
public string $type; public string $type;
@ -3521,12 +3521,6 @@ class ClassInfo {
$this->cond = $cond; $this->cond = $cond;
$this->phpVersionIdMinimumCompatibility = $minimumPhpVersionIdCompatibility; $this->phpVersionIdMinimumCompatibility = $minimumPhpVersionIdCompatibility;
$this->isUndocumentable = $isUndocumentable; $this->isUndocumentable = $isUndocumentable;
self::$currentClass = $this;
}
static public function getCurrentClassName(): string
{
return self::$currentClass->name->toString();
} }
/** @param array<string, ConstInfo> $allConstInfos */ /** @param array<string, ConstInfo> $allConstInfos */
@ -4448,6 +4442,7 @@ class FileInfo {
if (!$stmt instanceof PhpParser\Node\Stmt\Interface_) { if (!$stmt instanceof PhpParser\Node\Stmt\Interface_) {
getTranslator()->parseTraitUseForStub($stmt, $className); getTranslator()->parseTraitUseForStub($stmt, $className);
} }
ClassInfo::$currentClass = $className->toString();
foreach ($stmt->stmts as $classStmt) { foreach ($stmt->stmts as $classStmt) {
$cond = self::handlePreprocessorConditions($conds, $classStmt); $cond = self::handlePreprocessorConditions($conds, $classStmt);
@ -4581,7 +4576,7 @@ class FileInfo {
$code = ""; $code = "";
foreach ($this->classInfos as $class) { foreach ($this->classInfos as $class) {
ClassInfo::$currentClass = $class; ClassInfo::$currentClass = $class->name->toString();
$code .= "\n" . $class->getRegistration($allConstInfos); $code .= "\n" . $class->getRegistration($allConstInfos);
} }

Loading…
Cancel
Save