Skip to content

Commit

Permalink
Optimization of ObjectType::isSuperTypeOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 1, 2021
1 parent f831917 commit 369cc18
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<ruleset name="PHPStan">
<rule ref="build-cs/vendor/consistence/coding-standard/Consistence/ruleset.xml">
<exclude name="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidFormat"/>
<exclude name="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.MissingVariable"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameAfterKeyword"/>
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation"/>
<exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly"/>
Expand Down
25 changes: 23 additions & 2 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,19 @@ 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 (static::class === self::class) {
$thisDescription = $this->describeCache();
} else {
$thisDescription = $this->describe(VerbosityLevel::cache());
}

if (get_class($type) === self::class) {
/** @var self $type */
$description = $type->describeCache();
} else {
$description = $type->describe(VerbosityLevel::cache());
}

if (isset(self::$superTypes[$thisDescription][$description])) {
return self::$superTypes[$thisDescription][$description];
}
Expand Down Expand Up @@ -298,6 +309,16 @@ function () use ($level): string {
);
}

private function describeCache(): string
{
$description = $this->className;
if ($this->subtractedType !== null) {
$description .= sprintf('~%s', $this->subtractedType->describe(VerbosityLevel::cache()));
}

return $description;
}

public function toNumber(): Type
{
if ($this->isInstanceOf('SimpleXMLElement')->yes()) {
Expand Down

0 comments on commit 369cc18

Please sign in to comment.