| Total Complexity | 9 |
| Total Lines | 72 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 9 | final class Locator |
||
| 10 | { |
||
| 11 | private static ?Locator $instance = null; |
||
| 12 | |||
| 13 | /** @var array<string, mixed> */ |
||
| 14 | private array $instanceCache = []; |
||
| 15 | |||
| 16 | 3 | private function __construct() |
|
| 18 | 3 | } |
|
| 19 | |||
| 20 | /** |
||
| 21 | * @codeCoverageIgnore |
||
| 22 | */ |
||
| 23 | private function __clone() |
||
| 25 | } |
||
| 26 | |||
| 27 | 3 | public static function getInstance(): self |
|
| 28 | { |
||
| 29 | 3 | if (self::$instance === null) { |
|
| 30 | 3 | self::$instance = new self(); |
|
| 31 | } |
||
| 32 | |||
| 33 | 3 | return self::$instance; |
|
|
|
|||
| 34 | } |
||
| 35 | |||
| 36 | 2 | public static function resetInstance(): void |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @template T |
||
| 43 | * |
||
| 44 | * @param class-string<T> $className |
||
| 45 | * |
||
| 46 | * @return T|null |
||
| 47 | */ |
||
| 48 | 3 | public function get(string $className) |
|
| 49 | { |
||
| 50 | 3 | if (isset($this->instanceCache[$className])) { |
|
| 51 | /** @var T $instance */ |
||
| 52 | 1 | $instance = $this->instanceCache[$className]; |
|
| 53 | |||
| 54 | 1 | return $instance; |
|
| 55 | } |
||
| 56 | |||
| 57 | 3 | $locatedInstance = AnonymousGlobal::getByClassName($className) |
|
| 58 | 3 | ?? $this->newInstance($className); |
|
| 59 | |||
| 60 | /** @psalm-suppress MixedAssignment */ |
||
| 61 | 3 | $this->instanceCache[$className] = $locatedInstance; |
|
| 62 | |||
| 63 | 3 | return $locatedInstance; |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @template T |
||
| 68 | * |
||
| 69 | * @param class-string<T> $className |
||
| 70 | * |
||
| 71 | * @return T|null |
||
| 72 | */ |
||
| 73 | 3 | private function newInstance(string $className) |
|
| 81 | } |
||
| 82 | } |
||
| 83 |