Skip to content

Commit

Permalink
Merge pull request #1414 from staabm/parent
Browse files Browse the repository at this point in the history
More efficient `ReflectionClass->getParentClass()`
  • Loading branch information
Ocramius authored Mar 26, 2024
2 parents d63ad67 + f757f21 commit bc1d04b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<code><![CDATA[reflectClass]]></code>
<code><![CDATA[reflectClass]]></code>
<code><![CDATA[reflectClass]]></code>
<code><![CDATA[reflectClass]]></code>
<code><![CDATA[toLowerString]]></code>
</ImpureMethodCall>
</file>
Expand Down
10 changes: 7 additions & 3 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,12 +1086,16 @@ public function getEndColumn(): int
*/
public function getParentClass(): ReflectionClass|null
{
$parentClass = $this->getParentClasses()[0] ?? null;

if ($parentClass === null) {
$parentClassName = $this->getParentClassName();
if ($parentClassName === null) {
return null;
}

if ($this->name === $parentClassName) {
throw CircularReference::fromClassName($parentClassName);
}

$parentClass = $this->reflector->reflectClass($parentClassName);
if ($parentClass->isInterface() || $parentClass->isTrait()) {
throw NotAClassReflection::fromReflectionClass($parentClass);
}
Expand Down

0 comments on commit bc1d04b

Please sign in to comment.