From e7d3e6b70a3ee44b0ccdcc0cd0689b5724d177e4 Mon Sep 17 00:00:00 2001 From: Caleb White Date: Mon, 14 Jul 2025 13:10:50 -0500 Subject: [PATCH] feat: add api to get ObjectType from ClassReflection and EnumCaseReflection --- src/Reflection/ClassReflection.php | 13 +++++++++++++ src/Reflection/EnumCaseReflection.php | 9 +++++++++ src/Type/ObjectType.php | 17 ++--------------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 45c0fd4bff..b4787074b4 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -1422,6 +1422,19 @@ public function getAttributeClassFlags(): int return $flags; } + public function getObjectType(): ObjectType + { + if (!$this->isGeneric()) { + return new ObjectType($this->getName()); + } + + return new GenericObjectType( + $this->getName(), + $this->typeMapToList($this->getActiveTemplateTypeMap()), + variances: $this->varianceMapToList($this->getCallSiteVarianceMap()), + ); + } + public function getTemplateTypeMap(): TemplateTypeMap { if ($this->templateTypeMap !== null) { diff --git a/src/Reflection/EnumCaseReflection.php b/src/Reflection/EnumCaseReflection.php index 7a86e38726..94cced2739 100644 --- a/src/Reflection/EnumCaseReflection.php +++ b/src/Reflection/EnumCaseReflection.php @@ -7,6 +7,7 @@ use PHPStan\Internal\DeprecatedAttributeHelper; use PHPStan\Reflection\Deprecation\DeprecationProvider; use PHPStan\TrinaryLogic; +use PHPStan\Type\Enum\EnumCaseObjectType; use PHPStan\Type\Type; /** @@ -55,6 +56,14 @@ public function getName(): string return $this->reflection->getName(); } + public function getEnumCaseObjectType(): EnumCaseObjectType + { + return new EnumCaseObjectType( + $this->declaringEnum->getName(), + $this->getName(), + ); + } + public function getBackingValueType(): ?Type { return $this->backingValueType; diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 3d9bb96326..25ee53d4e9 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -133,19 +133,6 @@ public static function resetCaches(): void self::$enumCases = []; } - private static function createFromReflection(ClassReflection $reflection): self - { - if (!$reflection->isGeneric()) { - return new ObjectType($reflection->getName()); - } - - return new GenericObjectType( - $reflection->getName(), - $reflection->typeMapToList($reflection->getActiveTemplateTypeMap()), - variances: $reflection->varianceMapToList($reflection->getCallSiteVarianceMap()), - ); - } - public function getClassName(): string { return $this->className; @@ -1639,7 +1626,7 @@ private function getParent(): ?ObjectType return null; } - return $this->cachedParent = self::createFromReflection($parentReflection); + return $this->cachedParent = $parentReflection->getObjectType(); } /** @return ObjectType[] */ @@ -1653,7 +1640,7 @@ private function getInterfaces(): array return $this->cachedInterfaces = []; } - return $this->cachedInterfaces = array_map(static fn (ClassReflection $interfaceReflection): self => self::createFromReflection($interfaceReflection), $thisReflection->getInterfaces()); + return $this->cachedInterfaces = array_map(static fn (ClassReflection $interfaceReflection): self => $interfaceReflection->getObjectType(), $thisReflection->getInterfaces()); } public function tryRemove(Type $typeToRemove): ?Type