Completed
Push — develop ( f0edc8...54f05a )
by Filipe
22s queued 20s
created

ConstructorArgumentInspector::arguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 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
use ReflectionClass;
13
14
/**
15
 * ConstructorArgumentInspector
16
 *
17
 * @package Slick\Di\Inspector
18
 * @author  Filipe Silva <[email protected]>
19
*/
20
class ConstructorArgumentInspector
21
{
22
    /**
23
     * @var ReflectionClass
24
     */
25
    private $reflectionClass;
26
27
    /**
28
     * @var array
29
     */
30
    private $override;
31
32
    /**
33
     * Creates a ConstructorArgumentInspector
34
     *
35
     * @param ReflectionClass $reflectionClass
36
     * @param array $override
37
     */
38
    public function __construct(ReflectionClass $reflectionClass, array $override = [])
39
    {
40
        $this->reflectionClass = $reflectionClass;
41
        $this->override = $override;
42
    }
43
44
    /**
45
     * Returns the list of alias to used as arguments on object definition
46
     *
47
     * @return array
48
     */
49
    public function arguments()
50
    {
51
        $arguments = $this->definedArguments();
52
        return array_replace($arguments, $this->override);
53
    }
54
55
    /**
56
     * Get the list of arguments from constructor defined parameters
57
     *
58
     * @return string[]
59
     */
60
    private function definedArguments()
61
    {
62
        $arguments = [];
63
        $constructor = $this->reflectionClass->getConstructor();
64
65
        if (null === $constructor) {
66
            return $arguments;
67
        }
68
69
        $parameters = $constructor->getParameters();
70
71
        foreach ($parameters as $parameter) {
72
            $class = $parameter->getType();
73
            if (is_null($class)) {
74
                break;
75
            }
76
77
            $arguments[] = "@{$parameter->getType()->getName()}";
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionNamedType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $arguments[] = "@{$parameter->getType()->/** @scrutinizer ignore-call */ getName()}";
Loading history...
78
        }
79
        return $arguments;
80
    }
81
82
}
83