Completed
Push — master ( a124c8...dbcf9b )
by Дмитрий
03:11
created

CompiledExpression::getTypeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
     * @var Variable|null
119
     */
120
    protected $variable;
121
122
    /**
123
     * Construct new CompiledExpression to pass result
124
     *
125
     * @param int $type
126
     * @param mixed $value
127
     * @param Variable|null $variable
128
     */
129 370
    public function __construct($type = self::UNKNOWN, $value = null, Variable $variable = null)
130
    {
131 370
        $this->type = $type;
132 370
        $this->value = $value;
133 370
        $this->variable = $variable;
134 370
    }
135
136
    /**
137
     * @return mixed
138
     */
139 370
    public function getValue()
140
    {
141 370
        return $this->value;
142
    }
143
144
    /**
145
     * @param integer $value
146
     * @return boolean
147
     */
148 72
    public function isEquals($value)
149
    {
150 72
        return $this->value == $value;
151
    }
152
153
    /**
154
     * @return int
155
     */
156 370
    public function getType()
157
    {
158 370
        return $this->type;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public function getTypeName()
165
    {
166
        return CompilerTypes::getTypeName($this->type);
167
    }
168
169
    /**
170
     * @param string $name Name of the Variable
171
     * @return Variable
172
     */
173 1
    public function toVariable($name)
174
    {
175 1
        return new Variable($name, $this->value, $this->type);
176
    }
177
178
    /**
179
     * If we don't know $type but know $value
180
     *
181
     * @param $value
182
     * @throws RuntimeException
183
     * @return CompiledExpression
184
     */
185 144
    public static function fromZvalValue($value)
186
    {
187 144
        return new CompiledExpression(CompilerTypes::getTypeByValue($value), $value);
188
    }
189
190
    /**
191
     * This is needed via in feature $this->type can store multiple type(s) by bitmask
192
     *
193
     * @return bool
194
     */
195
    public function canBeObject()
196
    {
197
        return (boolean) ($this->type == self::OBJECT || $this->type & self::OBJECT);
198
    }
199
200
    /**
201
     * @return bool
202
     */
203
    public function isObject()
204
    {
205
        return $this->type == self::OBJECT;
206
    }
207
208
    //@codeCoverageIgnoreStart
209
    /**
210
     * Check that $this->value is correct for $this->type
211
     *
212
     * @todo Implement it ;)
213
     * @return boolean
214
     */
215
    public function isCorrectValue()
216
    {
217
        $type = gettype($this->value);
218
219
        switch ($this->type) {
220
            case CompiledExpression::INTEGER:
221
                return $type == 'integer';
222
            case CompiledExpression::NUMBER:
223
                return $type == 'integer' || $type == 'double';
224
            case CompiledExpression::DOUBLE:
225
                return $type == 'double';
226
            case CompiledExpression::BOOLEAN:
227
                return $type == 'boolean';
228
        }
229
230
        return true;
231
    }
232
    //@codeCoverageIgnoreEnd
233
234
    /**
235
     * @return Variable|null
236
     */
237
    public function getVariable()
238
    {
239
        return $this->variable;
240
    }
241
}
242