Completed
Pull Request — master (#115)
by Enrico
11:24
created

Variable::isReferenced()   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

Importance

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