Passed
Pull Request — master (#129)
by Sergei
02:56 queued 41s
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 16
    public function __construct(ReflectionParameter $parameter)
18
    {
19 16
        $this->parameter = $parameter;
20 16
    }
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 instantinate "%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" type parameter "%s" when instantinate "%s". ' .
61 1
                'Please specify argument explicitly.',
62 1
                $this->getType(),
63 1
                $this->parameter->getName(),
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 1
        if ($type instanceof ReflectionUnionType) {
78
            /** @var ReflectionNamedType[] */
79
            $namedTypes = $type->getTypes();
80
            $names = array_map(
81
                static fn (ReflectionNamedType $t) => $t->getName(),
82
                $namedTypes
83
            );
84
            return implode('|', $names);
85
        }
86
87
        /** @var ReflectionNamedType $type */
88
89 1
        return $type->getName();
90
    }
91
92 2
    private function getCallable(): string
93
    {
94 2
        $callable = [];
95
96 2
        $class = $this->parameter->getDeclaringClass();
97 2
        if ($class !== null) {
98 2
            $callable[] = $class->getName();
99
        }
100 2
        $callable[] = $this->parameter->getDeclaringFunction()->getName() . '()';
101
102 2
        return implode('::', $callable);
103
    }
104
}
105