Completed
Push — master ( 6ab7bd...834806 )
by Дмитрий
05:03
created

Variable::incSets()   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
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 399
    public function __construct($name, $defaultValue = null, $type = CompiledExpression::UNKNOWN, $branch = self::BRANCH_ROOT)
69
    {
70 399
        $this->name = $name;
71
72 399
        if (!is_null($defaultValue)) {
73 395
            $this->sets++;
74 395
            $this->value = $defaultValue;
75 395
        }
76
77 399
        $this->type = (int) $type;
78 399
        $this->branch = $branch;
79 399
    }
80
81
    /**
82
     * @return int
83
     */
84 24
    public function incGets()
85
    {
86 24
        return $this->gets++;
87
    }
88
89
    /**
90
     * @return int
91
     */
92 18
    public function incSets()
93
    {
94 18
        return $this->sets++;
95
    }
96
97
    /**
98
     * @return int
99
     */
100 3
    public function getGets()
101
    {
102 3
        return $this->gets;
103
    }
104
105
    /**
106
     * @return int
107
     */
108 3
    public function getSets()
109
    {
110 3
        return $this->sets;
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116 386
    public function getName()
117
    {
118 386
        return $this->name;
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124 17
    public function getType()
125
    {
126 17
        return $this->type;
127
    }
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 18
    public function getValue()
141
    {
142 18
        return $this->value;
143
    }
144
145
    /**
146
     * @param int $type
147
     */
148 1
    public function modifyType($type)
149
    {
150 1
        $this->type = (int) $type;
151 1
    }
152
153
    /**
154
     * @param int $type
155
     * @param mixed $value
156
     */
157 4
    public function modify($type, $value)
158
    {
159 4
        $this->type = (int) $type;
160 4
        $this->value = $value;
161
162 4
        if ($this->referencedTo) {
163 3
            $this->referencedTo->modify($type, $value);
164 3
        }
165 4
    }
166
167
    /**
168
     * Increment uses for gets and sets
169
     */
170 10
    public function incUse()
171
    {
172 10
        $this->incGets();
173 10
        $this->incSets();
174 10
    }
175
176
    /**
177
     * Increment value of the variable
178
     */
179 5
    public function inc()
180
    {
181 5
        $this->value++;
182 5
    }
183
184
    /**
185
     * Decrement value of the variable
186
     */
187 5
    public function dec()
188
    {
189 5
        $this->value--;
190 5
    }
191
192
    /**
193
     * @return boolean
194
     */
195 1
    public function isReferenced()
196
    {
197 1
        return $this->referenced;
198
    }
199
200
    /**
201
     * @return bool
202
     */
203 3
    public function isNumeric()
204
    {
205
        return (
206 3
            $this->type & CompiledExpression::INTEGER ||
207 2
            $this->type & CompiledExpression::DOUBLE ||
208 1
            $this->type == CompiledExpression::NUMBER
209 3
        );
210
    }
211
212
    /**
213
     * Check if you are setting values to variable but didn't use it (mean get)
214
     *
215
     * @return bool
216
     */
217 14
    public function isUnused()
218
    {
219 14
        return $this->gets == 0 && $this->sets > 0;
220
    }
221
222
    /**
223
     * @return null|Variable
224
     */
225 4
    public function getReferencedTo()
226
    {
227 4
        return $this->referencedTo;
228
    }
229
230
    /**
231
     * @param null|Variable $referencedTo
232
     */
233 3
    public function setReferencedTo(Variable $referencedTo = null)
234
    {
235 3
        $this->referenced = true;
236 3
        $this->referencedTo = $referencedTo;
237 3
    }
238
239
    /**
240
     * @return string
241
     */
242 1
    public function getSymbolType()
243
    {
244 1
        return 'variable';
245
    }
246
247
    /**
248
     * @return array
249
     */
250
    public function __debugInfo()
251
    {
252
        if ($this->value) {
253
            $value = 'Exists!';
254
        } else {
255
            $value = 'Doest not exist';
256
        }
257
258
        switch (gettype($this->value)) {
259
            case 'integer':
260
            case 'double':
261
                $value = $this->value;
262
                break;
263
        }
264
265
        return [
266
            'name' => $this->name,
267
            'type' => $this->type,
268
            'value' => [
269
                'type' => gettype($this->value),
270
                'value' => $value
271
            ],
272
            'branch' => $this->branch,
273
            'symbol-type' => $this->getSymbolType()
274
        ];
275
    }
276
}
277