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