Completed
Pull Request — master (#116)
by Enrico
03:35
created

Types   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 19.57%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 83
ccs 9
cts 46
cp 0.1957
rs 10
wmc 23
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeByValue() 0 4 1
C getTypeName() 0 27 11
C getType() 0 28 11
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 1
    public static function getTypeName($type)
20
    {
21
        switch ($type) {
22 1
            case CompiledExpression::INTEGER:
23 1
                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
            default:
43
                return 'unknown';
44
        }
45
    }
46
47
    /**
48
     * Get type by $value
49
     *
50
     * @param $value
51
     * @return int
52
     */
53 1
    public static function getTypeByValue($value)
54
    {
55 1
        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 1
    public static function getType($typeName)
66
    {
67
        switch ($typeName) {
68 1
            case 'integer':
69 1
            case 'int':
70 1
                return CompiledExpression::INTEGER;
71
            case 'double':
72
                return CompiledExpression::DOUBLE;
73
            case 'string':
74
                return CompiledExpression::STRING;
75
            case 'resource':
76
                return CompiledExpression::RESOURCE;
77
            case 'callable':
78
                return CompiledExpression::CALLABLE_TYPE;
79
            case 'object':
80
                return CompiledExpression::OBJECT;
81
            case 'array':
82
                return CompiledExpression::ARR;
83
            case 'boolean':
84
                return CompiledExpression::BOOLEAN;
85
            case 'NULL':
86
                return CompiledExpression::NULL;
87
        }
88
89
        //@codeCoverageIgnoreStart
90
        throw new RuntimeException("Type '{$typeName}' is not supported");
91
        //@codeCoverageIgnoreEnd
92
    }
93
}
94