Skip to content

Commit 9cf5370

Browse files
calebdwondrejmirtes
authored andcommitted
feat: add api to get ObjectType from ClassReflection and EnumCaseReflection
1 parent ae7fd77 commit 9cf5370

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/Reflection/ClassReflection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,19 @@ public function getAttributeClassFlags(): int
14231423
return $flags;
14241424
}
14251425

1426+
public function getObjectType(): ObjectType
1427+
{
1428+
if (!$this->isGeneric()) {
1429+
return new ObjectType($this->getName());
1430+
}
1431+
1432+
return new GenericObjectType(
1433+
$this->getName(),
1434+
$this->typeMapToList($this->getActiveTemplateTypeMap()),
1435+
variances: $this->varianceMapToList($this->getCallSiteVarianceMap()),
1436+
);
1437+
}
1438+
14261439
public function getTemplateTypeMap(): TemplateTypeMap
14271440
{
14281441
if ($this->templateTypeMap !== null) {

src/Reflection/EnumCaseReflection.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Internal\DeprecatedAttributeHelper;
88
use PHPStan\Reflection\Deprecation\DeprecationProvider;
99
use PHPStan\TrinaryLogic;
10+
use PHPStan\Type\Enum\EnumCaseObjectType;
1011
use PHPStan\Type\Type;
1112

1213
/**
@@ -55,6 +56,14 @@ public function getName(): string
5556
return $this->reflection->getName();
5657
}
5758

59+
public function getEnumCaseObjectType(): EnumCaseObjectType
60+
{
61+
return new EnumCaseObjectType(
62+
$this->declaringEnum->getName(),
63+
$this->getName(),
64+
);
65+
}
66+
5867
public function getBackingValueType(): ?Type
5968
{
6069
return $this->backingValueType;

src/Type/ObjectType.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,6 @@ public static function resetCaches(): void
133133
self::$enumCases = [];
134134
}
135135

136-
private static function createFromReflection(ClassReflection $reflection): self
137-
{
138-
if (!$reflection->isGeneric()) {
139-
return new ObjectType($reflection->getName());
140-
}
141-
142-
return new GenericObjectType(
143-
$reflection->getName(),
144-
$reflection->typeMapToList($reflection->getActiveTemplateTypeMap()),
145-
variances: $reflection->varianceMapToList($reflection->getCallSiteVarianceMap()),
146-
);
147-
}
148-
149136
public function getClassName(): string
150137
{
151138
return $this->className;
@@ -1639,7 +1626,7 @@ private function getParent(): ?ObjectType
16391626
return null;
16401627
}
16411628

1642-
return $this->cachedParent = self::createFromReflection($parentReflection);
1629+
return $this->cachedParent = $parentReflection->getObjectType();
16431630
}
16441631

16451632
/** @return ObjectType[] */
@@ -1653,7 +1640,7 @@ private function getInterfaces(): array
16531640
return $this->cachedInterfaces = [];
16541641
}
16551642

1656-
return $this->cachedInterfaces = array_map(static fn (ClassReflection $interfaceReflection): self => self::createFromReflection($interfaceReflection), $thisReflection->getInterfaces());
1643+
return $this->cachedInterfaces = array_map(static fn (ClassReflection $interfaceReflection): self => $interfaceReflection->getObjectType(), $thisReflection->getInterfaces());
16571644
}
16581645

16591646
public function tryRemove(Type $typeToRemove): ?Type

0 commit comments

Comments
 (0)