Completed
Pull Request — master (#116)
by Enrico
04:25
created

Types::getTypeName()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 27
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 12.8905

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 11
nop 1
dl 0
loc 27
ccs 18
cts 24
cp 0.75
crap 12.8905
rs 5.2653
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 5
    public static function getTypeName($type)
20
    {
21
        switch ($type) {
22 5
            case CompiledExpression::INTEGER:
23 5
                return 'integer';
24 1
            case CompiledExpression::DOUBLE:
25 1
                return 'double';
26 1
            case CompiledExpression::STRING:
27 1
                return 'string';
28 1
            case CompiledExpression::NUMBER:
29
                return 'number';
30 1
            case CompiledExpression::ARR:
31 1
                return 'array';
32 1
            case CompiledExpression::OBJECT:
33 1
                return 'object';
34 1
            case CompiledExpression::RESOURCE:
35
                return 'resource';
36 1
            case CompiledExpression::CALLABLE_TYPE:
37
                return 'callable';
38 1
            case CompiledExpression::BOOLEAN:
39 1
                return 'boolean';
40 1
            case CompiledExpression::NULL:
41 1
                return 'null';
42
            default:
43
                return 'unknown';
44
        }
45
    }
46
47
    /**
48
     * Get type by $value
49
     *
50
     * @param $value
51
     * @return int
52
     */
53 145
    public static function getTypeByValue($value)
54
    {
55 145
        return self::getType(gettype($value));
56
    }
57
58
59
    /**
60
     * Get type (integer) by $type
61
     *
62
     * @param string $typeName
63
     * @return int
64
     */
65 145
    public static function getType($typeName)
66
    {
67
        switch ($typeName) {
68 145
            case 'integer':
69 145
            case 'int':
70 81
                return CompiledExpression::INTEGER;
71 64
            case 'double':
72 47
                return CompiledExpression::DOUBLE;
73 17
            case 'string':
74 1
                return CompiledExpression::STRING;
75 16
            case 'resource':
76
                return CompiledExpression::RESOURCE;
77 16
            case 'callable':
78
                return CompiledExpression::CALLABLE_TYPE;
79 16
            case 'object':
80
                return CompiledExpression::OBJECT;
81 16
            case 'array':
82 1
                return CompiledExpression::ARR;
83 15
            case 'boolean':
84 14
                return CompiledExpression::BOOLEAN;
85 1
            case 'NULL':
86 1
                return CompiledExpression::NULL;
87
        }
88
89
        //@codeCoverageIgnoreStart
90
        throw new RuntimeException("Type '{$typeName}' is not supported");
91
        //@codeCoverageIgnoreEnd
92
    }
93
}
94