Completed
Push — master ( 95a6e8...b8af6d )
by Filipe
07:12
created

MethodInspector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 98
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefinition() 0 5 1
A getMetaData() 0 14 3
A getArguments() 0 11 3
A getParameter() 0 15 2
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\DependencyInspector;
11
12
use ReflectionParameter;
13
use Slick\Common\Base;
14
use Slick\Common\Inspector;
15
use Slick\Di\Definition\Object as ObjectDefinition;
16
17
/**
18
 * Method Inspector for dependency definition
19
 *
20
 * @package Slick\Di\DependencyInspector
21
 * @author  Filipe Silva <[email protected]>
22
 *
23
 * @property string      $name Method name
24
 * @property
25
 * @property Parameter[] $arguments Method argument list
26
 */
27
class MethodInspector extends Base
28
{
29
30
    /**
31
     * @readwrite
32
     * @var ObjectDefinition
33
     */
34
    protected $definition;
35
36
    /**
37
     * @readwrite
38
     * @var string
39
     */
40
    protected $name;
41
42
    /**
43
     * @var ReflectionParameter[]
44
     */
45
    protected $metaData;
46
47
    /**
48
     * @read
49
     * @var Parameter[]
50
     */
51
    protected $arguments;
52
53
    /**
54
     * Set object definition
55
     *
56
     * @param ObjectDefinition $definition
57
     * @return $this|self|MethodInspector
58
     */
59 30
    public function setDefinition(ObjectDefinition $definition)
60
    {
61 30
        $this->definition = $definition;
62 30
        return $this;
63
    }
64
65
    /**
66
     * Gets constructor reflection object
67
     *
68
     * @return ReflectionParameter[]
69
     */
70 30
    protected function getMetaData()
71
    {
72 30
        if (is_null($this->metaData)) {
73 30
            $this->metaData = [];
74 30
            $inspector = Inspector::forClass($this->definition->className);
75 30
            if ($inspector->hasMethod($this->name)) {
76 28
                $this->metaData = $inspector
77 28
                    ->getReflection()
78 28
                    ->getMethod($this->name)
79 28
                    ->getParameters();
80 28
            }
81 30
        }
82 30
        return $this->metaData;
83
    }
84
85
    /**
86
     * Gets method arguments
87
     *
88
     * @return Parameter[]
89
     */
90 30
    public function getArguments()
91
    {
92 30
        if (is_null($this->arguments)) {
93 30
            $this->arguments = [];
94 30
            $parameters = $this->getMetaData();
95 30
            foreach ($parameters as $param) {
96 28
                $this->arguments[] = $this->getParameter($param);
97 30
            }
98 30
        }
99 30
        return $this->arguments;
100
    }
101
102
    /**
103
     * Creates a Parameter object from provided ReflectionParameter object
104
     *
105
     * @param \ReflectionParameter $param
106
     *
107
     * @return Parameter
108
     */
109 28
    private function getParameter(\ReflectionParameter $param)
110
    {
111 28
        $defaultValue = null;
112 28
        if ($isOptional = $param->isOptional()) {
113 28
            $defaultValue = $param->getDefaultValue();
114 28
        }
115 28
        return new Parameter(
116
            [
117 28
                'name' => $param->getName(),
118 28
                'id' => $param->getClass(),
119 28
                'default' => $defaultValue,
120
                'optional' => $isOptional
121 28
            ]
122 28
        );
123
    }
124
}