Passed
Push — main ( c5612c...d21d40 )
by Thomas
02:40
created

Entry::reify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck;
6
7
use Closure;
8
use Conia\Chuck\Exception\ContainerException;
9
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
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
94
    {
95 53
        $this->value = $value;
96
    }
97
}
98