Completed
Push — master ( 384e0d...8d1b9d )
by Filipe
02:36
created

ConstructorArgumentInspector::definedArguments()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 0
1
<?php
2
3
/**
4
 * This file is part of slick/di package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Di\Inspector;
11
12
/**
13
 * ConstructorArgumentInspector
14
 *
15
 * @package Slick\Di\Inspector
16
 * @author  Filipe Silva <[email protected]>
17
*/
18
class ConstructorArgumentInspector
19
{
20
    /**
21
     * @var \ReflectionClass
22
     */
23
    private $reflectionClass;
24
25
    /**
26
     * @var array
27
     */
28
    private $override;
29
30
    /**
31
     * Creates a ConstructorArgumentInspector
32
     *
33
     * @param \ReflectionClass $reflectionClass
34
     * @param array $override
35
     */
36
    public function __construct(\ReflectionClass $reflectionClass, array $override = [])
37
    {
38
        $this->reflectionClass = $reflectionClass;
39
        $this->override = $override;
40
    }
41
42
    /**
43
     * Returns the list of alias to used as arguments on object definition
44
     *
45
     * @return array
46
     */
47
    public function arguments()
48
    {
49
        $arguments = $this->definedArguments();
50
        return array_replace($arguments, $this->override);
51
    }
52
53
    /**
54
     * Get the list of arguments from constructor defined parameters
55
     *
56
     * @return string[]
57
     */
58
    private function definedArguments()
59
    {
60
        $arguments = [];
61
        $constructor = $this->reflectionClass->getConstructor();
62
63
        if (null === $constructor) {
64
            return $arguments;
65
        }
66
67
        $parameters = $constructor->getParameters();
68
69
        foreach ($parameters as $parameter) {
70
            $class = $parameter->getClass();
71
            if (is_null($class)) {
72
                break;
73
            }
74
75
            $arguments[] = "@{$parameter->getClass()->getName()}";
76
        }
77
        return $arguments;
78
    }
79
80
}
81