1 | <?php |
||
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 | * @return TypeReflection |
||
108 | */ |
||
109 | 5 | public static function createFromVariable($variable) |
|
113 | |||
114 | /** |
||
115 | * Constructor. |
||
116 | * |
||
117 | * @param string $type Reflected type or class name |
||
118 | * @param bool $enableAutoload |
||
119 | */ |
||
120 | 10 | public function __construct($type, $enableAutoload = false) |
|
125 | |||
126 | /** |
||
127 | * Getter of $enableAutoload |
||
128 | * |
||
129 | * @return boolean |
||
130 | */ |
||
131 | 1 | public function isAutoloadEnabled() |
|
135 | |||
136 | /** |
||
137 | * Setter of $enableAutoload |
||
138 | * |
||
139 | * @param boolean $enableAutoload |
||
140 | */ |
||
141 | public function setEnableAutoload($enableAutoload) |
||
145 | |||
146 | /** |
||
147 | * Gets the reflected type. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 10 | public function getType() |
|
155 | |||
156 | /** |
||
157 | * 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 | 1 | public function getStandardizedType() |
|
172 | |||
173 | /** |
||
174 | * Indicates if the type is recognized. |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function isValid() |
||
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() |
||
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() |
||
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() |
||
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() |
||
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() |
||
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 | 1 | public function isInteger() |
|
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() |
|
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 | 1 | public function isNumber() |
|
284 | |||
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 | 1 | public function isScalar() |
|
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() |
||
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() |
||
325 | |||
326 | /** |
||
327 | * Indicates whether the reflected type is resource. |
||
328 | * Valid values are: |
||
329 | * - resource |
||
330 | * |
||
331 | * @return bool |
||
332 | */ |
||
333 | public function isResource() |
||
337 | |||
338 | /** |
||
339 | * Indicates whether the reflected type is callable. |
||
340 | * Valid values are: |
||
341 | * - callable |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | public function isCallable() |
||
349 | |||
350 | /** |
||
351 | * Indicates whether the reflected type is $standardizedType. |
||
352 | * |
||
353 | * @param string $standardizedType see specific const. |
||
354 | * @return bool |
||
355 | */ |
||
356 | 5 | public function is($standardizedType) |
|
366 | |||
367 | /** |
||
368 | * Setter of $type. |
||
369 | * |
||
370 | * @param string $type |
||
371 | */ |
||
372 | 10 | private function setType($type) |
|
376 | |||
377 | /** |
||
378 | * Getter of $typeMappingList. |
||
379 | * |
||
380 | * @return array |
||
381 | */ |
||
382 | 5 | private function getTypeMappingList() |
|
386 | |||
387 | /** |
||
388 | * Returns a type mapping. |
||
389 | * |
||
390 | * @param string $standardizeType |
||
391 | * @return array |
||
392 | */ |
||
393 | 5 | private function getTypeMapping($standardizeType = '') |
|
397 | |||
398 | /** |
||
399 | * Indicates whether self::$type is a possible type of $standardizedType. |
||
400 | * |
||
401 | * @param string $standardizedType |
||
402 | * @return bool |
||
403 | */ |
||
404 | 5 | private function isInTypeMapping($standardizedType) |
|
408 | |||
409 | /** |
||
410 | * get the possible types. |
||
411 | * |
||
412 | * @return array |
||
413 | */ |
||
414 | 1 | private function getStandardizedTypes() |
|
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.