AbstractDefinition::mapParameterToInjection()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace TheCodingMachine\Funky;
5
6
use Psr\Container\ContainerInterface;
7
use ReflectionMethod;
8
use ReflectionParameter;
9
use TheCodingMachine\Funky\Annotations\Factory;
10
use TheCodingMachine\Funky\Annotations\Tag;
11
use TheCodingMachine\Funky\Injections\ContainerInjection;
12
use TheCodingMachine\Funky\Injections\Injection;
13
use TheCodingMachine\Funky\Injections\ServiceInjection;
14
15
abstract class AbstractDefinition
16
{
17
    /**
18
     * @var ReflectionMethod
19
     */
20
    protected $reflectionMethod;
21
    /**
22
     * @var string
23
     */
24
    protected $name;
25
    /**
26
     * @var Tag[]
27
     */
28
    protected $tags = [];
29
30
    public function __construct(ReflectionMethod $reflectionMethod)
31
    {
32
        $this->reflectionMethod = $reflectionMethod;
33
    }
34
35
    /**
36
     * Returns a list of services to be injected.
37
     *
38
     * @return Injection
39
     */
40
    protected function mapParameterToInjection(ReflectionParameter $reflectionParameter): Injection
41
    {
42
        $type = $reflectionParameter->getType();
43
        // No type? Let's inject by parameter name.
44
        if ($type === null || $type->isBuiltin()) {
45
            return new ServiceInjection($reflectionParameter->getName(), !$reflectionParameter->allowsNull());
46
        }
47
        if (((string)$type) === ContainerInterface::class) {
48
            return new ContainerInjection();
49
        }
50
        return new ServiceInjection((string)$type, !$reflectionParameter->allowsNull());
51
    }
52
53
    /**
54
     * @return ReflectionMethod
55
     */
56
    public function getReflectionMethod(): ReflectionMethod
57
    {
58
        return $this->reflectionMethod;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getName(): string
65
    {
66
        return $this->name;
67
    }
68
69
    /**
70
     * @return Tag[]
71
     */
72
    public function getTags(): array
73
    {
74
        return $this->tags;
75
    }
76
}
77