Skip to content

Commit

Permalink
Implement Memoize<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Mar 22, 2024
1 parent 7948bc2 commit 12a5789
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
38 changes: 19 additions & 19 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,12 +34,27 @@
*/
final class ReflectionEnum extends CoreReflectionEnum
{
/** @var list<ReflectionEnumUnitCase>|list<ReflectionEnumBackedCase>|null */
private array|null $cases = null;
/** @var Memoize<list<ReflectionEnumUnitCase>|list<ReflectionEnumBackedCase>> */
private Memoize $cases;

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

$this->cases = new Memoize(function() {
$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 @@ -533,23 +549,7 @@ public function getCase(string $name): ReflectionEnumUnitCase|ReflectionEnumBack
/** @return list<ReflectionEnumUnitCase>|list<ReflectionEnumBackedCase> */
public function getCases(): array
{
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 $this->cases = $mappedCases;
return $this->cases->memoize();

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)

ImpureMethodCall

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

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

declare(strict_types=1);

namespace Roave\BetterReflection\Util;

/** @template T */
final class Memoize {
/**
* @var T
* @psalm-suppress PossiblyNullPropertyAssignmentValue
*/
private mixed $cached = null;

This comment has been minimized.

Copy link
@Ocramius

Ocramius Mar 22, 2024

Member

I would make this an uninitialized readonly typed property

This comment has been minimized.

Copy link
@staabm

staabm Mar 22, 2024

Author Contributor

when to write a value into the property when we make it readonly?

should the callable be invoked in __construct() already?


/**
* @var callable(): T
*/
private $fn;

/** @param callable(): T $fn */
public function __construct(callable $fn)
{
$this->fn = $fn;
}

/**
* @return T
*/
public function memoize(): mixed {
if ($this->cached === null) {
$this->cached = ($this->fn)();

This comment has been minimized.

Copy link
@Ocramius

Ocramius Mar 22, 2024

Member

We should discard the function after use

}
return $this->cached;
}
}

0 comments on commit 12a5789

Please sign in to comment.