1
|
|
|
<?php |
2
|
|
|
namespace TRex\Reflection; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* TypeReflection reflect the types. |
7
|
|
|
* |
8
|
|
|
* @package TRex\Reflection |
9
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
10
|
|
|
* @transient |
11
|
|
|
*/ |
12
|
|
|
class TypeReflection |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
const VOID_TYPE = 'void'; |
16
|
|
|
const MIXED_TYPE = 'mixed'; |
17
|
|
|
const NULL_TYPE = 'null'; |
18
|
|
|
const BOOLEAN_TYPE = 'boolean'; |
19
|
|
|
const STRING_TYPE = 'string'; |
20
|
|
|
const INTEGER_TYPE = 'integer'; |
21
|
|
|
const FLOAT_TYPE = 'float'; |
22
|
|
|
const NUMBER_TYPE = 'number'; |
23
|
|
|
const SCALAR_TYPE = 'scalar'; |
24
|
|
|
const ARRAY_TYPE = 'array'; |
25
|
|
|
const OBJECT_TYPE = 'object'; |
26
|
|
|
const RESOURCE_TYPE = 'resource'; |
27
|
|
|
const CALLABLE_TYPE = 'callable'; |
28
|
|
|
const UNKNOWN_TYPE = 'unknown type'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $typeMappingList = array( |
34
|
|
|
self::VOID_TYPE => array( |
35
|
|
|
'void', |
36
|
|
|
), |
37
|
|
|
self::MIXED_TYPE => array( |
38
|
|
|
'mixed', |
39
|
|
|
), |
40
|
|
|
self::NULL_TYPE => array( |
41
|
|
|
'null', |
42
|
|
|
), |
43
|
|
|
self::BOOLEAN_TYPE => array( |
44
|
|
|
'bool', |
45
|
|
|
'boolean', |
46
|
|
|
), |
47
|
|
|
self::STRING_TYPE => array( |
48
|
|
|
'string', |
49
|
|
|
), |
50
|
|
|
self::INTEGER_TYPE => array( |
51
|
|
|
'int', |
52
|
|
|
'integer', |
53
|
|
|
'long', |
54
|
|
|
), |
55
|
|
|
self::FLOAT_TYPE => array( |
56
|
|
|
'float', |
57
|
|
|
'double', |
58
|
|
|
'real', |
59
|
|
|
), |
60
|
|
|
self::NUMBER_TYPE => array( |
61
|
|
|
'int', |
62
|
|
|
'integer', |
63
|
|
|
'long', |
64
|
|
|
'float', |
65
|
|
|
'double', |
66
|
|
|
'real', |
67
|
|
|
), |
68
|
|
|
self::SCALAR_TYPE => array( |
69
|
|
|
'bool', |
70
|
|
|
'boolean', |
71
|
|
|
'string', |
72
|
|
|
'int', |
73
|
|
|
'integer', |
74
|
|
|
'long', |
75
|
|
|
'float', |
76
|
|
|
'double', |
77
|
|
|
'real', |
78
|
|
|
), |
79
|
|
|
self::ARRAY_TYPE => array( |
80
|
|
|
'array', |
81
|
|
|
), |
82
|
|
|
self::OBJECT_TYPE => array( |
83
|
|
|
'object', |
84
|
|
|
), |
85
|
|
|
self::RESOURCE_TYPE => array( |
86
|
|
|
'resource', |
87
|
|
|
), |
88
|
|
|
self::CALLABLE_TYPE => array( |
89
|
|
|
'callable', |
90
|
|
|
), |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Reflected type or class name |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
private $type; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var bool |
102
|
|
|
*/ |
103
|
|
|
private $enableAutoload; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param mixed $variable |
107
|
5 |
|
* @return TypeReflection |
108
|
|
|
*/ |
109
|
5 |
|
public static function createFromVariable($variable) |
110
|
5 |
|
{ |
111
|
5 |
|
return new self(is_object($variable) ? get_class($variable) : strtolower(gettype($variable))); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Constructor. |
116
|
|
|
* |
117
|
|
|
* @param string $type Reflected type or class name |
118
|
1 |
|
* @param bool $enableAutoload |
119
|
|
|
*/ |
120
|
1 |
|
public function __construct($type, $enableAutoload = false) |
121
|
|
|
{ |
122
|
|
|
$this->setType($type); |
123
|
|
|
$this->enableAutoload = $enableAutoload; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Getter of $enableAutoload |
128
|
|
|
* |
129
|
|
|
* @return boolean |
130
|
|
|
*/ |
131
|
|
|
public function isAutoloadEnabled() |
132
|
|
|
{ |
133
|
|
|
return $this->enableAutoload; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Setter of $enableAutoload |
138
|
5 |
|
* |
139
|
|
|
* @param boolean $enableAutoload |
140
|
5 |
|
*/ |
141
|
|
|
public function setEnableAutoload($enableAutoload) |
142
|
|
|
{ |
143
|
|
|
$this->enableAutoload = (boolean)$enableAutoload; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Gets the reflected type. |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
1 |
|
*/ |
151
|
|
|
public function getType() |
152
|
1 |
|
{ |
153
|
1 |
|
return $this->type; |
154
|
1 |
|
} |
155
|
|
|
|
156
|
1 |
|
/** |
157
|
1 |
|
* Depending on the type, returns a standardized type, a constant value of XXX_TYPE. |
158
|
|
|
* Note that if the type is a class name, object type is returned. |
159
|
|
|
* |
160
|
|
|
* |
161
|
|
|
* @return string |
|
|
|
|
162
|
|
|
*/ |
163
|
|
|
public function getStandardizedType() |
164
|
|
|
{ |
165
|
|
|
foreach ($this->getStandardizedTypes() as $standardizedType) { |
166
|
|
|
if ($this->is($standardizedType)) { |
167
|
|
|
return $standardizedType; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
return self::UNKNOWN_TYPE; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Indicates if the type is recognized. |
175
|
|
|
* |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
public function isValid() |
179
|
|
|
{ |
180
|
|
|
return !$this->is(self::UNKNOWN_TYPE); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Indicates whether the reflected type is void. |
185
|
|
|
* Valid values are: |
186
|
|
|
* - void |
187
|
|
|
* |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function isVoid() |
191
|
|
|
{ |
192
|
|
|
return $this->is(self::VOID_TYPE); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Indicates whether the reflected type is mixed. |
197
|
|
|
* Valid values are: |
198
|
|
|
* - mixed |
199
|
|
|
* |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
public function isMixed() |
203
|
|
|
{ |
204
|
|
|
return $this->is(self::MIXED_TYPE); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Indicates whether the reflected type is null. |
209
|
|
|
* Valid values are: |
210
|
|
|
* - null |
211
|
|
|
* |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public function isNull() |
215
|
|
|
{ |
216
|
|
|
return $this->is(self::NULL_TYPE); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Indicates whether the reflected type is boolean. |
221
|
|
|
* Valid values are: |
222
|
|
|
* - bool |
223
|
|
|
* - boolean |
224
|
|
|
* |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
public function isBoolean() |
228
|
|
|
{ |
229
|
|
|
return $this->is(self::BOOLEAN_TYPE); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Indicates whether the reflected type is string. |
234
|
|
|
* Valid values are: |
235
|
|
|
* - string |
236
|
|
|
* |
237
|
|
|
* @return bool |
238
|
|
|
*/ |
239
|
|
|
public function isString() |
240
|
1 |
|
{ |
241
|
|
|
return $this->is(self::STRING_TYPE); |
242
|
1 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Indicates whether the reflected type is integer. |
246
|
|
|
* Valid values are |
247
|
|
|
* - int |
248
|
|
|
* - integer |
249
|
|
|
* - long |
250
|
|
|
* |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
public function isInteger() |
254
|
1 |
|
{ |
255
|
|
|
return $this->is(self::INTEGER_TYPE); |
256
|
1 |
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Indicates whether the reflected type is float. |
260
|
|
|
* Valid values are |
261
|
|
|
* - float |
262
|
|
|
* - double |
263
|
|
|
* - real |
264
|
|
|
* |
265
|
|
|
* @return bool |
266
|
|
|
*/ |
267
|
1 |
|
public function isFloat() |
268
|
|
|
{ |
269
|
1 |
|
return $this->is(self::FLOAT_TYPE); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Indicates whether the reflected type is number. |
274
|
|
|
* Valid values are: |
275
|
|
|
* - every value valid for int |
276
|
|
|
* - every value valid for float |
277
|
|
|
* |
278
|
|
|
* @return bool |
279
|
|
|
*/ |
280
|
|
|
public function isNumber() |
281
|
|
|
{ |
282
|
1 |
|
return $this->is(self::NUMBER_TYPE); |
283
|
|
|
} |
284
|
1 |
|
|
285
|
|
|
/** |
286
|
|
|
* Indicates whether the reflected type is scalar. |
287
|
|
|
* Valid values are: |
288
|
|
|
* - every value valid for boolean |
289
|
|
|
* - every value valid for string |
290
|
|
|
* - every value valid for int |
291
|
|
|
* - every value valid for float |
292
|
|
|
* |
293
|
|
|
* @return bool |
294
|
|
|
*/ |
295
|
|
|
public function isScalar() |
296
|
|
|
{ |
297
|
|
|
return $this->is(self::SCALAR_TYPE); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Indicates whether the reflected type is array. |
302
|
|
|
* Valid values are: |
303
|
|
|
* - array |
304
|
|
|
* - xxx[] |
305
|
|
|
* |
306
|
|
|
* @return bool |
307
|
|
|
*/ |
308
|
|
|
public function isArray() |
309
|
|
|
{ |
310
|
|
|
return $this->is(self::ARRAY_TYPE); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Indicates whether the reflected type is object. |
315
|
|
|
* Valid values are: |
316
|
|
|
* - object |
317
|
|
|
* - valid class name |
318
|
|
|
* |
319
|
|
|
* @return bool |
320
|
|
|
*/ |
321
|
|
|
public function isObject() |
322
|
|
|
{ |
323
|
|
|
return $this->is(self::OBJECT_TYPE); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Indicates whether the reflected type is resource. |
328
|
|
|
* Valid values are: |
329
|
|
|
* - resource |
330
|
|
|
* |
331
|
5 |
|
* @return bool |
332
|
|
|
*/ |
333
|
5 |
|
public function isResource() |
334
|
1 |
|
{ |
335
|
|
|
return $this->is(self::RESOURCE_TYPE); |
336
|
5 |
|
} |
337
|
1 |
|
|
338
|
|
|
/** |
339
|
5 |
|
* Indicates whether the reflected type is callable. |
340
|
|
|
* Valid values are: |
341
|
|
|
* - callable |
342
|
|
|
* |
343
|
|
|
* @return bool |
344
|
|
|
*/ |
345
|
|
|
public function isCallable() |
346
|
|
|
{ |
347
|
5 |
|
return $this->is(self::CALLABLE_TYPE); |
348
|
|
|
} |
349
|
5 |
|
|
350
|
5 |
|
/** |
351
|
|
|
* Indicates whether the reflected type is $standardizedType. |
352
|
|
|
* |
353
|
|
|
* @param string $standardizedType see specific const. |
354
|
|
|
* @return bool |
355
|
|
|
*/ |
356
|
|
|
public function is($standardizedType) |
357
|
5 |
|
{ |
358
|
|
|
if ($standardizedType === self::OBJECT_TYPE && class_exists($this->getType(), $this->isAutoloadEnabled())) { |
359
|
5 |
|
return true; |
360
|
|
|
} |
361
|
|
|
if ($standardizedType === self::ARRAY_TYPE && stripos($this->getType(), '[]') !== false) { |
362
|
|
|
return true; |
363
|
|
|
} |
364
|
|
|
return $this->isInTypeMapping($standardizedType); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
5 |
|
* Setter of $type. |
369
|
|
|
* |
370
|
5 |
|
* @param string $type |
371
|
|
|
*/ |
372
|
|
|
private function setType($type) |
373
|
|
|
{ |
374
|
|
|
$this->type = (string)$type; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Getter of $typeMappingList. |
379
|
5 |
|
* |
380
|
|
|
* @return array |
381
|
5 |
|
*/ |
382
|
|
|
private function getTypeMappingList() |
383
|
|
|
{ |
384
|
|
|
return $this->typeMappingList; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Returns a type mapping. |
389
|
1 |
|
* |
390
|
|
|
* @param string $standardizeType |
391
|
1 |
|
* @return array |
392
|
|
|
*/ |
393
|
|
|
private function getTypeMapping($standardizeType = '') |
394
|
|
|
{ |
395
|
|
|
return $this->getTypeMappingList()[$standardizeType]; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Indicates whether self::$type is a possible type of $standardizedType. |
400
|
|
|
* |
401
|
|
|
* @param string $standardizedType |
402
|
|
|
* @return bool |
403
|
|
|
*/ |
404
|
|
|
private function isInTypeMapping($standardizedType) |
405
|
|
|
{ |
406
|
|
|
return in_array(strtolower($this->getType()), $this->getTypeMapping($standardizedType), true); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* get the possible types. |
411
|
|
|
* |
412
|
|
|
* @return array |
|
|
|
|
413
|
|
|
*/ |
414
|
|
|
private function getStandardizedTypes() |
415
|
|
|
{ |
416
|
|
|
return array_keys($this->getTypeMappingList()); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.