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
Show file tree
Hide file tree
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
31 changes: 22 additions & 9 deletions src/Reflection/Adapter/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
use Roave\BetterReflection\Util\FileHelper;
use Roave\BetterReflection\Util\Memoize;
use ValueError;

use function array_combine;
Expand All @@ -33,9 +34,28 @@
*/
final class ReflectionEnum extends CoreReflectionEnum
{
/** @var Memoize<list<ReflectionEnumUnitCase>|list<ReflectionEnumBackedCase>> */
private Memoize $cases;

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

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

PHPDoc tag @var for property Roave\BetterReflection\Reflection\Adapter\ReflectionEnum::$cases contains generic type Roave\BetterReflection\Util\Memoize<array<int, Roave\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase|Roave\BetterReflection\Reflection\Adapter\ReflectionEnumUnitCase>> but class Roave\BetterReflection\Util\Memoize is not generic.

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

View workflow job for this annotation

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

PHPDoc tag @var for property Roave\BetterReflection\Reflection\Adapter\ReflectionEnum::$cases contains generic type Roave\BetterReflection\Util\Memoize<array<int, Roave\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase|Roave\BetterReflection\Reflection\Adapter\ReflectionEnumUnitCase>> but class Roave\BetterReflection\Util\Memoize is not generic.

public function __construct(private BetterReflectionEnum $betterReflectionEnum)
{
unset($this->name);

$this->cases = new Memoize(function () {

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

View workflow job for this annotation

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

InvalidArgument

src/Reflection/Adapter/ReflectionEnum.php:44:36: InvalidArgument: Argument 1 of Roave\BetterReflection\Util\Memoize::__construct expects null|pure-Closure():mixed, but impure-Closure():list<Roave\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase|Roave\BetterReflection\Reflection\Adapter\ReflectionEnumUnitCase> provided (see https://psalm.dev/004)
$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 $mappedCases;
});
}

/** @return non-empty-string */
Expand Down Expand Up @@ -527,17 +547,10 @@
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);
}

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

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

View workflow job for this annotation

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

ImpureMethodCall

src/Reflection/Adapter/ReflectionEnum.php:553:30: ImpureMethodCall: Cannot call a possibly-mutating method Roave\BetterReflection\Util\Memoize::get from a mutation-free context (see https://psalm.dev/203)

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

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

Method Roave\BetterReflection\Reflection\Adapter\ReflectionEnum::getCases() should return array<int, Roave\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase|Roave\BetterReflection\Reflection\Adapter\ReflectionEnumUnitCase> but returns Roave\BetterReflection\Util\T.

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

View workflow job for this annotation

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

Method Roave\BetterReflection\Reflection\Adapter\ReflectionEnum::getCases() should return array<int, Roave\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase|Roave\BetterReflection\Reflection\Adapter\ReflectionEnumUnitCase> but returns Roave\BetterReflection\Util\T.
}

public function isBacked(): bool
Expand Down
29 changes: 29 additions & 0 deletions src/Util/Memoize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Roave\BetterReflection\Util;

use Closure;

/** * @template T * @internal do not touch: you have been warned. */
final class Memoize
{
private readonly mixed $cached;

Check failure on line 12 in src/Util/Memoize.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

Class Roave\BetterReflection\Util\Memoize has an uninitialized readonly property $cached. Assign it in the constructor.

Check failure on line 12 in src/Util/Memoize.php

View workflow job for this annotation

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

Class Roave\BetterReflection\Util\Memoize has an uninitialized readonly property $cached. Assign it in the constructor.

/** @param pure-Closure(): T $cb */
public function __construct(private Closure|null $cb)

Check failure on line 15 in src/Util/Memoize.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

PHPDoc tag @param for parameter $cb contains unresolvable type.

Check failure on line 15 in src/Util/Memoize.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

PHPDoc type for property Roave\BetterReflection\Util\Memoize::$cb contains unresolvable type.

Check failure on line 15 in src/Util/Memoize.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

PHPDoc type for property Roave\BetterReflection\Util\Memoize::$cb with type mixed is not subtype of native type Closure|null.

Check failure on line 15 in src/Util/Memoize.php

View workflow job for this annotation

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

PHPDoc tag @param for parameter $cb contains unresolvable type.

Check failure on line 15 in src/Util/Memoize.php

View workflow job for this annotation

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

PHPDoc type for property Roave\BetterReflection\Util\Memoize::$cb contains unresolvable type.

Check failure on line 15 in src/Util/Memoize.php

View workflow job for this annotation

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

PHPDoc type for property Roave\BetterReflection\Util\Memoize::$cb with type mixed is not subtype of native type Closure|null.
{
}

/** @return T */

Check failure on line 19 in src/Util/Memoize.php

View workflow job for this annotation

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

MixedInferredReturnType

src/Util/Memoize.php:19:17: MixedInferredReturnType: Could not verify return type 'T' for Roave\BetterReflection\Util\Memoize::get (see https://psalm.dev/047)
public function get(): mixed

Check failure on line 20 in src/Util/Memoize.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

Method Roave\BetterReflection\Util\Memoize::get() has invalid return type Roave\BetterReflection\Util\T.

Check failure on line 20 in src/Util/Memoize.php

View workflow job for this annotation

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

Method Roave\BetterReflection\Util\Memoize::get() has invalid return type Roave\BetterReflection\Util\T.
{
if ($this->cb) {
$this->cached = ($this->cb)();

Check failure on line 23 in src/Util/Memoize.php

View workflow job for this annotation

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

InaccessibleProperty

src/Util/Memoize.php:23:13: InaccessibleProperty: Roave\BetterReflection\Util\Memoize::$cached is marked readonly (see https://psalm.dev/054)

Check failure on line 23 in src/Util/Memoize.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

Readonly property Roave\BetterReflection\Util\Memoize::$cached is assigned outside of the constructor.

Check failure on line 23 in src/Util/Memoize.php

View workflow job for this annotation

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

Readonly property Roave\BetterReflection\Util\Memoize::$cached is assigned outside of the constructor.
$this->cb = null;
}

return $this->cached;

Check failure on line 27 in src/Util/Memoize.php

View workflow job for this annotation

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

MixedReturnStatement

src/Util/Memoize.php:27:16: MixedReturnStatement: Possibly-mixed return value (see https://psalm.dev/138)
}
}
Loading