Passed
Push — master ( e5f068...e72230 )
by Jonathan
03:43
created

AutoWiringAware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 2
b 0
f 0
dl 0
loc 41
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A argumentWithoutType() 0 7 2
A autoWiringArguments() 0 14 4
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
        return $reflector->getDefaultValue();
29
    }
30
31
    /**
32
     * @param ReflectionParameter $reflector
33
     *
34
     * @throws ContainerException
35
     * @throws ReflectionException
36
     *
37
     * @return mixed|null
38
     */
39 54
    protected function autoWiringArguments(ReflectionParameter $reflector)
40
    {
41 54
        if (true === $this->has($reflector->getName())) {
42 18
            return $this->get($reflector->getName());
43
        }
44
45
        /** @var ReflectionNamedType|null $type */
46 39
        $type = $reflector->getType();
47
48 39
        if (null !== $type && !$type->isBuiltin()) {
49 24
            return $this->get($type->getName());
50
        }
51
52 15
        return $this->argumentWithoutType($reflector);
53
    }
54
}
55