GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( cb3266...7d01f1 )
by Raphaël
02:03
created

TypeReflection   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 407
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 64.06%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 33
c 3
b 0
f 2
lcom 1
cbo 0
dl 0
loc 407
ccs 41
cts 64
cp 0.6406
rs 9.4

26 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromVariable() 0 4 2
A __construct() 0 5 1
A isAutoloadEnabled() 0 4 1
A setEnableAutoload() 0 4 1
A getType() 0 4 1
A getStandardizedType() 0 9 3
A isValid() 0 4 1
A isVoid() 0 4 1
A isMixed() 0 4 1
A isNull() 0 4 1
A isBoolean() 0 4 1
A isString() 0 4 1
A isInteger() 0 4 1
A isFloat() 0 4 1
A isNumber() 0 4 1
A isScalar() 0 4 1
A isArray() 0 4 1
A isObject() 0 4 1
A isResource() 0 4 1
A isCallable() 0 4 1
B is() 0 10 5
A setType() 0 4 1
A getTypeMappingList() 0 4 1
A getTypeMapping() 0 4 1
A isInTypeMapping() 0 4 1
A getStandardizedTypes() 0 4 1
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
     * @return TypeReflection
108
     */
109 5
    public static function createFromVariable($variable)
110
    {
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
     * @param bool $enableAutoload
119
     */
120 10
    public function __construct($type, $enableAutoload = false)
121
    {
122 10
        $this->setType($type);
123 10
        $this->enableAutoload = $enableAutoload;
124 10
    }
125
126
    /**
127
     * Getter of $enableAutoload
128
     *
129
     * @return boolean
130
     */
131 1
    public function isAutoloadEnabled()
132
    {
133 1
        return $this->enableAutoload;
134
    }
135
136
    /**
137
     * Setter of $enableAutoload
138
     *
139
     * @param boolean $enableAutoload
140
     */
141
    public function setEnableAutoload($enableAutoload)
142
    {
143
        $this->enableAutoload = (boolean)$enableAutoload;
144
    }
145
146
    /**
147
     * Gets the reflected type.
148
     *
149
     * @return string
150
     */
151 10
    public function getType()
152
    {
153 10
        return $this->type;
154
    }
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
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

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.

Loading history...
162
     */
163 1
    public function getStandardizedType()
164
    {
165 1
        foreach ($this->getStandardizedTypes() as $standardizedType) {
166 1
            if ($this->is($standardizedType)) {
167 1
                return $standardizedType;
168
            }
169 1
        }
170 1
        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
    {
241
        return $this->is(self::STRING_TYPE);
242
    }
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()
254
    {
255 1
        return $this->is(self::INTEGER_TYPE);
256
    }
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 1
    public function isNumber()
281
    {
282 1
        return $this->is(self::NUMBER_TYPE);
283
    }
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()
296
    {
297 1
        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
     * @return bool
332
     */
333
    public function isResource()
334
    {
335
        return $this->is(self::RESOURCE_TYPE);
336
    }
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()
346
    {
347
        return $this->is(self::CALLABLE_TYPE);
348
    }
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)
357
    {
358 5
        if ($standardizedType === self::OBJECT_TYPE && class_exists($this->getType(), $this->isAutoloadEnabled())) {
359 1
            return true;
360
        }
361 5
        if ($standardizedType === self::ARRAY_TYPE && stripos($this->getType(), '[]') !== false) {
362 1
            return true;
363
        }
364 5
        return $this->isInTypeMapping($standardizedType);
365
    }
366
367
    /**
368
     * Setter of $type.
369
     *
370
     * @param string $type
371
     */
372 10
    private function setType($type)
373
    {
374 10
        $this->type = (string)$type;
375 10
    }
376
377
    /**
378
     * Getter of $typeMappingList.
379
     *
380
     * @return array
381
     */
382 5
    private function getTypeMappingList()
383
    {
384 5
        return $this->typeMappingList;
385
    }
386
387
    /**
388
     * Returns a type mapping.
389
     *
390
     * @param string $standardizeType
391
     * @return array
392
     */
393 5
    private function getTypeMapping($standardizeType = '')
394
    {
395 5
        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 5
    private function isInTypeMapping($standardizedType)
405
    {
406 5
        return in_array(strtolower($this->getType()), $this->getTypeMapping($standardizedType), true);
407
    }
408
409
    /**
410
     * get the possible types.
411
     *
412
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
413
     */
414 1
    private function getStandardizedTypes()
415
    {
416 1
        return array_keys($this->getTypeMappingList());
417
    }
418
}
419