Test Failed
Push — master ( a0eed4...bb00ac )
by Kirill
149:40
created

NumberValueNode   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 24 6
A isHex() 0 4 1
A parseHex() 0 4 1
A isBinary() 0 4 1
A parseBin() 0 4 1
A isExponential() 0 4 1
A parseExponential() 0 4 1
A isFloat() 0 4 1
A parseFloat() 0 4 1
A isInt() 0 4 2
A parseInt() 0 4 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Frontend\AST\Invocation;
11
12
use Railt\Parser\Ast\LeafInterface;
13
use Railt\SDL\Frontend\Parser;
14
15
/**
16
 * Class NumberValue
17
 */
18
class NumberValueNode extends AbstractValueNode
19
{
20
    /**
21
     * @return int|float
22
     */
23
    public function parse()
24
    {
25
        /** @var LeafInterface $value */
26
        $value = $this->getChild(0);
27
28
        switch (true) {
29
            case $this->isHex($value):
30
                return $this->parseHex($value->getValue(1));
31
32
            case $this->isBinary($value):
33
                return $this->parseBin($value->getValue(1));
34
35
            case $this->isExponential($value):
36
                return $this->parseExponential($value->getValue());
37
38
            case $this->isFloat($value):
39
                return $this->parseFloat($value->getValue());
40
41
            case $this->isInt($value):
42
                return $this->parseInt($value->getValue());
43
        }
44
45
        return (float)$value->getValue();
46
    }
47
48
    /**
49
     * @param LeafInterface $leaf
50
     * @return bool
51
     */
52
    private function isHex(LeafInterface $leaf): bool
53
    {
54
        return $leaf->getName() === Parser::T_HEX_NUMBER;
55
    }
56
57
    /**
58
     * @param string $value
59
     * @return int
60
     */
61
    private function parseHex(string $value): int
62
    {
63
        return \hexdec($value);
64
    }
65
66
    /**
67
     * @param LeafInterface $leaf
68
     * @return bool
69
     */
70
    private function isBinary(LeafInterface $leaf): bool
71
    {
72
        return $leaf->getName() === Parser::T_BIN_NUMBER;
73
    }
74
75
    /**
76
     * @param string $value
77
     * @return int
78
     */
79
    private function parseBin(string $value): int
80
    {
81
        return \bindec($value);
82
    }
83
84
    /**
85
     * @param LeafInterface $leaf
86
     * @return bool
87
     */
88
    private function isExponential(LeafInterface $leaf): bool
89
    {
90
        return \substr_count(\mb_strtolower($leaf->getValue()), 'e') !== 0;
91
    }
92
93
    /**
94
     * @param string $value
95
     * @return float
96
     */
97
    private function parseExponential(string $value): float
98
    {
99
        return (float)$value;
100
    }
101
102
    /**
103
     * @param LeafInterface $leaf
104
     * @return bool
105
     */
106
    private function isFloat(LeafInterface $leaf): bool
107
    {
108
        return \substr_count($leaf->getValue(), '.') !== 0;
109
    }
110
111
    /**
112
     * @param string $value
113
     * @return float
114
     */
115
    private function parseFloat(string $value): float
116
    {
117
        return (float)$value;
118
    }
119
120
    /**
121
     * @param LeafInterface $leaf
122
     * @return bool
123
     */
124
    private function isInt(LeafInterface $leaf): bool
125
    {
126
        return $leaf->getName() === Parser::T_NUMBER && \substr_count($leaf->getValue(), '.') === 0;
127
    }
128
129
    /**
130
     * @param string $value
131
     * @return int
132
     */
133
    private function parseInt(string $value): int
134
    {
135
        return $value >> 0;
136
    }
137
}
138