From 369cc186b6274a390a398ca74a2de6d715a8a47c Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 1 Feb 2021 17:44:20 +0100 Subject: [PATCH] Optimization of ObjectType::isSuperTypeOf() --- phpcs.xml | 1 + src/Type/ObjectType.php | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index 3b7d0ad724..ee361a5d09 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -2,6 +2,7 @@ + diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 91b42d7690..7702e3a4a7 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -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]; } @@ -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()) {