1 | <?php |
||
10 | class Variable |
||
11 | { |
||
12 | const BRANCH_ROOT = 0; |
||
13 | |||
14 | const BRANCH_CONDITIONAL_TRUE = 1; |
||
15 | |||
16 | const BRANCH_CONDITIONAL_FALSE = 2; |
||
17 | |||
18 | const BRANCH_CONDITIONAL_EXTERNAL = 3; |
||
19 | |||
20 | const BRANCH_UNKNOWN = 4; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $name; |
||
26 | |||
27 | /** |
||
28 | * @var mixed |
||
29 | */ |
||
30 | protected $value; |
||
31 | |||
32 | /** |
||
33 | * @var integer|string |
||
34 | */ |
||
35 | protected $branch; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $gets = 0; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $sets = 0; |
||
46 | |||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $referenced = false; |
||
51 | |||
52 | /** |
||
53 | * @var Variable|null |
||
54 | */ |
||
55 | protected $referencedTo; |
||
56 | |||
57 | /** |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $type; |
||
61 | |||
62 | /** |
||
63 | * @param string $name |
||
64 | * @param mixed $defaultValue |
||
65 | * @param int $type |
||
66 | * @param int|string $branch |
||
67 | */ |
||
68 | 1 | public function __construct($name, $defaultValue = null, $type = CompiledExpression::UNKNOWN, $branch = self::BRANCH_ROOT) |
|
69 | { |
||
70 | 1 | $this->name = $name; |
|
71 | |||
72 | 1 | if (!is_null($defaultValue)) { |
|
73 | 1 | $this->sets++; |
|
74 | 1 | $this->value = $defaultValue; |
|
75 | 1 | } |
|
76 | |||
77 | 1 | $this->type = (int) $type; |
|
78 | 1 | $this->branch = $branch; |
|
79 | 1 | } |
|
80 | |||
81 | /** |
||
82 | * @return int |
||
83 | */ |
||
84 | public function incGets() |
||
85 | { |
||
86 | return $this->gets++; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return int |
||
91 | */ |
||
92 | public function incSets() |
||
93 | { |
||
94 | return $this->sets++; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return int |
||
99 | */ |
||
100 | public function getGets() |
||
101 | { |
||
102 | return $this->gets; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return int |
||
107 | */ |
||
108 | public function getSets() |
||
109 | { |
||
110 | return $this->sets; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return mixed |
||
115 | */ |
||
116 | public function getName() |
||
117 | { |
||
118 | return $this->name; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return mixed |
||
123 | */ |
||
124 | 1 | public function getType() |
|
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getTypeName() |
||
133 | { |
||
134 | return Types::getTypeName($this->type); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return null |
||
139 | */ |
||
140 | 1 | public function getValue() |
|
144 | |||
145 | /** |
||
146 | * @param int $type |
||
147 | */ |
||
148 | public function modifyType($type) |
||
152 | |||
153 | /** |
||
154 | * @param int $type |
||
155 | * @param mixed $value |
||
156 | */ |
||
157 | public function modify($type, $value) |
||
158 | { |
||
159 | $this->type = (int) $type; |
||
160 | $this->value = $value; |
||
161 | |||
162 | if ($this->referencedTo) { |
||
163 | $this->referencedTo->modify($type, $value); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | public function incUse() |
||
168 | { |
||
169 | $this->incGets(); |
||
170 | $this->incSets(); |
||
171 | } |
||
172 | |||
173 | public function inc() |
||
177 | |||
178 | public function dec() |
||
182 | |||
183 | /** |
||
184 | * @return boolean |
||
185 | */ |
||
186 | public function isReferenced() |
||
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function isNumeric() |
||
195 | { |
||
196 | return ( |
||
197 | $this->type & CompiledExpression::INTEGER || |
||
198 | $this->type & CompiledExpression::DOUBLE || |
||
199 | $this->type == CompiledExpression::NUMBER |
||
200 | ); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Check if you are setting values to variable but didn't use it (mean get) |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | public function isUnused() |
||
212 | |||
213 | /** |
||
214 | * @return null|Variable |
||
215 | */ |
||
216 | public function getReferencedTo() |
||
220 | |||
221 | /** |
||
222 | * @param null|Variable $referencedTo |
||
223 | */ |
||
224 | public function setReferencedTo(Variable $referencedTo = null) |
||
225 | { |
||
226 | $this->referenced = true; |
||
227 | $this->referencedTo = $referencedTo; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return string |
||
232 | */ |
||
233 | public function getSymbolType() |
||
234 | { |
||
235 | return 'variable'; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @return array |
||
240 | */ |
||
241 | public function __debugInfo() |
||
253 | } |
||
254 |