Passed
Pull Request — master (#129)
by Sergei
02:56 queued 41s
created

ParameterDefinition   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 88.37%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
eloc 40
c 2
b 1
f 0
dl 0
loc 90
ccs 38
cts 43
cp 0.8837
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getCallable() 0 11 2
A isVariadic() 0 3 1
A __construct() 0 3 1
A resolve() 0 28 4
A getType() 0 21 3
A isOptional() 0 3 1
A hasValue() 0 3 2
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