|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Benoth\StaticReflection\Reflection; |
|
4
|
|
|
|
|
5
|
|
|
class ReflectionParameter extends Reflection |
|
6
|
|
|
{ |
|
7
|
|
|
protected $declaringFunction; |
|
8
|
|
|
protected $type; |
|
9
|
|
|
protected $byRef; |
|
10
|
|
|
protected $required = true; |
|
11
|
|
|
protected $variadic = false; |
|
12
|
|
|
protected $defaultType = 'null'; |
|
13
|
|
|
protected $defaultValue = null; |
|
14
|
|
|
protected $constantName = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Gets the declaring function or method. |
|
18
|
|
|
* |
|
19
|
|
|
* @return ReflectionFunction |
|
20
|
|
|
*/ |
|
21
|
3 |
|
public function getDeclaringFunction() |
|
22
|
|
|
{ |
|
23
|
3 |
|
return $this->declaringFunction; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Gets the declaring class for a method. |
|
28
|
|
|
* |
|
29
|
|
|
* @return ReflectionClass|ReflectionTrait|ReflectionInterface|null Always null for a function |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function getDeclaringClass() |
|
32
|
|
|
{ |
|
33
|
3 |
|
if ($this->declaringFunction instanceof ReflectionFunction) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
return $this->declaringFunction->getDeclaringClass(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
public function getFileName() |
|
41
|
|
|
{ |
|
42
|
3 |
|
return $this->declaringFunction->getFileName(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Checks if the parameter is passed in by reference. |
|
47
|
|
|
* |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
3 |
|
public function isByRef() |
|
51
|
|
|
{ |
|
52
|
3 |
|
return $this->byRef; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Checks if the parameter is passed in by reference. |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function isPassedByReference() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->isByRef(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Checks if the parameter can be passed by value. |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function canBePassedByValue() |
|
71
|
|
|
{ |
|
72
|
|
|
return !$this->isByRef(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Checks if the parameter is required. |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
15 |
|
public function isRequired() |
|
81
|
|
|
{ |
|
82
|
15 |
|
return $this->required; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Checks if the parameter is declared as a variadic parameter. |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function isVariadic() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->variadic; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Checks if the parameter is optional. |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
public function isOptional() |
|
101
|
|
|
{ |
|
102
|
|
|
return !$this->required; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Gets the associated type of a parameter. |
|
107
|
|
|
* |
|
108
|
|
|
* @return ReflectionType ReflectionType if a parameter type is specified, null otherwise. |
|
109
|
|
|
*/ |
|
110
|
3 |
|
public function getType() |
|
111
|
|
|
{ |
|
112
|
|
|
// @todo Handle ReflectionType ? |
|
113
|
3 |
|
return $this->type; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Gets the class type hinted for the parameter as a ReflectionClass object. |
|
118
|
|
|
* |
|
119
|
|
|
* @return ReflectionClass|null |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getClass() |
|
122
|
|
|
{ |
|
123
|
|
|
if ($this->declaringFunction instanceof ReflectionFunction) { |
|
124
|
|
|
$source = $this->getDeclaringFunction(); |
|
125
|
|
|
} else { |
|
126
|
|
|
$source = $this->getDeclaringClass(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $source->getIndex()->getClass($this->getType()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Checks if the parameter has a type associated with it. |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
public function hasType() |
|
138
|
|
|
{ |
|
139
|
|
|
// @todo Handle ReflectionType ? |
|
140
|
|
|
return $this->getType() !== null; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Checks if the parameter expects an array. |
|
145
|
|
|
* |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function isArray() |
|
149
|
|
|
{ |
|
150
|
|
|
// @todo Handle ReflectionType ? |
|
151
|
|
|
return $this->getType() == 'array'; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Checks if the parameter expects a callable. |
|
156
|
|
|
* |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function isCallable() |
|
160
|
|
|
{ |
|
161
|
|
|
// @todo Handle ReflectionType ? |
|
162
|
|
|
return $this->getType() == 'callable'; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Checks whether the parameter allows NULL. |
|
167
|
|
|
* |
|
168
|
|
|
* @return bool |
|
169
|
|
|
*/ |
|
170
|
|
|
public function allowsNull() |
|
171
|
|
|
{ |
|
172
|
|
|
if ($this->getType() === null) { |
|
173
|
|
|
return true; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if ($this->isOptional()) { |
|
177
|
|
|
return true; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $this->defaultValue === null; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Checks if a default value for the parameter is available. |
|
185
|
|
|
* |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
public function isDefaultValueAvailable() |
|
189
|
|
|
{ |
|
190
|
|
|
return !$this->isRequired(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
3 |
|
public function getDefault() |
|
194
|
|
|
{ |
|
195
|
3 |
|
return [$this->defaultType, $this->defaultValue]; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
3 |
|
public function getDefaultType() |
|
199
|
|
|
{ |
|
200
|
3 |
|
return $this->defaultType; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Gets default parameter value. |
|
205
|
|
|
* |
|
206
|
|
|
* @throws \ReflectionException If the parameter is required |
|
207
|
|
|
* |
|
208
|
|
|
* @return mixed |
|
209
|
|
|
*/ |
|
210
|
12 |
|
public function getDefaultValue() |
|
211
|
|
|
{ |
|
212
|
12 |
|
if ($this->isRequired()) { |
|
213
|
9 |
|
throw new \ReflectionException('Parameter '.$this->getName().' is required and does not have a default value'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
3 |
|
return $this->defaultValue; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Returns the default value's constant name if default value is constant or null. |
|
221
|
|
|
* |
|
222
|
|
|
* @return string|null |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getDefaultValueConstantName() |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->constantName; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Returns whether the default value of this parameter is a named constant. |
|
231
|
|
|
* |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
public function isDefaultValueConstant() |
|
235
|
|
|
{ |
|
236
|
|
|
return !is_null($this->constantName); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Gets the position of the parameter. |
|
241
|
|
|
* |
|
242
|
|
|
* The position of the parameter, left to right, starting at position #0. |
|
243
|
|
|
* |
|
244
|
|
|
* @return int |
|
245
|
|
|
*/ |
|
246
|
|
|
public function getPosition() |
|
247
|
|
|
{ |
|
248
|
|
|
foreach ($this->getDeclaringFunction()->getParameters() as $position => $parameter) { |
|
249
|
|
|
if ($this === $parameter) { |
|
250
|
|
|
return $position; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
963 |
|
public function setDeclaringFunction(ReflectionFunctionAbstract $declaringFunction) |
|
256
|
|
|
{ |
|
257
|
963 |
|
$this->declaringFunction = $declaringFunction; |
|
258
|
|
|
|
|
259
|
963 |
|
return $this; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
963 |
|
public function setType($type) |
|
263
|
|
|
{ |
|
264
|
963 |
|
$this->type = $type; |
|
265
|
|
|
|
|
266
|
963 |
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
963 |
|
public function setByRef($byRef) |
|
270
|
|
|
{ |
|
271
|
963 |
|
$this->byRef = (bool) $byRef; |
|
272
|
|
|
|
|
273
|
963 |
|
return $this; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
774 |
|
public function setRequired($required) |
|
277
|
|
|
{ |
|
278
|
774 |
|
$this->required = (bool) $required; |
|
279
|
|
|
|
|
280
|
774 |
|
return $this; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
963 |
|
public function setVariadic($variadic) |
|
284
|
|
|
{ |
|
285
|
963 |
|
$this->variadic = (bool) $variadic; |
|
286
|
|
|
|
|
287
|
963 |
|
return $this; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
774 |
|
public function setDefault($type, $value) |
|
291
|
|
|
{ |
|
292
|
774 |
|
$this->defaultType = $type; |
|
293
|
774 |
|
$this->defaultValue = $value; |
|
294
|
|
|
|
|
295
|
774 |
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
174 |
|
public function setDefaultValueConstantName($constantName) |
|
299
|
|
|
{ |
|
300
|
174 |
|
$this->constantName = $constantName; |
|
301
|
|
|
|
|
302
|
174 |
|
return $this; |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|