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

Improve ReflectionEnum->getCases() performance #1410

Open
wants to merge 8 commits into
base: 6.30.x
Choose a base branch
from
Open
Changes from 4 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
26 changes: 19 additions & 7 deletions src/Reflection/Adapter/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
*/
final class ReflectionEnum extends CoreReflectionEnum
{
/** @var list<ReflectionEnumUnitCase>|list<ReflectionEnumBackedCase>|null */
private array|null $cases = null;

public function __construct(private BetterReflectionEnum $betterReflectionEnum)
{
unset($this->name);
Expand Down Expand Up @@ -527,17 +530,26 @@
return new ReflectionEnumUnitCase($case);
}

/** @return list<ReflectionEnumUnitCase|ReflectionEnumBackedCase> */
/** @return list<ReflectionEnumUnitCase>|list<ReflectionEnumBackedCase> */
public function getCases(): array
{
/** @psalm-suppress ImpureFunctionCall */
return array_map(function (BetterReflectionEnumCase $case): ReflectionEnumUnitCase|ReflectionEnumBackedCase {
if ($this->betterReflectionEnum->isBacked()) {
return new ReflectionEnumBackedCase($case);
if ($this->cases !== null) {
return $this->cases;
}

$isBacked = $this->betterReflectionEnum->isBacked();
$cases = $this->betterReflectionEnum->getCases();

$mappedCases = [];
foreach ($cases as $case) {
if ($isBacked) {
$mappedCases[] = new ReflectionEnumBackedCase($case);
} else {
$mappedCases[] = new ReflectionEnumUnitCase($case);
}
}

return new ReflectionEnumUnitCase($case);
}, array_values($this->betterReflectionEnum->getCases()));
return $this->cases = $mappedCases;

Check failure on line 552 in src/Reflection/Adapter/ReflectionEnum.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

InaccessibleProperty

src/Reflection/Adapter/ReflectionEnum.php:552:16: InaccessibleProperty: Roave\BetterReflection\Reflection\Adapter\ReflectionEnum::$cases is marked readonly (see https://psalm.dev/054)
}

public function isBacked(): bool
Expand Down
Loading