Completed
Push — master ( f3a461...296f07 )
by Дмитрий
09:44
created

Types   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 41.86%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 21
c 5
b 1
f 2
lcom 0
cbo 0
dl 0
loc 80
ccs 18
cts 43
cp 0.4186
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D getTypeName() 0 25 10
A getTypeByValue() 0 4 1
D getType() 0 27 10
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
                return CompiledExpression::LNUMBER;
68 64
            case 'double':
69 47
                return CompiledExpression::DNUMBER;
70 17
            case 'string':
71 1
                return CompiledExpression::STRING;
72 16
            case 'resource':
73
                return CompiledExpression::RESOURCE;
74 16
            case 'callable':
75
                return CompiledExpression::CALLABLE_TYPE;
76 16
            case 'object':
77
                return CompiledExpression::OBJECT;
78 16
            case 'array':
79 1
                return CompiledExpression::ARR;
80 15
            case 'boolean':
81 14
                return CompiledExpression::BOOLEAN;
82 1
            case 'NULL':
83 1
                return CompiledExpression::NULL;
84
        }
85
86
        //@codeCoverageIgnoreStart
87
        throw new RuntimeException("Type '{$typeName}' is not supported");
88
        //@codeCoverageIgnoreEnd
89
    }
90
}
91