From 489020d5a316aac33113cc1480ffafe9db4b54e1 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 23 Apr 2026 15:09:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(parser):=20=E4=BC=98=E5=8C=96=E7=B1=BB?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=AE=A1=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 ClassInfo::$currentClass 从对象引用改为字符串存储 - 移除 getCurrentClassName 方法并直接使用 $currentClass 属性 - 在类解析过程中正确设置当前类名字符串值 - 统一常量访问时的类名获取方式 - 简化类名字符串的存储和访问流程 --- src/gen_stub.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/gen_stub.php b/src/gen_stub.php index 84aac6a4..4c437546 100755 --- a/src/gen_stub.php +++ b/src/gen_stub.php @@ -226,7 +226,7 @@ class SimpleType { } 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()); @@ -2304,11 +2304,11 @@ class EvaluatedValue if ($expr instanceof Expr\ClassConstFetch) { $class = $expr->class->toString(); if ($class === 'self') { - $constName = ClassInfo::$currentClass->name->toString() . "::" . $expr->name->__toString(); + $constName = ClassInfo::$currentClass . "::" . $expr->name->__toString(); if (isset($allConstInfos[$constName])) { return $allConstInfos[$constName]->getValue($allConstInfos)->value; } 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') { return $class; @@ -3444,7 +3444,7 @@ class AttributeInfo { } class ClassInfo { - static public ?self $currentClass = null; + static public string $currentClass = ''; public /* readonly */ Name $name; private int $flags; public string $type; @@ -3521,12 +3521,6 @@ class ClassInfo { $this->cond = $cond; $this->phpVersionIdMinimumCompatibility = $minimumPhpVersionIdCompatibility; $this->isUndocumentable = $isUndocumentable; - self::$currentClass = $this; - } - - static public function getCurrentClassName(): string - { - return self::$currentClass->name->toString(); } /** @param array $allConstInfos */ @@ -4448,6 +4442,7 @@ class FileInfo { if (!$stmt instanceof PhpParser\Node\Stmt\Interface_) { getTranslator()->parseTraitUseForStub($stmt, $className); } + ClassInfo::$currentClass = $className->toString(); foreach ($stmt->stmts as $classStmt) { $cond = self::handlePreprocessorConditions($conds, $classStmt); @@ -4581,7 +4576,7 @@ class FileInfo { $code = ""; foreach ($this->classInfos as $class) { - ClassInfo::$currentClass = $class; + ClassInfo::$currentClass = $class->name->toString(); $code .= "\n" . $class->getRegistration($allConstInfos); }