Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster EnumCaseObjectType->describe() #3208

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions src/Type/Enum/EnumCaseObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> */
private array $descriptions = [];

/** @api */
public function __construct(
string $className,
Expand All @@ -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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to cache this in the parent instead? In ObjectType

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also works. it makes the repro 0,2-0,3 seconds slower though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the cache into ObjectType.

feel free to close this PR, in case it no longer serves a purpose.

}

public function equals(Type $type): bool
Expand Down
Loading