Test Failed
Push — master ( b7ab7b...94ee03 )
by Kirill
02:59
created

VarSymbol::set()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 18
ccs 0
cts 13
cp 0
crap 20
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\IR\SymbolTable;
11
12
use Railt\SDL\Exception\TypeConflictException;
13
use Railt\SDL\IR\Type\TypeInterface;
14
15
/**
16
 * Class Record
17
 */
18
class VarSymbol implements VarSymbolInterface
19
{
20
    /**
21
     * @var bool
22
     */
23
    protected $const = true;
24
25
    /**
26
     * @var string
27
     */
28
    private $name;
29
30
    /**
31
     * @var null|TypeInterface
32
     */
33
    private $type;
34
35
    /**
36
     * @var ValueInterface|null
37
     */
38
    private $value;
39
40
    /**
41
     * Record constructor.
42
     * @param string $name
43
     * @param TypeInterface|null $type
44
     */
45
    public function __construct(string $name, TypeInterface $type = null)
46
    {
47
        $this->name = $name;
48
        $this->type = $type;
49
    }
50
51
    /**
52
     * @return null|ValueInterface
53
     */
54
    public function getValue(): ?ValueInterface
55
    {
56
        return $this->value;
57
    }
58
59
    /**
60
     * @param null|ValueInterface $value
61
     * @return VarSymbolInterface
62
     * @throws TypeConflictException
63
     */
64
    public function set(?ValueInterface $value): VarSymbolInterface
65
    {
66
        $allowed =
67
            // Not initialized variable or removing value
68
            $value === null ||
69
            $this->type === null ||
70
            // Or same type
71
            $value->getType()->typeOf($this->type);
72
73
        if ($allowed) {
74
            $this->value = $value;
75
76
            return $this;
77
        }
78
79
        $error = 'Can not set a new value of type %s into %s $%s';
80
        throw new TypeConflictException(\sprintf($error, $value->getType(), $this->type, $this->getName()));
81
    }
82
83
    /**
84
     * @return VarSymbolInterface
85
     */
86
    public function lock(): VarSymbolInterface
87
    {
88
        $this->const = true;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return VarSymbolInterface
95
     */
96
    public function unlock(): VarSymbolInterface
97
    {
98
        $this->const = false;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    public function isConstant(): bool
107
    {
108
        return $this->const;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getName(): string
115
    {
116
        return $this->name;
117
    }
118
119
    /**
120
     * @return TypeInterface
121
     */
122
    public function getType(): ?TypeInterface
123
    {
124
        return $this->type;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function __toString(): string
131
    {
132
        return \sprintf('<%s:%s> = %s', $this->getName(), $this->getType() ?? '?', $this->value ?? 'Null');
133
    }
134
}
135
136