Passed
Push — master ( 7a62dc...2dc019 )
by Alexander
02:08
created

ParameterDefinition::isVariadic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definition;
6
7
use ReflectionNamedType;
8
use ReflectionParameter;
9
use ReflectionUnionType;
10
use Yiisoft\Factory\DependencyResolverInterface;
11
use Yiisoft\Factory\Exception\NotInstantiableException;
12
13
final class ParameterDefinition implements DefinitionInterface
14
{
15
    private ReflectionParameter $parameter;
16
17 17
    public function __construct(ReflectionParameter $parameter)
18
    {
19 17
        $this->parameter = $parameter;
20 17
    }
21
22 63
    public function isVariadic(): bool
23
    {
24 63
        return $this->parameter->isVariadic();
25
    }
26
27 45
    public function isOptional(): bool
28
    {
29 45
        return $this->parameter->isOptional();
30
    }
31
32 41
    public function hasValue(): bool
33
    {
34 41
        return $this->parameter->isDefaultValueAvailable() || $this->parameter->allowsNull();
35
    }
36
37 46
    public function resolve(DependencyResolverInterface $container)
38
    {
39 46
        if ($this->parameter->isDefaultValueAvailable()) {
40 42
            return $this->parameter->getDefaultValue();
41
        }
42
43 4
        if ($this->parameter->allowsNull()) {
44 2
            return null;
45
        }
46
47 2
        if ($this->isOptional()) {
48 1
            throw new NotInstantiableException(
49 1
                sprintf(
50
                    'Can not determine default value of parameter "%s" when instantiating "%s" ' .
51 1
                    'because it is PHP internal. Please specify argument explicitly.',
52 1
                    $this->parameter->getName(),
53 1
                    $this->getCallable(),
54
                )
55
            );
56
        }
57
58 1
        throw new NotInstantiableException(
59 1
            sprintf(
60
                'Can not determine value of the "%s" parameter of type "%s" when instantiating "%s". ' .
61 1
                'Please specify argument explicitly.',
62 1
                $this->parameter->getName(),
63 1
                $this->getType(),
64 1
                $this->getCallable(),
65
            )
66
        );
67
    }
68
69 1
    private function getType(): string
70
    {
71 1
        $type = $this->parameter->getType();
72
73 1
        if ($type === null) {
74
            return 'undefined';
75
        }
76
77
        /** @psalm-suppress UndefinedClass, TypeDoesNotContainType */
78 1
        if ($type instanceof ReflectionUnionType) {
79
            /** @var ReflectionNamedType[] */
80
            $namedTypes = $type->getTypes();
81
            $names = array_map(
82
                static fn (ReflectionNamedType $t) => $t->getName(),
83
                $namedTypes
84
            );
85
            return implode('|', $names);
86
        }
87
88
        /** @var ReflectionNamedType $type */
89
90 1
        return $type->getName();
91
    }
92
93 2
    private function getCallable(): string
94
    {
95 2
        $callable = [];
96
97 2
        $class = $this->parameter->getDeclaringClass();
98 2
        if ($class !== null) {
99 2
            $callable[] = $class->getName();
100
        }
101 2
        $callable[] = $this->parameter->getDeclaringFunction()->getName() . '()';
102
103 2
        return implode('::', $callable);
104
    }
105
}
106