| Total Complexity | 8 |
| Total Lines | 70 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 36 | class Container implements ContainerInterface |
||
| 37 | { |
||
| 38 | /** @var array<class-string<T>,T> */ |
||
|
1 ignored issue
–
show
|
|||
| 39 | private array $container = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param class-string<T> $id |
||
|
1 ignored issue
–
show
|
|||
| 43 | * |
||
| 44 | * @return bool |
||
| 45 | */ |
||
| 46 | public function has(string $id): bool |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param class-string<T> $id |
||
|
1 ignored issue
–
show
|
|||
| 53 | * |
||
| 54 | * @return T |
||
| 55 | */ |
||
| 56 | public function get(string $id): object |
||
| 57 | { |
||
| 58 | return $this->container[$id] ??= $this->make($id); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param class-string<T> $id |
||
|
1 ignored issue
–
show
|
|||
| 63 | * @param T $object |
||
| 64 | * |
||
| 65 | * @return $this |
||
| 66 | */ |
||
| 67 | public function set(string $id, object $object): static |
||
| 68 | { |
||
| 69 | $this->container[$id] = $object; |
||
| 70 | |||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param class-string<T> $id |
||
|
1 ignored issue
–
show
|
|||
| 76 | * |
||
| 77 | * @return T |
||
| 78 | */ |
||
| 79 | private function make(string $id): object |
||
| 80 | { |
||
| 81 | $reflector = new ReflectionClass($id); |
||
| 82 | $constructor = $reflector->getConstructor(); |
||
| 83 | |||
| 84 | if ($constructor === null) { |
||
| 85 | return new $id(); |
||
| 86 | } |
||
| 87 | |||
| 88 | $parameters = array_map(self::makeParameter(...), $constructor->getParameters()); |
||
| 89 | |||
| 90 | return new $id(...$parameters); |
||
| 91 | } |
||
| 92 | |||
| 93 | private function makeParameter(ReflectionParameter $reflection_parameter): mixed |
||
| 106 | } |
||
| 107 | } |
||
| 108 |