-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
staabm
Author
Contributor
|
||
|
||
/** | ||
* @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.
Sorry, something went wrong. |
||
} | ||
return $this->cached; | ||
} | ||
} |
I would make this an uninitialized readonly typed property