|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the ApiGen (http://apigen.org) |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the file LICENSE that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace ApiGen\Parser\Reflection; |
|
11
|
|
|
|
|
12
|
|
|
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface; |
|
13
|
|
|
use ApiGen\Contracts\Parser\Reflection\Magic\MagicParameterReflectionInterface; |
|
14
|
|
|
use TokenReflection; |
|
15
|
|
|
|
|
16
|
|
|
class ReflectionParameterMagic extends ReflectionParameter implements MagicParameterReflectionInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $name; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $typeHint; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $position; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var bool |
|
36
|
|
|
*/ |
|
37
|
|
|
private $defaultValueDefinition; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool |
|
41
|
|
|
*/ |
|
42
|
|
|
private $unlimited; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var bool |
|
46
|
|
|
*/ |
|
47
|
|
|
private $passedByReference; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var ReflectionMethodMagic |
|
51
|
|
|
*/ |
|
52
|
|
|
private $declaringFunction; |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
public function __construct(array $settings) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->name = $settings['name']; |
|
58
|
|
|
$this->position = $settings['position']; |
|
59
|
|
|
$this->typeHint = $settings['typeHint']; |
|
60
|
|
|
$this->defaultValueDefinition = $settings['defaultValueDefinition']; |
|
61
|
|
|
$this->unlimited = $settings['unlimited']; |
|
62
|
|
|
$this->passedByReference = $settings['passedByReference']; |
|
63
|
|
|
$this->declaringFunction = $settings['declaringFunction']; |
|
64
|
|
|
|
|
65
|
|
|
$this->reflectionType = get_class($this); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getName() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->name; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getTypeHint() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->typeHint; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getFileName() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->declaringFunction->getFileName(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function isTokenized() |
|
100
|
|
|
{ |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* {@inheritdoc} |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getPrettyName() |
|
109
|
|
|
{ |
|
110
|
|
|
return str_replace('()', '($' . $this->name . ')', $this->declaringFunction->getPrettyName()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getDeclaringClass() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->declaringFunction->getDeclaringClass(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getDeclaringClassName() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->declaringFunction->getDeclaringClassName(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getDeclaringFunction() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->declaringFunction; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getDeclaringFunctionName() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->declaringFunction->getName(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* {@inheritdoc} |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getStartLine() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->declaringFunction->getStartLine(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritdoc} |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getEndLine() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->declaringFunction->getEndLine(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* {@inheritdoc} |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getDocComment() |
|
172
|
|
|
{ |
|
173
|
|
|
return ''; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* {@inheritdoc} |
|
179
|
|
|
*/ |
|
180
|
|
|
public function isDefaultValueAvailable() |
|
181
|
|
|
{ |
|
182
|
|
|
return (bool) $this->defaultValueDefinition; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* {@inheritdoc} |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getDefaultValueDefinition() |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->defaultValueDefinition; |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* {@inheritdoc} |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getPosition() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->position; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* {@inheritdoc} |
|
205
|
|
|
*/ |
|
206
|
|
|
public function isArray() |
|
207
|
|
|
{ |
|
208
|
|
|
return TokenReflection\ReflectionParameter::ARRAY_TYPE_HINT === $this->typeHint; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* {@inheritdoc} |
|
214
|
|
|
*/ |
|
215
|
|
|
public function isCallable() |
|
216
|
|
|
{ |
|
217
|
|
|
return TokenReflection\ReflectionParameter::CALLABLE_TYPE_HINT === $this->typeHint; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* {@inheritdoc} |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getClass() |
|
225
|
|
|
{ |
|
226
|
|
|
$className = $this->getClassName(); |
|
227
|
|
|
return $className === null ? null : $this->getParsedClasses()[$className]; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* {@inheritdoc} |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getClassName() |
|
235
|
|
|
{ |
|
236
|
|
|
if ($this->isArray() || $this->isCallable()) { |
|
237
|
|
|
return null; |
|
238
|
|
|
} |
|
239
|
|
|
if (isset($this->getParsedClasses()[$this->typeHint])) { |
|
240
|
|
|
return $this->typeHint; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return null; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* {@inheritdoc} |
|
249
|
|
|
*/ |
|
250
|
|
|
public function allowsNull() |
|
251
|
|
|
{ |
|
252
|
|
|
if ($this->isArray() || $this->isCallable()) { |
|
253
|
|
|
return strtolower($this->defaultValueDefinition) === 'null'; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
return ! empty($this->defaultValueDefinition); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* {@inheritdoc} |
|
262
|
|
|
*/ |
|
263
|
|
|
public function isOptional() |
|
264
|
|
|
{ |
|
265
|
|
|
return $this->isDefaultValueAvailable(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* {@inheritdoc} |
|
271
|
|
|
*/ |
|
272
|
|
|
public function isPassedByReference() |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->passedByReference; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* {@inheritdoc} |
|
280
|
|
|
*/ |
|
281
|
|
|
public function canBePassedByValue() |
|
282
|
|
|
{ |
|
283
|
|
|
return false; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* {@inheritdoc} |
|
289
|
|
|
*/ |
|
290
|
|
|
public function isUnlimited() |
|
291
|
|
|
{ |
|
292
|
|
|
return $this->unlimited; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.