Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Type/Constant/ConstantStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class ConstantStringType extends StringType implements ConstantScalarType

private ?Type $arrayKeyType = null;

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

/** @api */
public function __construct(private string $value, private bool $isClassString = false)
{
Expand Down Expand Up @@ -119,9 +122,15 @@ public function getObjectTypeOrClassStringObjectType(): Type

public function describe(VerbosityLevel $level): string
{
$levelValue = $level->getLevelValue();

if (isset($this->cachedDescriptions[$levelValue])) {
return $this->cachedDescriptions[$levelValue];
}

return $level->handle(
static fn (): string => 'string',
function (): string {
function () use ($levelValue): string {
$value = $this->value;

if (!$this->isClassString) {
Expand All @@ -132,9 +141,9 @@ function (): string {
}
}

return self::export($value);
return $this->cachedDescriptions[$levelValue] = self::export($value);
},
fn (): string => self::export($this->value),
fn (): string => $this->cachedDescriptions[$levelValue] = self::export($this->value),
);
}

Expand Down
Loading