Completed
Push — master ( 5c8cc7...fa15a1 )
by Дмитрий
11:26
created

Types::getType()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 11.4763

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
ccs 16
cts 19
cp 0.8421
rs 5.2653
cc 11
eloc 22
nc 11
nop 1
crap 11.4763

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::NUMBER:
27
                return 'number';
28
            case CompiledExpression::ARR:
29
                return 'array';
30
            case CompiledExpression::OBJECT:
31
                return 'object';
32
            case CompiledExpression::RESOURCE:
33
                return 'resource';
34
            case CompiledExpression::CALLABLE_TYPE:
35
                return 'callable';
36
            case CompiledExpression::BOOLEAN:
37
                return 'boolean';
38
            case CompiledExpression::NULL:
39
                return 'null';
40
            default:
41
                return 'uknown';
42
        }
43
    }
44
45
    /**
46
     * Get type by $value
47
     *
48
     * @param $value
49
     * @return int
50
     */
51 144
    public static function getTypeByValue($value)
52
    {
53 144
        return self::getType(gettype($value));
54
    }
55
56
57
    /**
58
     * Get type (integer) by $type
59
     *
60
     * @param string $typeName
61
     * @return int
62
     */
63 144
    public static function getType($typeName)
64
    {
65
        switch ($typeName) {
66 144
            case 'integer':
67 80
            case 'int':
68 64
                return CompiledExpression::INTEGER;
69 47
            case 'double':
70 17
                return CompiledExpression::DOUBLE;
71 1
            case 'string':
72 16
                return CompiledExpression::STRING;
73
            case 'resource':
74 16
                return CompiledExpression::RESOURCE;
75
            case 'callable':
76 16
                return CompiledExpression::CALLABLE_TYPE;
77
            case 'object':
78 16
                return CompiledExpression::OBJECT;
79 1
            case 'array':
80 15
                return CompiledExpression::ARR;
81 14
            case 'boolean':
82 1
                return CompiledExpression::BOOLEAN;
83 1
            case 'NULL':
84
                return CompiledExpression::NULL;
85
        }
86
87
        //@codeCoverageIgnoreStart
88
        throw new RuntimeException("Type '{$typeName}' is not supported");
89
        //@codeCoverageIgnoreEnd
90
    }
91
}
92