Skip to content

Commit

Permalink
More optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 2, 2021
1 parent 0661ca8 commit 91f477f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class ObjectType implements TypeWithClassName, SubtractableType
/** @var array<string, array<string, self>> */
private static array $ancestors = [];

/** @var array<string, self> */
private array $currentAncestors = [];

public function __construct(
string $className,
?Type $subtractedType = null,
Expand Down Expand Up @@ -984,10 +987,15 @@ public function getClassReflection(): ?ClassReflection
*/
public function getAncestorWithClassName(string $className): ?TypeWithClassName
{
if (isset($this->currentAncestors[$className])) {
return $this->currentAncestors[$className];
}

$thisReflection = $this->getClassReflection();
if ($thisReflection === null) {
return null;
}

$description = $this->describeCache() . '-' . $thisReflection->getCacheKey();
if (isset(self::$ancestors[$description][$className])) {
return self::$ancestors[$description][$className];
Expand All @@ -1000,21 +1008,21 @@ public function getAncestorWithClassName(string $className): ?TypeWithClassName
$theirReflection = $broker->getClass($className);

if ($theirReflection->getName() === $thisReflection->getName()) {
return self::$ancestors[$description][$className] = $this;
return self::$ancestors[$description][$className] = $this->currentAncestors[$className] = $this;
}

foreach ($this->getInterfaces() as $interface) {
$ancestor = $interface->getAncestorWithClassName($className);
if ($ancestor !== null) {
return self::$ancestors[$description][$className] = $ancestor;
return self::$ancestors[$description][$className] = $this->currentAncestors[$className] = $ancestor;
}
}

$parent = $this->getParent();
if ($parent !== null) {
$ancestor = $parent->getAncestorWithClassName($className);
if ($ancestor !== null) {
return self::$ancestors[$description][$className] = $ancestor;
return self::$ancestors[$description][$className] = $this->currentAncestors[$className] = $ancestor;
}
}

Expand Down

0 comments on commit 91f477f

Please sign in to comment.