1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA; |
7
|
|
|
|
8
|
|
|
use PHPSA\Compiler\Types; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A Variable |
12
|
|
|
*/ |
13
|
|
|
class Variable |
14
|
|
|
{ |
15
|
|
|
const BRANCH_ROOT = 0; |
16
|
|
|
|
17
|
|
|
const BRANCH_CONDITIONAL_TRUE = 1; |
18
|
|
|
|
19
|
|
|
const BRANCH_CONDITIONAL_FALSE = 2; |
20
|
|
|
|
21
|
|
|
const BRANCH_CONDITIONAL_EXTERNAL = 3; |
22
|
|
|
|
23
|
|
|
const BRANCH_UNKNOWN = 4; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string variable name |
27
|
|
|
*/ |
28
|
|
|
protected $name; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var mixed variable value |
32
|
|
|
*/ |
33
|
|
|
protected $value; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var integer|string |
37
|
|
|
*/ |
38
|
|
|
protected $branch; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var int how many times was read from the var |
42
|
|
|
*/ |
43
|
|
|
protected $gets = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int how many times was written to the var |
47
|
|
|
*/ |
48
|
|
|
protected $sets = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool is it referenced to another var? |
52
|
|
|
*/ |
53
|
|
|
protected $referenced = false; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var Variable|null to which variable referenced? |
57
|
|
|
*/ |
58
|
|
|
protected $referencedTo; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int variable type |
62
|
|
|
*/ |
63
|
|
|
protected $type; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Creates a variable. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* @param mixed $defaultValue |
70
|
|
|
* @param int $type |
71
|
|
|
* @param int|string $branch |
72
|
|
|
*/ |
73
|
54 |
|
public function __construct($name, $defaultValue = null, $type = CompiledExpression::UNKNOWN, $branch = self::BRANCH_ROOT) |
74
|
|
|
{ |
75
|
54 |
|
$this->name = $name; |
76
|
|
|
|
77
|
54 |
|
if (!is_null($defaultValue)) { |
78
|
54 |
|
$this->sets++; |
79
|
54 |
|
$this->value = $defaultValue; |
80
|
|
|
} |
81
|
|
|
|
82
|
54 |
|
$this->type = $type; |
83
|
54 |
|
$this->branch = $branch; |
84
|
54 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Increases the read counter. |
88
|
|
|
* |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
52 |
|
public function incGets() |
92
|
|
|
{ |
93
|
52 |
|
return $this->gets++; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Increases the write counter. |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
26 |
|
public function incSets() |
102
|
|
|
{ |
103
|
26 |
|
return $this->sets++; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Gets the read counter. |
108
|
|
|
* |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
public function getGets() |
112
|
|
|
{ |
113
|
|
|
return $this->gets; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the write counter. |
118
|
|
|
* |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
|
|
public function getSets() |
122
|
|
|
{ |
123
|
|
|
return $this->sets; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
54 |
|
public function getName() |
130
|
|
|
{ |
131
|
54 |
|
return $this->name; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
23 |
|
public function getType() |
138
|
|
|
{ |
139
|
23 |
|
return $this->type; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getTypeName() |
146
|
|
|
{ |
147
|
|
|
return Types::getTypeName($this->type); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
23 |
|
public function getValue() |
154
|
|
|
{ |
155
|
23 |
|
return $this->value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Changes variable type. |
160
|
|
|
* |
161
|
|
|
* @param int $type |
162
|
|
|
*/ |
163
|
|
|
public function modifyType($type) |
164
|
|
|
{ |
165
|
|
|
$this->type = $type; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Changes variable type and value. |
170
|
|
|
* |
171
|
|
|
* @param int $type |
172
|
|
|
* @param mixed $value |
173
|
|
|
*/ |
174
|
5 |
|
public function modify($type, $value) |
175
|
|
|
{ |
176
|
5 |
|
$this->type = $type; |
177
|
5 |
|
$this->value = $value; |
178
|
|
|
|
179
|
5 |
|
if ($this->referencedTo) { |
180
|
|
|
$this->referencedTo->modify($type, $value); |
181
|
|
|
} |
182
|
5 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Increment uses for gets and sets |
186
|
|
|
*/ |
187
|
4 |
|
public function incUse() |
188
|
|
|
{ |
189
|
4 |
|
$this->incGets(); |
190
|
4 |
|
$this->incSets(); |
191
|
4 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Increment value of the variable |
195
|
|
|
*/ |
196
|
4 |
|
public function inc() |
197
|
|
|
{ |
198
|
4 |
|
$this->value++; |
199
|
4 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Decrement value of the variable |
203
|
|
|
*/ |
204
|
1 |
|
public function dec() |
205
|
|
|
{ |
206
|
1 |
|
$this->value--; |
207
|
1 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return boolean |
211
|
|
|
*/ |
212
|
|
|
public function isReferenced() |
213
|
|
|
{ |
214
|
|
|
return $this->referenced; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Is it an integer,double or number. |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function isNumeric() |
223
|
|
|
{ |
224
|
|
|
return ( |
225
|
|
|
$this->type & CompiledExpression::INTEGER || |
226
|
|
|
$this->type & CompiledExpression::DOUBLE || |
227
|
|
|
$this->type == CompiledExpression::NUMBER |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Check if you are setting values to variable but didn't use it (means get) |
233
|
|
|
* |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
50 |
|
public function isUnused() |
237
|
|
|
{ |
238
|
50 |
|
return $this->gets == 0 && $this->sets > 0; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return null|Variable |
243
|
|
|
*/ |
244
|
4 |
|
public function getReferencedTo() |
245
|
|
|
{ |
246
|
4 |
|
return $this->referencedTo; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param null|Variable $referencedTo |
251
|
|
|
*/ |
252
|
|
|
public function setReferencedTo(Variable $referencedTo = null) |
253
|
|
|
{ |
254
|
|
|
$this->referenced = true; |
255
|
|
|
$this->referencedTo = $referencedTo; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
11 |
|
public function getSymbolType() |
262
|
|
|
{ |
263
|
11 |
|
return 'variable'; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
//@codeCoverageIgnoreStart |
267
|
|
|
/** |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
|
|
public function __debugInfo() |
271
|
|
|
{ |
272
|
|
|
if ($this->value) { |
273
|
|
|
$value = 'Exists!'; |
274
|
|
|
} else { |
275
|
|
|
$value = 'Doest not exist'; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
switch (gettype($this->value)) { |
279
|
|
|
case 'integer': |
280
|
|
|
case 'double': |
281
|
|
|
$value = $this->value; |
282
|
|
|
break; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return [ |
286
|
|
|
'name' => $this->name, |
287
|
|
|
'type' => $this->type, |
288
|
|
|
'value' => [ |
289
|
|
|
'type' => gettype($this->value), |
290
|
|
|
'value' => $value |
291
|
|
|
], |
292
|
|
|
'branch' => $this->branch, |
293
|
|
|
'symbol-type' => $this->getSymbolType() |
294
|
|
|
]; |
295
|
|
|
} |
296
|
|
|
//@codeCoverageIgnoreEnd |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Is global variable? |
300
|
|
|
* |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
50 |
|
public function isGlobal() |
304
|
|
|
{ |
305
|
50 |
|
return false; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|