Skip to content

Commit

Permalink
Optimized caching of isSuperTypeOf in ObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 24, 2021
1 parent c6b37ee commit e88491b
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class ObjectType implements TypeWithClassName, SubtractableType

private ?GenericObjectType $genericObjectType = null;

/** @var array<string, \PHPStan\TrinaryLogic> */
private array $superTypes = [];
/** @var array<string, array<string, \PHPStan\TrinaryLogic>> */
private static array $superTypes = [];

public function __construct(
string $className,
Expand Down Expand Up @@ -138,33 +138,34 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic

public function isSuperTypeOf(Type $type): TrinaryLogic
{
$thisDescription = $this->describe(VerbosityLevel::cache());
$description = $type->describe(VerbosityLevel::cache());
if (isset($this->superTypes[$description])) {
return $this->superTypes[$description];
if (isset(self::$superTypes[$thisDescription][$description])) {
return self::$superTypes[$thisDescription][$description];
}

if ($type instanceof CompoundType) {
return $this->superTypes[$description] = $type->isSubTypeOf($this);
return self::$superTypes[$thisDescription][$description] = $type->isSubTypeOf($this);
}

if ($type instanceof ObjectWithoutClassType) {
if ($type->getSubtractedType() !== null) {
$isSuperType = $type->getSubtractedType()->isSuperTypeOf($this);
if ($isSuperType->yes()) {
return $this->superTypes[$description] = TrinaryLogic::createNo();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createNo();
}
}
return $this->superTypes[$description] = TrinaryLogic::createMaybe();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
}

if (!$type instanceof TypeWithClassName) {
return $this->superTypes[$description] = TrinaryLogic::createNo();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createNo();
}

if ($this->subtractedType !== null) {
$isSuperType = $this->subtractedType->isSuperTypeOf($type);
if ($isSuperType->yes()) {
return $this->superTypes[$description] = TrinaryLogic::createNo();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createNo();
}
}

Expand All @@ -174,47 +175,47 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
) {
$isSuperType = $type->getSubtractedType()->isSuperTypeOf($this);
if ($isSuperType->yes()) {
return $this->superTypes[$description] = TrinaryLogic::createNo();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createNo();
}
}

$thisClassName = $this->className;
$thatClassName = $type->getClassName();

if ($thatClassName === $thisClassName) {
return $this->superTypes[$description] = TrinaryLogic::createYes();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createYes();
}

$broker = Broker::getInstance();

if ($this->getClassReflection() === null || !$broker->hasClass($thatClassName)) {
return $this->superTypes[$description] = TrinaryLogic::createMaybe();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
}

$thisClassReflection = $this->getClassReflection();
$thatClassReflection = $broker->getClass($thatClassName);

if ($thisClassReflection->getName() === $thatClassReflection->getName()) {
return $this->superTypes[$description] = TrinaryLogic::createYes();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createYes();
}

if ($thatClassReflection->isSubclassOf($thisClassName)) {
return $this->superTypes[$description] = TrinaryLogic::createYes();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createYes();
}

if ($thisClassReflection->isSubclassOf($thatClassName)) {
return $this->superTypes[$description] = TrinaryLogic::createMaybe();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
}

if ($thisClassReflection->isInterface() && !$thatClassReflection->getNativeReflection()->isFinal()) {
return $this->superTypes[$description] = TrinaryLogic::createMaybe();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
}

if ($thatClassReflection->isInterface() && !$thisClassReflection->getNativeReflection()->isFinal()) {
return $this->superTypes[$description] = TrinaryLogic::createMaybe();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
}

return $this->superTypes[$description] = TrinaryLogic::createNo();
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createNo();
}

public function equals(Type $type): bool
Expand Down

0 comments on commit e88491b

Please sign in to comment.