Completed
Push — master ( 5b7dfb...3e297a )
by David
12s
created

AbstractDefinition::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Injections\ContainerInjection;
11
use TheCodingMachine\Funky\Injections\Injection;
12
use TheCodingMachine\Funky\Injections\ServiceInjection;
13
14
abstract class AbstractDefinition
15
{
16
    /**
17
     * @var ReflectionMethod
18
     */
19
    protected $reflectionMethod;
20
    /**
21
     * @var string
22
     */
23
    protected $name;
24
25
    public function __construct(ReflectionMethod $reflectionMethod)
26
    {
27
        $this->reflectionMethod = $reflectionMethod;
28
    }
29
30
    /**
31
     * Returns a list of services to be injected.
32
     *
33
     * @return Injection
34
     */
35
    protected function mapParameterToInjection(ReflectionParameter $reflectionParameter): Injection
36
    {
37
        $type = $reflectionParameter->getType();
38
        // No type? Let's inject by parameter name.
39
        if ($type === null || $type->isBuiltin()) {
40
            return new ServiceInjection($reflectionParameter->getName(), !$reflectionParameter->allowsNull());
41
        }
42
        if (((string)$type) === ContainerInterface::class) {
43
            return new ContainerInjection();
44
        }
45
        return new ServiceInjection((string)$type, !$reflectionParameter->allowsNull());
46
    }
47
48
    /**
49
     * @return ReflectionMethod
50
     */
51
    public function getReflectionMethod(): ReflectionMethod
52
    {
53
        return $this->reflectionMethod;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getName(): string
60
    {
61
        return $this->name;
62
    }
63
}
64