1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Parsing; |
4
|
|
|
|
5
|
|
|
use Pinq\Expressions as O; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Implementation of the function signature interface. |
9
|
|
|
* |
10
|
|
|
* @author Elliot Levin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class FunctionSignature extends MagicResolvable implements IFunctionSignature |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
protected $type; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var boolean |
21
|
|
|
*/ |
22
|
|
|
protected $returnsReference; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int|null |
26
|
|
|
*/ |
27
|
|
|
protected $accessModifier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int|null |
31
|
|
|
*/ |
32
|
|
|
protected $polymorphModifier; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool|null |
36
|
|
|
*/ |
37
|
|
|
protected $isStatic; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $name; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var O\ParameterExpression[] |
46
|
|
|
*/ |
47
|
|
|
protected $parameterExpressions; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string[]|null |
51
|
|
|
*/ |
52
|
|
|
protected $scopedVariableNames; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $hash; |
58
|
|
|
|
59
|
|
|
protected function __construct( |
60
|
|
|
$type, |
61
|
|
|
$returnsReference, |
62
|
|
|
$accessModifier, |
63
|
|
|
$polymorphModifier, |
64
|
|
|
$isStatic, |
65
|
|
|
$name, |
66
|
|
|
array $parameterExpressions, |
67
|
|
|
array $scopedVariableNames = null |
68
|
|
|
) { |
69
|
|
|
parent::__construct($parameterExpressions); |
70
|
|
|
|
71
|
|
|
$this->type = $type; |
72
|
|
|
$this->returnsReference = $returnsReference; |
73
|
|
|
$this->accessModifier = $accessModifier; |
74
|
|
|
$this->polymorphModifier = $polymorphModifier; |
75
|
|
|
$this->isStatic = $isStatic; |
76
|
|
|
$this->parameterExpressions = $parameterExpressions; |
77
|
|
|
$this->name = $name; |
78
|
|
|
$this->scopedVariableNames = $scopedVariableNames; |
79
|
|
|
|
80
|
|
|
$this->hash = implode( |
81
|
|
|
'-', |
82
|
|
|
[ |
83
|
|
|
$type, |
84
|
|
|
$returnsReference, |
85
|
|
|
$accessModifier, |
86
|
|
|
$polymorphModifier, |
87
|
|
|
$isStatic, |
88
|
|
|
$name, |
89
|
|
|
O\Expression::hashAll($parameterExpressions), |
90
|
|
|
$scopedVariableNames !== null ? implode('|', $scopedVariableNames) : '', |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function withResolvedMagic(array $resolvedExpressions) |
96
|
|
|
{ |
97
|
|
|
return new self( |
98
|
|
|
$this->type, |
99
|
|
|
$this->returnsReference, |
100
|
|
|
$this->accessModifier, |
101
|
|
|
$this->polymorphModifier, |
102
|
|
|
$this->isStatic, |
103
|
|
|
$this->name, |
104
|
|
|
O\Expression::simplifyAll($resolvedExpressions), |
105
|
|
|
$this->scopedVariableNames); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Creates a function signature instance from the supplied reflection. |
110
|
|
|
* |
111
|
|
|
* @param \ReflectionFunctionAbstract $reflection |
112
|
|
|
* |
113
|
|
|
* @return self |
114
|
|
|
*/ |
115
|
|
|
public static function fromReflection(\ReflectionFunctionAbstract $reflection) |
116
|
|
|
{ |
117
|
|
|
$returnsReference = $reflection->returnsReference(); |
118
|
|
|
$name = $reflection->getShortName(); |
119
|
|
|
$parameterExpressions = self::getParameterExpressionsFromReflection($reflection); |
120
|
|
|
|
121
|
|
|
if ($reflection->isClosure()) { |
122
|
|
|
return self::closure( |
123
|
|
|
$returnsReference, |
124
|
|
|
$parameterExpressions, |
125
|
|
|
array_keys($reflection->getStaticVariables()) |
126
|
|
|
); |
127
|
|
|
} elseif ($reflection instanceof \ReflectionMethod) { |
128
|
|
|
if ($reflection->isPublic()) { |
129
|
|
|
$accessModifier = self::ACCESS_PUBLIC; |
130
|
|
|
} elseif ($reflection->isProtected()) { |
131
|
|
|
$accessModifier = self::ACCESS_PROTECTED; |
132
|
|
|
} else { |
133
|
|
|
$accessModifier = self::ACCESS_PRIVATE; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($reflection->isAbstract()) { |
137
|
|
|
$polymorphModifier = self::POLYMORPH_ABSTRACT; |
138
|
|
|
} elseif ($reflection->isFinal()) { |
139
|
|
|
$polymorphModifier = self::POLYMORPH_FINAL; |
140
|
|
|
} else { |
141
|
|
|
$polymorphModifier = null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return self::method( |
145
|
|
|
$returnsReference, |
146
|
|
|
$accessModifier, |
147
|
|
|
$polymorphModifier, |
148
|
|
|
$reflection->isStatic(), |
149
|
|
|
$name, |
150
|
|
|
$parameterExpressions |
151
|
|
|
); |
152
|
|
|
} else { |
153
|
|
|
return self::func( |
154
|
|
|
$returnsReference, |
155
|
|
|
$name, |
156
|
|
|
$parameterExpressions |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Creates a function signature with the supplied parameters. |
163
|
|
|
* |
164
|
|
|
* @param boolean $returnsReference |
165
|
|
|
* @param string $name |
166
|
|
|
* @param O\ParameterExpression[] $parameterExpressions |
167
|
|
|
* |
168
|
|
|
* @return self |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public static function func( |
171
|
|
|
$returnsReference, |
172
|
|
|
$name, |
173
|
|
|
array $parameterExpressions |
174
|
|
|
) { |
175
|
|
|
return new self( |
176
|
|
|
self::TYPE_FUNCTION, |
177
|
|
|
$returnsReference, |
178
|
|
|
null, |
179
|
|
|
null, |
180
|
|
|
null, |
181
|
|
|
$name, |
182
|
|
|
$parameterExpressions, |
183
|
|
|
null); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Creates a closure signature with the supplied parameters. |
188
|
|
|
* |
189
|
|
|
* @param boolean $returnsReference |
190
|
|
|
* @param O\ParameterExpression[] $parameterExpressions |
191
|
|
|
* @param string[] $scopedVariableNames |
192
|
|
|
* |
193
|
|
|
* @return self |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
public static function closure( |
196
|
|
|
$returnsReference, |
197
|
|
|
array $parameterExpressions, |
198
|
|
|
array $scopedVariableNames |
199
|
|
|
) { |
200
|
|
|
return new self( |
201
|
|
|
self::TYPE_CLOSURE, |
202
|
|
|
$returnsReference, |
203
|
|
|
null, |
204
|
|
|
null, |
205
|
|
|
null, |
206
|
|
|
null, |
207
|
|
|
$parameterExpressions, |
208
|
|
|
$scopedVariableNames); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Creates a method signature with the supplied parameters. |
213
|
|
|
* |
214
|
|
|
* @param boolean $returnsReference |
215
|
|
|
* @param int|null $accessModifier |
216
|
|
|
* @param int|null $polymorphModifier |
217
|
|
|
* @param boolean $isStatic |
218
|
|
|
* @param string $name |
219
|
|
|
* @param O\ParameterExpression[] $parameterExpressions |
220
|
|
|
* |
221
|
|
|
* @return self |
222
|
|
|
*/ |
223
|
|
|
public static function method( |
224
|
|
|
$returnsReference, |
225
|
|
|
$accessModifier, |
226
|
|
|
$polymorphModifier, |
227
|
|
|
$isStatic, |
228
|
|
|
$name, |
229
|
|
|
array $parameterExpressions |
230
|
|
|
) { |
231
|
|
|
return new self( |
232
|
|
|
self::TYPE_METHOD, |
233
|
|
|
$returnsReference, |
234
|
|
|
$accessModifier, |
235
|
|
|
$polymorphModifier, |
236
|
|
|
$isStatic, |
237
|
|
|
$name, |
238
|
|
|
$parameterExpressions, |
239
|
|
|
null); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
protected static function getParameterExpressionsFromReflection(\ReflectionFunctionAbstract $reflection) |
243
|
|
|
{ |
244
|
|
|
$parameterExpressions = []; |
245
|
|
|
|
246
|
|
|
foreach ($reflection->getParameters() as $parameter) { |
247
|
|
|
$parameterExpressions[] = self::getParameterExpression($parameter); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $parameterExpressions; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
private static function getParameterExpression(\ReflectionParameter $parameter) |
254
|
|
|
{ |
255
|
|
|
$typeHint = null; |
256
|
|
|
|
257
|
|
|
if ($parameter->isArray()) { |
258
|
|
|
$typeHint = 'array'; |
259
|
|
|
} elseif ($parameter->isCallable()) { |
260
|
|
|
$typeHint = 'callable'; |
261
|
|
|
} elseif ($parameter->getClass() !== null) { |
262
|
|
|
$typeHint = $parameter->getClass()->getName(); |
|
|
|
|
263
|
|
|
$typeHint = $typeHint[0] === '\\' ? $typeHint : '\\' . $typeHint; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return O\Expression::parameter( |
267
|
|
|
$parameter->getName(), |
|
|
|
|
268
|
|
|
$typeHint, |
269
|
|
|
$parameter->isDefaultValueAvailable() ? O\Expression::value($parameter->getDefaultValue()) : null, |
270
|
|
|
$parameter->isPassedByReference(), |
271
|
|
|
method_exists($parameter, 'isVariadic') && $parameter->isVariadic() |
272
|
|
|
); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function getType() |
276
|
|
|
{ |
277
|
|
|
return $this->type; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function returnsReference() |
281
|
|
|
{ |
282
|
|
|
return $this->returnsReference; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function getAccessModifier() |
286
|
|
|
{ |
287
|
|
|
return $this->accessModifier; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function getPolymorphModifier() |
291
|
|
|
{ |
292
|
|
|
return $this->polymorphModifier; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function isStatic() |
296
|
|
|
{ |
297
|
|
|
return $this->isStatic; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function getName() |
301
|
|
|
{ |
302
|
|
|
return $this->name; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function getParameterExpressions() |
306
|
|
|
{ |
307
|
|
|
return $this->parameterExpressions; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function getScopedVariableNames() |
311
|
|
|
{ |
312
|
|
|
return $this->scopedVariableNames; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function getHash() |
316
|
|
|
{ |
317
|
|
|
return $this->hash; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|