Test Failed
Push — master ( 6bac61...6350a6 )
by Kirill
03:02
created

NumberValueNode::parseInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Value;
11
12
use Railt\Parser\Ast\LeafInterface;
13
use Railt\Parser\Ast\Rule;
14
use Railt\SDL\Compiler\Parser;
15
16
/**
17
 * Class NumberValueNode
18
 */
19
class NumberValueNode extends Rule implements ValueInterface
20
{
21
    /**
22
     * @return string
23
     */
24
    public function toString(): string
25
    {
26
        return (string)$this->toPrimitive();
27
    }
28
29
    /**
30
     * @return float|int
31
     */
32
    public function toPrimitive()
33
    {
34
        return $this->parse();
35
    }
36
37
    /**
38
     * @return int|float
39
     */
40
    protected function parse()
41
    {
42
        /** @var LeafInterface $value */
43
        $value = $this->getChild(0);
44
45
        switch (true) {
46
            case $this->isHex($value):
47
                return $this->parseHex($value->getValue(1));
48
49
            case $this->isBinary($value):
50
                return $this->parseBin($value->getValue(1));
51
52
            case $this->isExponential($value):
53
                return $this->parseExponential($value->getValue());
54
55
            case $this->isFloat($value):
56
                return $this->parseFloat($value->getValue());
57
58
            case $this->isInt($value):
59
                return $this->parseInt($value->getValue());
60
        }
61
62
        return (float)$value->getValue();
63
    }
64
65
    /**
66
     * @param LeafInterface $leaf
67
     * @return bool
68
     */
69
    private function isHex(LeafInterface $leaf): bool
70
    {
71
        return $leaf->getName() === Parser::T_HEX_NUMBER;
72
    }
73
74
    /**
75
     * @param string $value
76
     * @return int
77
     */
78
    private function parseHex(string $value): int
79
    {
80
        return \hexdec($value);
81
    }
82
83
    /**
84
     * @param LeafInterface $leaf
85
     * @return bool
86
     */
87
    private function isBinary(LeafInterface $leaf): bool
88
    {
89
        return $leaf->getName() === Parser::T_BIN_NUMBER;
90
    }
91
92
    /**
93
     * @param string $value
94
     * @return int
95
     */
96
    private function parseBin(string $value): int
97
    {
98
        return \bindec($value);
99
    }
100
101
    /**
102
     * @param LeafInterface $leaf
103
     * @return bool
104
     */
105
    private function isExponential(LeafInterface $leaf): bool
106
    {
107
        return \substr_count(\mb_strtolower($leaf->getValue()), 'e') !== 0;
108
    }
109
110
    /**
111
     * @param string $value
112
     * @return float
113
     */
114
    private function parseExponential(string $value): float
115
    {
116
        return (float)$value;
117
    }
118
119
    /**
120
     * @param LeafInterface $leaf
121
     * @return bool
122
     */
123
    private function isFloat(LeafInterface $leaf): bool
124
    {
125
        return \substr_count($leaf->getValue(), '.') !== 0;
126
    }
127
128
    /**
129
     * @param string $value
130
     * @return float
131
     */
132
    private function parseFloat(string $value): float
133
    {
134
        return (float)$value;
135
    }
136
137
    /**
138
     * @param LeafInterface $leaf
139
     * @return bool
140
     */
141
    private function isInt(LeafInterface $leaf): bool
142
    {
143
        return $leaf->getName() === Parser::T_NUMBER && \substr_count($leaf->getValue(), '.') === 0;
144
    }
145
146
    /**
147
     * @param string $value
148
     * @return int
149
     */
150
    private function parseInt(string $value): int
151
    {
152
        return $value >> 0;
153
    }
154
}
155