Completed
Push — master ( 530c00...13d490 )
by Дмитрий
02:40
created

Variable   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 82.86%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 26
c 3
b 0
f 1
lcom 2
cbo 1
dl 0
loc 244
ccs 58
cts 70
cp 0.8286
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A incGets() 0 4 1
A incSets() 0 4 1
A getGets() 0 4 1
A getSets() 0 4 1
A getName() 0 4 1
A getType() 0 4 1
A getValue() 0 4 1
A modifyType() 0 4 1
A inc() 0 4 1
A dec() 0 4 1
A isReferenced() 0 4 1
A isUnused() 0 4 2
A getReferencedTo() 0 4 1
A __construct() 0 12 2
A modify() 0 9 2
A incUse() 0 5 1
A isNumeric() 0 8 3
A setReferencedTo() 0 5 1
A __debugInfo() 0 12 1
A getSymbolType() 0 4 1
A getTypeName() 0 4 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 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 380
    public function __construct($name, $defaultValue = null, $type = CompiledExpression::UNKNOWN, $branch = self::BRANCH_ROOT)
69
    {
70 380
        $this->name = $name;
71
72 380
        if (!is_null($defaultValue)) {
73 376
            $this->sets++;
74 376
            $this->value = $defaultValue;
75 376
        }
76
77 380
        $this->type = (int) $type;
78 380
        $this->branch = $branch;
79 380
    }
80
81
    /**
82
     * @return int
83
     */
84 10
    public function incGets()
85
    {
86 10
        return $this->gets++;
87
    }
88
89
    /**
90
     * @return int
91
     */
92 11
    public function incSets()
93
    {
94 11
        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 367
    public function getName()
117
    {
118 367
        return $this->name;
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124 12
    public function getType()
125
    {
126 12
        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 13
    public function getValue()
141
    {
142 13
        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 1
    public function modify($type, $value)
158
    {
159 1
        $this->type = (int) $type;
160 1
        $this->value = $value;
161
162 1
        if ($this->referencedTo) {
163 1
            $this->referencedTo->modify($type, $value);
164 1
        }
165 1
    }
166
167 9
    public function incUse()
168
    {
169 9
        $this->incGets();
170 9
        $this->incSets();
171 9
    }
172
173 5
    public function inc()
174
    {
175 5
        $this->value++;
176 5
    }
177
178 5
    public function dec()
179
    {
180 5
        $this->value--;
181 5
    }
182
183
    /**
184
     * @return boolean
185
     */
186 1
    public function isReferenced()
187
    {
188 1
        return $this->referenced;
189
    }
190
191
    /**
192
     * @return bool
193
     */
194 3
    public function isNumeric()
195
    {
196
        return (
197 3
            $this->type & CompiledExpression::INTEGER ||
198 2
            $this->type & CompiledExpression::DOUBLE ||
199 1
            $this->type == CompiledExpression::NUMBER
200 3
        );
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 2
    public function isUnused()
209
    {
210 2
        return $this->gets == 0 && $this->sets > 0;
211
    }
212
213
    /**
214
     * @return null|Variable
215
     */
216 1
    public function getReferencedTo()
217
    {
218 1
        return $this->referencedTo;
219
    }
220
221
    /**
222
     * @param null|Variable $referencedTo
223
     */
224 1
    public function setReferencedTo(Variable $referencedTo = null)
225
    {
226 1
        $this->referenced = true;
227 1
        $this->referencedTo = $referencedTo;
228 1
    }
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()
242
    {
243
        return [
244
            'name' => $this->name,
245
            'type' => $this->type,
246
            'value' => [
247
                'type' => gettype($this->value)
248
            ],
249
            'branch' => $this->branch,
250
            'symbol-type' => $this->getSymbolType()
251
        ];
252
    }
253
}
254