Skip to content

feat: add api to get ObjectType from ClassReflection and EnumCaseReflection #4099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/Reflection/EnumCaseReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 2 additions & 15 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1639,7 +1626,7 @@ private function getParent(): ?ObjectType
return null;
}

return $this->cachedParent = self::createFromReflection($parentReflection);
return $this->cachedParent = $parentReflection->getObjectType();
}

/** @return ObjectType[] */
Expand All @@ -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
Expand Down
Loading