Conditions | 24 |
Paths | 119 |
Total Lines | 114 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
103 | public function parse(string $type) |
||
104 | { |
||
105 | $this->lexer->setInput($type); |
||
106 | $this->lexer->moveNext(); |
||
107 | |||
108 | // Start off in the initial state and keep a stack of previous states |
||
109 | $state = 0; |
||
110 | $stateStack = [$state]; |
||
111 | $symbol = self::SYMBOL_NONE; |
||
112 | |||
113 | // Semantic value stack (contains values of tokens and semantic action results) |
||
114 | $this->semStack = []; |
||
115 | $this->semValue = null; |
||
116 | |||
117 | $stackPos = 0; |
||
118 | |||
119 | while (true) { |
||
120 | $this->traceNewState($state, $symbol); |
||
121 | if ($this->actionBase[$state] === 0) { |
||
122 | $rule = $this->actionDefault[$state]; |
||
123 | } else { |
||
124 | if ($symbol === self::SYMBOL_NONE) { |
||
125 | $this->lexer->moveNext(); |
||
126 | $tokenId = $this->lexer->token['type'] ?? 0; |
||
127 | $tokenValue = $this->lexer->token['value'] ?? null; |
||
128 | |||
129 | // map the lexer token id to the internally used symbols |
||
130 | $symbol = $tokenId >= 0 && $tokenId < $this->tokenToSymbolMapSize |
||
131 | ? $this->tokenToSymbol[$tokenId] |
||
132 | : $this->invalidSymbol; |
||
133 | |||
134 | if ($symbol === $this->invalidSymbol) { |
||
135 | throw new RangeException(sprintf( |
||
136 | 'The lexer returned an invalid token (id=%d, value=%s)', |
||
137 | $tokenId, |
||
138 | $tokenValue |
||
139 | )); |
||
140 | } |
||
141 | |||
142 | $this->traceRead($symbol, $tokenValue); |
||
143 | } |
||
144 | |||
145 | $idx = $this->actionBase[$state] + $symbol; |
||
146 | if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol) |
||
147 | || ($state < $this->YY2TBLSTATE |
||
148 | && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0 |
||
149 | && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol)) |
||
150 | && ($action = $this->action[$idx]) !== $this->defaultAction) { |
||
151 | if ($action > 0) { |
||
152 | /** shift */ |
||
153 | $this->traceShift($symbol); |
||
154 | |||
155 | ++$stackPos; |
||
156 | $stateStack[$stackPos] = $state = $action; |
||
157 | $this->semStack[$stackPos] = $tokenValue; |
||
158 | $symbol = self::SYMBOL_NONE; |
||
159 | |||
160 | if ($action < $this->numNonLeafStates) { |
||
161 | continue; |
||
162 | } |
||
163 | |||
164 | /* $yyn >= numNonLeafStates means shift-and-reduce */ |
||
165 | $rule = $action - $this->numNonLeafStates; |
||
166 | } else { |
||
167 | $rule = -$action; |
||
168 | } |
||
169 | } else { |
||
170 | $rule = $this->actionDefault[$state]; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | for (;;) { |
||
175 | if ($rule === 0) { |
||
176 | /* accept */ |
||
177 | $this->traceAccept(); |
||
178 | |||
179 | return $this->semValue; |
||
180 | } |
||
181 | |||
182 | if ($rule === $this->unexpectedTokenRule) { |
||
183 | /* error */ |
||
184 | $msg = $this->getErrorMessage($symbol, $state); |
||
185 | |||
186 | throw new RuntimeException($msg); |
||
187 | } |
||
188 | |||
189 | /* reduce */ |
||
190 | $this->traceReduce($rule); |
||
191 | $this->reduceCallbacks[$rule]($stackPos); |
||
192 | |||
193 | $stackPos -= $this->ruleToLength[$rule]; |
||
194 | $nonTerminal = $this->ruleToNonTerminal[$rule]; |
||
195 | $idx = $this->gotoBase[$nonTerminal] + $stateStack[$stackPos]; |
||
196 | if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] === $nonTerminal) { |
||
197 | $state = $this->goto[$idx]; |
||
198 | } else { |
||
199 | $state = $this->gotoDefault[$nonTerminal]; |
||
200 | } |
||
201 | |||
202 | ++$stackPos; |
||
203 | $stateStack[$stackPos] = $state; |
||
204 | $this->semStack[$stackPos] = $this->semValue; |
||
205 | |||
206 | if ($state < $this->numNonLeafStates) { |
||
207 | break; |
||
208 | } |
||
209 | |||
210 | /* >= numNonLeafStates means shift-and-reduce */ |
||
211 | $rule = $state - $this->numNonLeafStates; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | throw new RuntimeException('Reached end of parser loop'); |
||
216 | } |
||
217 | |||
319 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..