Completed
Push — master ( 2dc9c0...7cf1b1 )
by Дмитрий
04:33
created

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