| Total Complexity | 18 |
| Total Lines | 86 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | class Entry |
||
| 11 | { |
||
| 12 | protected array|Closure|null $args = null; |
||
| 13 | protected bool $asIs = false; |
||
| 14 | protected bool $reify; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param non-empty-string $id |
||
|
|
|||
| 18 | * */ |
||
| 19 | 112 | public function __construct( |
|
| 20 | readonly protected string $id, |
||
| 21 | protected mixed $value |
||
| 22 | ) { |
||
| 23 | 112 | $this->reify = $this->negotiateReify($value); |
|
| 24 | } |
||
| 25 | |||
| 26 | 112 | protected function negotiateReify(mixed $value): bool |
|
| 27 | { |
||
| 28 | 112 | if (is_string($value)) { |
|
| 29 | 91 | if (!class_exists($value)) { |
|
| 30 | 91 | return false; |
|
| 31 | } |
||
| 32 | 112 | } elseif ($value instanceof Closure) { |
|
| 33 | 25 | return true; |
|
| 34 | } else { |
||
| 35 | 112 | if (is_scalar($value) || is_array($value) || is_object($value)) { |
|
| 36 | 112 | return false; |
|
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | 90 | return true; |
|
| 41 | } |
||
| 42 | |||
| 43 | 54 | public function shouldReify(): bool |
|
| 44 | { |
||
| 45 | 54 | return $this->reify; |
|
| 46 | } |
||
| 47 | |||
| 48 | 61 | public function shouldReturnAsIs(): bool |
|
| 49 | { |
||
| 50 | 61 | return $this->asIs; |
|
| 51 | } |
||
| 52 | |||
| 53 | 53 | public function getArgs(): array|Closure|null |
|
| 54 | { |
||
| 55 | 53 | return $this->args; |
|
| 56 | } |
||
| 57 | |||
| 58 | 1 | public function reify(bool $reify = true): self |
|
| 59 | { |
||
| 60 | 1 | $this->reify = $reify; |
|
| 61 | |||
| 62 | 1 | return $this; |
|
| 63 | } |
||
| 64 | |||
| 65 | 2 | public function asIs(bool $asIs = true): self |
|
| 66 | { |
||
| 67 | // An update call is unecessary |
||
| 68 | 2 | if ($asIs) { |
|
| 69 | 2 | $this->reify = false; |
|
| 70 | } |
||
| 71 | |||
| 72 | 2 | $this->asIs = $asIs; |
|
| 73 | |||
| 74 | 2 | return $this; |
|
| 75 | } |
||
| 76 | |||
| 77 | 3 | public function args(array|Closure $args): self |
|
| 78 | { |
||
| 79 | 3 | if ($this->value instanceof Closure) { |
|
| 80 | 1 | throw new ContainerException('Closure values in the registry cannot have arguments'); |
|
| 81 | } |
||
| 82 | |||
| 83 | 2 | $this->args = $args; |
|
| 84 | |||
| 85 | 2 | return $this; |
|
| 86 | } |
||
| 87 | |||
| 88 | 64 | public function value(): mixed |
|
| 89 | { |
||
| 90 | 64 | return $this->value; |
|
| 91 | } |
||
| 92 | |||
| 93 | 53 | public function update(mixed $value): void |
|
| 96 | } |
||
| 97 | } |
||
| 98 |