Passed
Push — main ( 438344...ee0fb9 )
by Thomas
02:54
created

RegistryEntry::negotiateReify()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 8.8333
cc 7
nc 5
nop 1
crap 7
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
/**
11
 * @psalm-type Args array<string|mixed>|Closure(): array<string|mixed>
12
 */
13
class RegistryEntry
14
{
15
    /** @var Args|null */
0 ignored issues
show
Bug introduced by
The type Conia\Chuck\Args was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
    protected array|Closure|null $args = null;
17
    protected bool $asIs = false;
18
    protected bool $reify;
19
    protected mixed $instance = null;
20
21
    /**
22
     * @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...
23
     * */
24 121
    public function __construct(
25
        readonly protected string $id,
26
        protected mixed $definition
27
    ) {
28 121
        $this->reify = $this->negotiateReify($definition);
29
    }
30
31 121
    protected function negotiateReify(mixed $definition): bool
32
    {
33 121
        if (is_string($definition)) {
34 98
            if (!class_exists($definition)) {
35 98
                return false;
36
            }
37 120
        } elseif ($definition instanceof Closure) {
38 28
            return true;
39
        } else {
40 120
            if (is_scalar($definition) || is_array($definition) || is_object($definition)) {
41 120
                return false;
42
            }
43
        }
44
45 97
        return true;
46
    }
47
48 60
    public function shouldReify(): bool
49
    {
50 60
        return $this->reify;
51
    }
52
53 67
    public function shouldReturnAsIs(): bool
54
    {
55 67
        return $this->asIs;
56
    }
57
58 61
    public function getArgs(): array|Closure|null
59
    {
60 61
        return $this->args;
61
    }
62
63 1
    public function reify(bool $reify = true): self
64
    {
65 1
        $this->reify = $reify;
66
67 1
        return $this;
68
    }
69
70 2
    public function asIs(bool $asIs = true): self
71
    {
72
        // An update call is unecessary
73 2
        if ($asIs) {
74 2
            $this->reify = false;
75
        }
76
77 2
        $this->asIs = $asIs;
78
79 2
        return $this;
80
    }
81
82 8
    public function args(mixed ...$args): self
83
    {
84 8
        $numArgs = count($args);
85
86 8
        if ($numArgs === 1) {
87 5
            if (is_string(array_key_first($args))) {
88
                /** @var Args  */
89 1
                $this->args = $args;
90 4
            } elseif (is_array($args[0]) || $args[0] instanceof Closure) {
91
                /** @var Args  */
92 3
                $this->args = $args[0];
93
            } else {
94 5
                throw new ContainerException(
95 5
                    'Registry entry arguments can be passed as a single associative array, ' .
96 5
                    'as named arguments, or as a Closure'
97 5
                );
98
            }
99 3
        } elseif ($numArgs > 1) {
100 3
            if (!is_string(array_key_first($args))) {
101 1
                throw new ContainerException(
102 1
                    'Registry entry arguments can be passed as a single associative array, ' .
103 1
                    'as named arguments, or as a Closure'
104 1
                );
105
            }
106
107 2
            $this->args = $args;
0 ignored issues
show
Documentation Bug introduced by
It seems like $args of type array<integer,mixed> is incompatible with the declared type Conia\Chuck\Args|null of property $args.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
        }
109
110 6
        return $this;
111
    }
112
113 10
    public function definition(): mixed
114
    {
115 10
        return $this->definition;
116
    }
117
118 3
    public function instance(): mixed
119
    {
120 3
        return $this->instance;
121
    }
122
123 67
    public function get(): mixed
124
    {
125 67
        return $this->instance ?? $this->definition;
126
    }
127
128 60
    public function set(mixed $instance): void
129
    {
130 60
        $this->instance = $instance;
131
    }
132
}
133