Skip to content

Commit

Permalink
moved description cache into ObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jul 3, 2024
1 parent 5ae9484 commit 465dfa0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 3 additions & 9 deletions src/Type/Enum/EnumCaseObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> */
private array $descriptions = [];

/** @api */
public function __construct(
string $className,
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class ObjectType implements TypeWithClassName, SubtractableType
/** @var array<string, list<EnumCaseObjectType>> */
private static array $enumCases = [];

/** @var array<int, string> */
private array $descriptions = [];

/** @api */
public function __construct(
private string $className,
Expand Down Expand Up @@ -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)) {
Expand All @@ -493,7 +501,7 @@ public function describe(VerbosityLevel $level): string
return $description;
};

return $level->handle(
return $this->descriptions[$levelValue] = $level->handle(
$preciseNameCallback,
$preciseNameCallback,
$preciseWithSubtracted,
Expand Down

0 comments on commit 465dfa0

Please sign in to comment.