1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
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 Hal\Component\OOP\Reflected; |
11
|
|
|
use Hal\Component\OOP\Reflected\ReflectedClass\ReflectedAnonymousClass; |
12
|
|
|
use Hal\Component\OOP\Resolver\NameResolver; |
13
|
|
|
use Hal\Component\OOP\Resolver\TypeResolver; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Result (method) |
18
|
|
|
* |
19
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
20
|
|
|
*/ |
21
|
|
|
class ReflectedMethod { |
22
|
|
|
|
23
|
|
|
CONST VISIBILITY_PUBLIC = 'public'; |
24
|
|
|
CONST VISIBILITY_PRIVATE = 'private'; |
25
|
|
|
CONST VISIBILITY_PROTECTED = 'protected'; |
26
|
|
|
CONST STATE_LOCAL = 1; |
27
|
|
|
CONST STATE_STATIC = 2; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $name; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $arguments = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $returns = array(); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $dependencies = array(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Resolver for names |
52
|
|
|
* |
53
|
|
|
* @var NameResolver |
54
|
|
|
*/ |
55
|
|
|
private $nameResolver; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private $tokens = array(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private $content; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Usage of method (getter, setter...) |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $usage; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var int |
76
|
|
|
*/ |
77
|
|
|
private $visibility = self::VISIBILITY_PUBLIC; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var int |
81
|
|
|
*/ |
82
|
|
|
private $state = self::STATE_LOCAL; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Anonymous class contained in this method |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
private $anonymousClasses = array(); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
private $instanciedClasses = array(); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var string |
98
|
|
|
*/ |
99
|
|
|
private $namespace; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var array |
103
|
|
|
*/ |
104
|
|
|
private $internalCalls = array(); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var array |
108
|
|
|
*/ |
109
|
|
|
private $externalCalls = array(); |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $name |
113
|
|
|
*/ |
114
|
|
|
public function __construct($name) |
115
|
|
|
{ |
116
|
|
|
$this->name = (string) $name; |
117
|
|
|
$this->nameResolver = new NameResolver(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getName() |
124
|
|
|
{ |
125
|
|
|
return $this->name; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return \SplObjectStorage |
130
|
|
|
*/ |
131
|
|
|
public function getArguments() |
132
|
|
|
{ |
133
|
|
|
return $this->arguments; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Attach argument |
138
|
|
|
* |
139
|
|
|
* @param ReflectedArgument $arg |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function pushArgument(ReflectedArgument $arg) { |
143
|
|
|
array_push($this->arguments, $arg); |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getContent() |
151
|
|
|
{ |
152
|
|
|
return $this->content; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $content |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
|
|
public function setContent($content) |
160
|
|
|
{ |
161
|
|
|
$this->content = $content; |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function getTokens() |
169
|
|
|
{ |
170
|
|
|
return $this->tokens; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param \Hal\Component\Token\TokenCollection $tokens |
175
|
|
|
* @return self |
176
|
|
|
*/ |
177
|
|
|
public function setTokens($tokens) |
178
|
|
|
{ |
179
|
|
|
$this->tokens = $tokens; |
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get returned value |
185
|
|
|
* |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function getReturns() { |
189
|
|
|
// on read : compare with aliases. We cannot make it in pushDependency() => aliases aren't yet known |
190
|
|
|
$result = array(); |
191
|
|
|
$resolver = new TypeResolver(); |
192
|
|
|
foreach($this->returns as $return) { |
193
|
|
|
$type = $this->nameResolver->resolve($return->getType(), null); |
194
|
|
|
|
195
|
|
|
if("\\" !== $type[0] &&!$resolver->isNative($type)) { |
196
|
|
|
$type = $this->namespace.'\\'.$type; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$return->setType($type); |
200
|
|
|
$result[] = $return; |
201
|
|
|
} |
202
|
|
|
return array_values($result); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Attach ne return information |
207
|
|
|
* |
208
|
|
|
* It make no sense for the moment to store any information abour return value / type. Maybe in PHP 6 ? :) |
209
|
|
|
* |
210
|
|
|
* @param ReflectedReturn $return |
211
|
|
|
* @return $this |
212
|
|
|
*/ |
213
|
|
|
public function pushReturn(ReflectedReturn $return) { |
214
|
|
|
array_push($this->returns, $return); |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Push dependency |
220
|
|
|
* |
221
|
|
|
* @param $name |
222
|
|
|
* @return self |
223
|
|
|
*/ |
224
|
|
|
public function pushDependency($name) { |
225
|
|
|
array_push($this->dependencies, $name); |
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
public function getDependencies() |
233
|
|
|
{ |
234
|
|
|
// on read : compare with aliases. We cannot make it in pushDependency() => aliases aren't yet known |
235
|
|
|
$dependencies = array(); |
236
|
|
|
foreach($this->dependencies as $name) { |
237
|
|
|
array_push($dependencies, $this->nameResolver->resolve($name, null)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// returned values |
241
|
|
|
$resolver = new TypeResolver(); |
242
|
|
|
foreach($this->returns as $return) { |
243
|
|
|
$name = $return->getType(); |
244
|
|
|
if(!$resolver->isNative($name)) { |
245
|
|
|
array_push($dependencies, $this->nameResolver->resolve($name, null)); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// anonymous classes in method (inner class) |
250
|
|
|
foreach($this->anonymousClasses as $c) { |
251
|
|
|
array_push($dependencies, $c->getParent()); |
252
|
|
|
$dependencies = array_merge($dependencies, $c->getDependencies()); |
253
|
|
|
} |
254
|
|
|
return array_unique($dependencies); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param NameResolver $resolver |
259
|
|
|
* @return self |
260
|
|
|
*/ |
261
|
|
|
public function setNameResolver(NameResolver $resolver) |
262
|
|
|
{ |
263
|
|
|
$this->nameResolver = $resolver; |
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return bool |
269
|
|
|
*/ |
270
|
|
|
public function isSetter() { |
271
|
|
|
return MethodUsage::USAGE_SETTER == $this->getUsage(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public function getUsage() |
278
|
|
|
{ |
279
|
|
|
return $this->usage; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param string $usage |
284
|
|
|
*/ |
285
|
|
|
public function setUsage($usage) |
286
|
|
|
{ |
287
|
|
|
$this->usage = $usage; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return bool |
292
|
|
|
*/ |
293
|
|
|
public function isGetter() { |
294
|
|
|
return MethodUsage::USAGE_GETTER == $this->getUsage(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return int |
299
|
|
|
*/ |
300
|
|
|
public function getVisibility() |
301
|
|
|
{ |
302
|
|
|
return $this->visibility; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param int $visibility |
307
|
|
|
* @return $this |
308
|
|
|
*/ |
309
|
|
|
public function setVisibility($visibility) |
310
|
|
|
{ |
311
|
|
|
$this->visibility = $visibility; |
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return int |
317
|
|
|
*/ |
318
|
|
|
public function getState() |
319
|
|
|
{ |
320
|
|
|
return $this->state; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param int $state |
325
|
|
|
* @return ReflectedMethod |
326
|
|
|
*/ |
327
|
|
|
public function setState($state) |
328
|
|
|
{ |
329
|
|
|
$this->state = $state; |
330
|
|
|
return $this; |
331
|
|
|
} |
332
|
|
|
/** |
333
|
|
|
* @param ReflectedAnonymousClass $class |
334
|
|
|
* @return $this |
335
|
|
|
*/ |
336
|
|
|
public function pushAnonymousClass(ReflectedAnonymousClass $class) { |
337
|
|
|
$this->anonymousClasses[] = $class; |
338
|
|
|
return $this; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return array |
343
|
|
|
*/ |
344
|
|
|
public function getAnonymousClasses() |
345
|
|
|
{ |
346
|
|
|
return $this->anonymousClasses; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param string $namespace |
351
|
|
|
* @return ReflectedMethod |
352
|
|
|
*/ |
353
|
|
|
public function setNamespace($namespace) |
354
|
|
|
{ |
355
|
|
|
$this->namespace = $namespace; |
356
|
|
|
return $this; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @return string |
361
|
|
|
*/ |
362
|
|
|
public function getNamespace() |
363
|
|
|
{ |
364
|
|
|
return $this->namespace; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param $class |
369
|
|
|
* @return $this |
370
|
|
|
*/ |
371
|
|
|
public function pushInstanciedClass($class) { |
372
|
|
|
$this->instanciedClasses[] = $class; |
373
|
|
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @return array |
378
|
|
|
*/ |
379
|
|
|
public function getInstanciedClasses() |
380
|
|
|
{ |
381
|
|
|
$classes = array(); |
382
|
|
|
foreach($this->instanciedClasses as $name) { |
383
|
|
|
array_push($classes, $this->nameResolver->resolve($name, null)); |
384
|
|
|
} |
385
|
|
|
return $classes; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param $call |
390
|
|
|
* @return $this |
391
|
|
|
*/ |
392
|
|
|
public function pushInternalCall($call) |
393
|
|
|
{ |
394
|
|
|
$this->internalCalls[] = $call; |
395
|
|
|
return $this; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return array |
400
|
|
|
*/ |
401
|
|
|
public function getInternalCalls() |
402
|
|
|
{ |
403
|
|
|
return $this->internalCalls; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @param $call |
408
|
|
|
* @return $this |
409
|
|
|
*/ |
410
|
|
|
public function pushExternalCall($call) |
411
|
|
|
{ |
412
|
|
|
$this->externalCalls[] = $call; |
413
|
|
|
return $this; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @return array |
418
|
|
|
*/ |
419
|
|
|
public function getExternalCalls() |
420
|
|
|
{ |
421
|
|
|
return $this->externalCalls; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
|
425
|
|
|
|
426
|
|
|
}; |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..