From 5ae9484a27e0d162888ee998e57b3aad7f31a655 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 3 Jul 2024 09:03:02 +0200 Subject: [PATCH 1/2] Faster EnumCaseObjectType->describe() --- src/Type/Enum/EnumCaseObjectType.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Type/Enum/EnumCaseObjectType.php b/src/Type/Enum/EnumCaseObjectType.php index 42d8f4afc2..30977fb702 100644 --- a/src/Type/Enum/EnumCaseObjectType.php +++ b/src/Type/Enum/EnumCaseObjectType.php @@ -20,12 +20,16 @@ use PHPStan\Type\SubtractableType; use PHPStan\Type\Type; use PHPStan\Type\VerbosityLevel; +use function array_key_exists; use function sprintf; /** @api */ class EnumCaseObjectType extends ObjectType { + /** @var array */ + private array $descriptions = []; + /** @api */ public function __construct( string $className, @@ -43,9 +47,11 @@ public function getEnumCaseName(): string public function describe(VerbosityLevel $level): string { - $parent = parent::describe($level); - - return sprintf('%s::%s', $parent, $this->enumCaseName); + $value = $level->getLevelValue(); + if (!array_key_exists($value, $this->descriptions)) { + $this->descriptions[$value] = sprintf('%s::%s', parent::describe($level), $this->enumCaseName); + } + return $this->descriptions[$value]; } public function equals(Type $type): bool From 465dfa04dfdb6b6c91ef223c7737f31001d813b2 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 3 Jul 2024 09:42:19 +0200 Subject: [PATCH 2/2] moved description cache into ObjectType --- src/Type/Enum/EnumCaseObjectType.php | 12 +++--------- src/Type/ObjectType.php | 10 +++++++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Type/Enum/EnumCaseObjectType.php b/src/Type/Enum/EnumCaseObjectType.php index 30977fb702..42d8f4afc2 100644 --- a/src/Type/Enum/EnumCaseObjectType.php +++ b/src/Type/Enum/EnumCaseObjectType.php @@ -20,16 +20,12 @@ use PHPStan\Type\SubtractableType; use PHPStan\Type\Type; use PHPStan\Type\VerbosityLevel; -use function array_key_exists; use function sprintf; /** @api */ class EnumCaseObjectType extends ObjectType { - /** @var array */ - private array $descriptions = []; - /** @api */ public function __construct( string $className, @@ -47,11 +43,9 @@ public function getEnumCaseName(): string public function describe(VerbosityLevel $level): string { - $value = $level->getLevelValue(); - if (!array_key_exists($value, $this->descriptions)) { - $this->descriptions[$value] = sprintf('%s::%s', parent::describe($level), $this->enumCaseName); - } - return $this->descriptions[$value]; + $parent = parent::describe($level); + + return sprintf('%s::%s', $parent, $this->enumCaseName); } public function equals(Type $type): bool diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 6a7b22e829..9112871f78 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -97,6 +97,9 @@ class ObjectType implements TypeWithClassName, SubtractableType /** @var array> */ private static array $enumCases = []; + /** @var array */ + private array $descriptions = []; + /** @api */ public function __construct( private string $className, @@ -475,6 +478,11 @@ private function checkSubclassAcceptability(string $thatClass): AcceptsResult public function describe(VerbosityLevel $level): string { + $levelValue = $level->getLevelValue(); + if (array_key_exists($levelValue, $this->descriptions)) { + return $this->descriptions[$levelValue]; + } + $preciseNameCallback = function (): string { $reflectionProvider = ReflectionProviderStaticAccessor::getInstance(); if (!$reflectionProvider->hasClass($this->className)) { @@ -493,7 +501,7 @@ public function describe(VerbosityLevel $level): string return $description; }; - return $level->handle( + return $this->descriptions[$levelValue] = $level->handle( $preciseNameCallback, $preciseNameCallback, $preciseWithSubtracted,