Symbol::__set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * This file is part of PHP-Yacc 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 PhpYacc\Grammar;
11
12
use PhpYacc\Exception\LogicException;
13
use PhpYacc\Yacc\Production;
14
15
/**
16
 * Class Symbol.
17
 *
18
 * @property null|Symbol $type
19
 * @property mixed $value
20
 * @property int $precedence
21
 * @property int $associativity
22
 * @property string $name
23
 * @property int $terminal
24
 */
25
class Symbol
26
{
27
    const UNDEF = 0;
28
    const LEFT = 1;
29
    const RIGHT = 2;
30
    const NON = 3;
31
    const MASK = 3;
32
    const TERMINAL = 0x100;
33
    const NON_TERMINAL = 0x200;
34
35
    /**
36
     * @var int
37
     */
38
    public $code;
39
40
    /**
41
     * @var null|Symbol
42
     */
43
    protected $_type;
44
45
    /**
46
     * @var mixed
47
     */
48
    protected $_value;
49
50
    /**
51
     * @var int
52
     */
53
    protected $_precedence;
54
55
    /**
56
     * @var int
57
     */
58
    protected $_associativity;
59
60
    /**
61
     * @var string
62
     */
63
    protected $_name;
64
65
    /**
66
     * @var bool
67
     */
68
    public $isTerminal = false;
69
70
    /**
71
     * @var bool
72
     */
73
    public $isnonterminal = false;
74
75
    /**
76
     * @var int
77
     */
78
    protected $_terminal = self::UNDEF;
79
80
    /**
81
     * Symbol constructor.
82
     *
83
     * @param int         $code
84
     * @param string      $name
85
     * @param null        $value
86
     * @param int         $terminal
87
     * @param int         $precedence
88
     * @param int         $associativity
89
     * @param Symbol|null $type
90
     *
91
     * @throws LogicException
92
     */
93
    public function __construct(int $code, string $name, $value = null, int $terminal = self::UNDEF, int $precedence = self::UNDEF, int $associativity = self::UNDEF, self $type = null)
94
    {
95
        $this->code = $code;
96
        $this->_name = $name;
97
        $this->_value = $value;
98
        $this->setTerminal($terminal);
99
        $this->_precedence = $precedence;
100
        $this->_associativity = $associativity;
101
        $this->_type = $type;
0 ignored issues
show
Documentation Bug introduced by
It seems like $type can also be of type object<self>. However, the property $_type is declared as type null|object<PhpYacc\Grammar\Symbol>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function isNilSymbol(): bool
108
    {
109
        return $this->_terminal === self::UNDEF;
110
    }
111
112
    /**
113
     * @param $name
114
     *
115
     * @return mixed
116
     */
117
    public function __get($name)
118
    {
119
        return $this->{'_'.$name};
120
    }
121
122
    /**
123
     * @param $name
124
     * @param $value
125
     */
126
    public function __set($name, $value)
127
    {
128
        $this->{'set'.$name}($value);
129
    }
130
131
    /**
132
     * @param int $terminal
133
     *
134
     * @throws LogicException
135
     */
136
    public function setTerminal(int $terminal)
137
    {
138
        $this->_terminal = $terminal;
139
        if ($terminal === self::TERMINAL) {
140
            $this->isTerminal = true;
141
            $this->isnonterminal = false;
142
        } elseif ($terminal === self::NON_TERMINAL) {
143
            $this->isTerminal = false;
144
            $this->isnonterminal = true;
145
        } else {
146
            $this->isTerminal = false;
147
            $this->isnonterminal = false;
148
        }
149
        $this->setValue($this->_value); // force check to prevent issues
150
    }
151
152
    /**
153
     * @param int $associativity
154
     */
155
    public function setAssociativity(int $associativity)
156
    {
157
        $this->_associativity = $associativity;
158
    }
159
160
    /**
161
     * @param int $precedence
162
     */
163
    public function setPrecedence(int $precedence)
164
    {
165
        $this->_precedence = $precedence;
166
    }
167
168
    /**
169
     * @param $value
170
     *
171
     * @throws LogicException
172
     */
173
    public function setValue($value)
174
    {
175
        if ($this->isTerminal && !is_int($value)) {
176
            throw new LogicException('Terminals value must be an integer, '.\gettype($value).' provided');
177
        } elseif ($this->isnonterminal && !($value instanceof Production || $value === null)) {
178
            throw new LogicException('NonTerminals value must be a production, '.\gettype($value).' provided');
179
        }
180
        $this->_value = $value;
181
    }
182
183
    /**
184
     * @param Symbol|null $type
185
     */
186
    public function setType(self $type = null)
187
    {
188
        $this->_type = $type;
0 ignored issues
show
Documentation Bug introduced by
It seems like $type can also be of type object<self>. However, the property $_type is declared as type null|object<PhpYacc\Grammar\Symbol>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
189
    }
190
191
    /**
192
     * @param int $flag
193
     */
194
    public function setAssociativityFlag(int $flag)
195
    {
196
        $this->_associativity |= $flag;
197
    }
198
}
199