|
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
|
882 |
|
public function __construct($name, $defaultValue = null, $type = CompiledExpression::UNKNOWN, $branch = self::BRANCH_ROOT) |
|
74
|
|
|
{ |
|
75
|
882 |
|
$this->name = $name; |
|
76
|
|
|
|
|
77
|
882 |
|
if (!is_null($defaultValue)) { |
|
78
|
882 |
|
$this->sets++; |
|
79
|
882 |
|
$this->value = $defaultValue; |
|
80
|
882 |
|
} |
|
81
|
|
|
|
|
82
|
882 |
|
$this->type = (int) $type; |
|
83
|
882 |
|
$this->branch = $branch; |
|
84
|
882 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Increases the read counter. |
|
88
|
|
|
* |
|
89
|
|
|
* @return int |
|
90
|
|
|
*/ |
|
91
|
88 |
|
public function incGets() |
|
92
|
|
|
{ |
|
93
|
88 |
|
return $this->gets++; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Increases the write counter. |
|
98
|
|
|
* |
|
99
|
|
|
* @return int |
|
100
|
|
|
*/ |
|
101
|
82 |
|
public function incSets() |
|
102
|
|
|
{ |
|
103
|
82 |
|
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
|
881 |
|
public function getName() |
|
130
|
|
|
{ |
|
131
|
881 |
|
return $this->name; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
57 |
|
public function getType() |
|
138
|
|
|
{ |
|
139
|
57 |
|
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
|
64 |
|
public function getValue() |
|
154
|
|
|
{ |
|
155
|
64 |
|
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 = (int) $type; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Changes variable type and value. |
|
170
|
|
|
* |
|
171
|
|
|
* @param int $type |
|
172
|
|
|
* @param mixed $value |
|
173
|
|
|
*/ |
|
174
|
6 |
|
public function modify($type, $value) |
|
175
|
|
|
{ |
|
176
|
6 |
|
$this->type = (int) $type; |
|
177
|
6 |
|
$this->value = $value; |
|
178
|
|
|
|
|
179
|
6 |
|
if ($this->referencedTo) { |
|
180
|
2 |
|
$this->referencedTo->modify($type, $value); |
|
181
|
2 |
|
} |
|
182
|
6 |
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Increment uses for gets and sets |
|
186
|
|
|
*/ |
|
187
|
45 |
|
public function incUse() |
|
188
|
|
|
{ |
|
189
|
45 |
|
$this->incGets(); |
|
190
|
45 |
|
$this->incSets(); |
|
191
|
45 |
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Increment value of the variable |
|
195
|
|
|
*/ |
|
196
|
20 |
|
public function inc() |
|
197
|
|
|
{ |
|
198
|
20 |
|
$this->value++; |
|
199
|
20 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Decrement value of the variable |
|
203
|
|
|
*/ |
|
204
|
17 |
|
public function dec() |
|
205
|
|
|
{ |
|
206
|
17 |
|
$this->value--; |
|
207
|
17 |
|
} |
|
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
|
44 |
|
public function isUnused() |
|
237
|
|
|
{ |
|
238
|
44 |
|
return $this->gets == 0 && $this->sets > 0; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return null|Variable |
|
243
|
|
|
*/ |
|
244
|
6 |
|
public function getReferencedTo() |
|
245
|
|
|
{ |
|
246
|
6 |
|
return $this->referencedTo; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param null|Variable $referencedTo |
|
251
|
|
|
*/ |
|
252
|
2 |
|
public function setReferencedTo(Variable $referencedTo = null) |
|
253
|
|
|
{ |
|
254
|
2 |
|
$this->referenced = true; |
|
255
|
2 |
|
$this->referencedTo = $referencedTo; |
|
256
|
2 |
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @return string |
|
260
|
|
|
*/ |
|
261
|
12 |
|
public function getSymbolType() |
|
262
|
|
|
{ |
|
263
|
12 |
|
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
|
|
|
|