Completed
Push — master ( f7db50...090344 )
by Дмитрий
03:31
created

CompiledExpression::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA;
7
8
use Ovr\PHPReflection\Types;
9
use PHPSA\Compiler\Types as CompilerTypes;
10
use RuntimeException;
11
12
class CompiledExpression
13
{
14
    /**
15
     * Unknown type
16
     */
17
    const UNKNOWN = Types::UNKNOWN_TYPE;
18
19
    /**
20
     * It's not unknown, It's unimplemented
21
     */
22
    const UNIMPLEMENTED = -100;
23
24
    /**
25
     * Void type
26
     */
27
    const VOID = Types::VOID_TYPE;
28
29
    const INTEGER = Types::INT_TYPE;
30
31
    /**
32
     * @deprectated
33
     */
34
    const LNUMBER = self::INTEGER;
35
36
    /**
37
     * Double/Float
38
     */
39
    const DOUBLE = Types::DOUBLE_TYPE;
40
41
    /**
42
     * Double/Float
43
     */
44
    const NUMBER = Types::NUMBER;
45
46
    /**
47
     * Double/Float
48
     * @deprectated
49
     */
50
    const DNUMBER = self::DOUBLE;
51
52
    /**
53
     * String
54
     */
55
    const STRING = Types::STRING_TYPE;
56
57
    /**
58
     * Boolean
59
     * true or false
60
     */
61
    const BOOLEAN = Types::BOOLEAN_TYPE;
62
63
    /**
64
     * Array
65
     */
66
    const ARR = Types::ARRAY_TYPE;
67
68
    /**
69
     * Object
70
     */
71
    const OBJECT = Types::OBJECT_TYPE;
72
73
    /**
74
     * Resource handler
75
     */
76
    const RESOURCE = Types::RESOURCE_TYPE;
77
78
    /**
79
     * Callable type
80
     */
81
    const CALLABLE_TYPE = Types::CALLABLE_TYPE;
82
83
    /**
84
     * Value is handled in variable
85
     */
86
    const VARIABLE = 512;
87
88
    /**
89
     * Resource handler
90
     */
91
    const NULL = Types::NULL_TYPE;
92
93
    /**
94
     * self::INT_TYPE | self::DOUBLE_TYPE | self::STRING_TYPE | self::BOOLEAN_TYPE | self::ARRAY_TYPE | self::RESOURCE_TYPE | self::OBJECT_TYPE | self::NULL_TYPE
95
     */
96
    const MIXED = Types::MIXED;
97
98
    /**
99
     * I can't explain what it's :D
100
     */
101
    const DYNAMIC = 10000;
102
103
    /**
104
     * By default we don't know what it is
105
     *
106
     * @var int
107
     */
108
    protected $type;
109
110
    /**
111
     * Possible value
112
     *
113
     * @var mixed
114
     */
115
    protected $value;
116
117
118
    /**
119
     * Construct new CompiledExpression to pass result
120
     *
121
     * @param int $type
122
     * @param null $value
123
     */
124 44
    public function __construct($type = self::UNKNOWN, $value = null)
125
    {
126 44
        $this->type = $type;
127 44
        $this->value = $value;
128 44
    }
129
130
    /**
131
     * @return mixed
132
     */
133 43
    public function getValue()
134
    {
135 43
        return $this->value;
136
    }
137
138
    /**
139
     * @param integer $value
140
     * @return boolean
141
     */
142
    public function isEquals($value)
143
    {
144
        return $this->value == $value;
145
    }
146
147
    /**
148
     * @return int
149
     */
150 44
    public function getType()
151
    {
152 44
        return $this->type;
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getTypeName()
159
    {
160
        return CompilerTypes::getTypeName($this->type);
161
    }
162
163
    /**
164
     * @param string $name Name of the Variable
165
     * @return Variable
166
     */
167 1
    public function toVariable($name)
168
    {
169 1
        return new Variable($name, $this->value, $this->type);
170
    }
171
172
    /**
173
     * If we don't know $type but know $value
174
     *
175
     * @param $value
176
     * @throws RuntimeException
177
     * @return CompiledExpression
178
     */
179 6
    public static function fromZvalValue($value)
180
    {
181 6
        return new CompiledExpression(CompilerTypes::getTypeByValue($value), $value);
182
    }
183
184
    //@codeCoverageIgnoreStart
185
    /**
186
     * Check that $this->value is correct for $this->type
187
     *
188
     * @todo Implement it ;)
189
     * @return boolean
190
     */
191
    public function isCorrectValue()
192
    {
193
        switch ($this->type) {
194
            case CompiledExpression::INTEGER:
195
                return gettype($this->value) == 'integer';
196
            case CompiledExpression::NUMBER:
197
                return gettype($this->value) == 'integer' || gettype($this->value) == 'double';
198
            case CompiledExpression::DOUBLE:
199
                return gettype($this->value) == 'double';
200
        }
201
202
        return true;
203
    }
204
    //@codeCoverageIgnoreEnd
205
}
206