Types::getType()   C
last analyzed

Complexity

Conditions 13
Paths 13

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
nc 13
nop 1
dl 0
loc 30
ccs 0
cts 22
cp 0
crap 182
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Compiler;
7
8
use PHPSA\CompiledExpression;
9
use RuntimeException;
10
11
class Types
12
{
13
    /**
14
     * Get name for the $type
15
     *
16
     * @param integer $type
17
     * @return string
18
     */
19
    public static function getTypeName($type)
20
    {
21
        switch ($type) {
22
            case CompiledExpression::INTEGER:
23
                return 'integer';
24
            case CompiledExpression::DOUBLE:
25
                return 'double';
26
            case CompiledExpression::STRING:
27
                return 'string';
28
            case CompiledExpression::NUMBER:
29
                return 'number';
30
            case CompiledExpression::ARR:
31
                return 'array';
32
            case CompiledExpression::OBJECT:
33
                return 'object';
34
            case CompiledExpression::RESOURCE:
35
                return 'resource';
36
            case CompiledExpression::CALLABLE_TYPE:
37
                return 'callable';
38
            case CompiledExpression::BOOLEAN:
39
                return 'boolean';
40
            case CompiledExpression::NULL:
41
                return 'null';
42
            case CompiledExpression::MIXED:
43
                return 'mixed';
44
            default:
45
                return 'unknown';
46
        }
47
    }
48
49
    /**
50
     * Get type by $value
51
     *
52
     * @param $value
53
     * @return int
54
     */
55
    public static function getTypeByValue($value)
56
    {
57
        return self::getType(gettype($value));
58
    }
59
60
    /**
61
     * Get type (integer) by $type
62
     *
63
     * @param string $typeName
64
     * @return int
65
     */
66
    public static function getType($typeName)
67
    {
68
        switch ($typeName) {
69
            case 'integer':
70
            case 'int':
71
                return CompiledExpression::INTEGER;
72
            case 'float':
73
            case 'double':
74
                return CompiledExpression::DOUBLE;
75
            case 'string':
76
                return CompiledExpression::STRING;
77
            case 'resource':
78
                return CompiledExpression::RESOURCE;
79
            case 'callable':
80
                return CompiledExpression::CALLABLE_TYPE;
81
            case 'object':
82
                return CompiledExpression::OBJECT;
83
            case 'array':
84
                return CompiledExpression::ARR;
85
            case 'boolean':
86
            case 'bool':
87
                return CompiledExpression::BOOLEAN;
88
            case 'NULL':
89
                return CompiledExpression::NULL;
90
        }
91
92
        //@codeCoverageIgnoreStart
93
        throw new RuntimeException("Type '{$typeName}' is not supported");
94
        //@codeCoverageIgnoreEnd
95
    }
96
}
97