Passed
Push — master ( 3e81fd...ba09db )
by Jonathan
04:14
created

AutoWiringAware::autoWiringArguments()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gravatalonga\Container;
6
7
use Psr\Container\ContainerInterface;
8
use ReflectionException;
9
use ReflectionNamedType;
10
use ReflectionParameter;
11
12
abstract class AutoWiringAware implements ContainerInterface
13
{
14
    /**
15
     * @param ReflectionParameter $reflector
16
     *
17
     * @throws ContainerException
18
     * @throws ReflectionException
19
     *
20
     * @return mixed
21
     */
22 15
    protected function argumentWithoutType(ReflectionParameter $reflector)
23
    {
24 15
        if (false === $reflector->isOptional()) {
25 6
            throw ContainerException::findType($reflector->getName());
26
        }
27
28 9
        if ($reflector->isDefaultValueAvailable()) {
29 9
            return $reflector->getDefaultValue();
30
        }
31
32
        if (true === $reflector->allowsNull()) {
33
            return null;
34
        }
35
36
        throw ContainerException::findType($reflector->getName());
37
    }
38
39
    /**
40
     * @param ReflectionParameter $reflector
41
     *
42
     * @throws ContainerException
43
     * @throws ReflectionException
44
     *
45
     * @return mixed|null
46
     */
47 54
    protected function autoWiringArguments(ReflectionParameter $reflector)
48
    {
49 54
        if (true === $this->has($reflector->getName())) {
50 18
            return $this->get($reflector->getName());
51
        }
52
53
        /** @var ReflectionNamedType|null $type */
54 39
        $type = $reflector->getType();
55
56 39
        if (null !== $type && !$type->isBuiltin()) {
57 24
            return $this->get($type->getName());
58
        }
59
60 15
        return $this->argumentWithoutType($reflector);
61
    }
62
}
63