1 | <?php |
||
9 | final class Argument |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | private $index; |
||
15 | |||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | private $isDefaultAvailable; |
||
20 | |||
21 | /** |
||
22 | * @var |
||
23 | */ |
||
24 | private $default; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $meta; |
||
30 | |||
31 | /** |
||
32 | * @var \ReflectionParameter |
||
33 | */ |
||
34 | private $reflection; |
||
35 | |||
36 | 64 | public function __construct(\ReflectionParameter $parameter, $name) |
|
37 | { |
||
38 | 64 | $interface = $this->getTypeHint($parameter); |
|
39 | 64 | $interface = ($interface === 'array') ? '' : $interface; // hhvm |
|
40 | 64 | $isOptional = $parameter->isOptional(); |
|
41 | 64 | $this->isDefaultAvailable = $parameter->isDefaultValueAvailable() || $isOptional; |
|
42 | 64 | if ($isOptional) { |
|
43 | 12 | $this->default = null; |
|
44 | } |
||
45 | 64 | $this->setDefaultValue($parameter); |
|
46 | 64 | $this->index = $interface . '-' . $name; |
|
47 | 64 | $this->reflection = $parameter; |
|
48 | 64 | $this->meta = sprintf( |
|
49 | 64 | "dependency '%s' with name '%s' used in %s:%d ($%s)", |
|
50 | $interface, |
||
51 | $name, |
||
52 | 64 | $this->reflection->getDeclaringFunction()->getFileName(), |
|
53 | 64 | $this->reflection->getDeclaringFunction()->getStartLine(), |
|
54 | 64 | $parameter->getName() |
|
|
|||
55 | ); |
||
56 | 64 | } |
|
57 | |||
58 | /** |
||
59 | * @return string |
||
60 | */ |
||
61 | 38 | public function __toString() |
|
62 | { |
||
63 | 38 | return $this->index; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Return reflection |
||
68 | * |
||
69 | * @return \ReflectionParameter |
||
70 | */ |
||
71 | 36 | public function get() |
|
72 | { |
||
73 | 36 | return $this->reflection; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return bool |
||
78 | */ |
||
79 | 13 | public function isDefaultAvailable() |
|
80 | { |
||
81 | 13 | return $this->isDefaultAvailable; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return mixed |
||
86 | */ |
||
87 | 7 | public function getDefaultValue() |
|
88 | { |
||
89 | 7 | return $this->default; |
|
90 | } |
||
91 | |||
92 | 7 | public function getMeta() |
|
93 | { |
||
94 | 7 | return $this->meta; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param \ReflectionParameter $parameter |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 64 | private function getTypeHint(\ReflectionParameter $parameter) |
|
103 | { |
||
104 | 64 | if (defined('HHVM_VERSION')) { |
|
105 | /* @noinspection PhpUndefinedFieldInspection */ |
||
106 | return $parameter->info['type_hint']; // @codeCoverageIgnore |
||
107 | } |
||
108 | 64 | $typHint = $parameter->getClass(); |
|
109 | |||
110 | 64 | return $typHint instanceof \ReflectionClass ? $typHint->name : ''; |
|
111 | } |
||
112 | |||
113 | 64 | private function setDefaultValue(\ReflectionParameter $parameter) |
|
125 | } |
||
126 |