Conditions | 33 |
Paths | 751 |
Total Lines | 126 |
Code Lines | 98 |
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 |
||
161 | protected function doGrammar() |
||
162 | { |
||
163 | $attribute = []; |
||
164 | $gbuffer = [null]; |
||
165 | $r = new Production('', 0); |
||
166 | |||
167 | $r->body = [$this->startPrime]; |
||
168 | $this->context->addGram($r); |
||
169 | |||
170 | $token = $this->lexer->getToken(); |
||
171 | |||
172 | while ($token->getType() !== Token::MARK && $token->getType() !== Token::EOF) { |
||
173 | if ($token->getType() === Token::NAME) { |
||
174 | if ($this->lexer->peek()->getValue()[0] === '@') { |
||
175 | $attribute[0] = $token->getValue(); |
||
176 | $this->lexer->getToken(); |
||
177 | $token = $this->lexer->getToken(); |
||
178 | } else { |
||
179 | $attribute[0] = null; |
||
180 | } |
||
181 | $gbuffer[0] = $this->context->internSymbol($token->getValue(), false); |
||
182 | $attribute[1] = null; |
||
183 | if ($gbuffer[0]->isterminal) { |
||
184 | throw new \RuntimeException("Nonterminal symbol expected: $token"); |
||
185 | } elseif (($tmp = $this->lexer->getToken())->getType() !== Token::COLON) { |
||
186 | throw new \RuntimeException("':' expected, $tmp found"); |
||
187 | } |
||
188 | if ($this->context->startSymbol === null) { |
||
189 | $this->context->startSymbol = $gbuffer[0]; |
||
190 | } |
||
191 | } elseif ($token->getValue()[0] === '|') { |
||
192 | if (!$gbuffer[0]) { |
||
193 | throw new \RuntimeException("Syntax Error, unexpected $token"); |
||
194 | } |
||
195 | $attribute[1] = null; |
||
196 | } elseif ($token->getType() === Token::BEGININC) { |
||
197 | $this->doCopy(); |
||
198 | $token = $this->lexer->getToken(); |
||
199 | continue; |
||
200 | } else { |
||
201 | throw new \RuntimeException("Syntax Error Unexpected $token"); |
||
202 | } |
||
203 | |||
204 | $lastTerm = $this->startPrime; |
||
205 | $action = null; |
||
206 | $pos = 0; |
||
207 | $i = 1; |
||
208 | while (true) { |
||
209 | $token = $this->lexer->getToken(); |
||
210 | |||
211 | if ($token->getValue()[0] === '=') { |
||
212 | $pos = $token->getLine(); |
||
213 | if (($token = $this->lexer->getToken())->getValue()[0] === '{') { |
||
214 | $pos = $token->getLine(); |
||
215 | $action = $this->copyAction($gbuffer, $i - 1, '}', $attribute); |
||
216 | } else { |
||
217 | $this->lexer->ungetToken(); |
||
218 | $action = $this->copyAction($gbuffer, $i - 1, ';', $attribute); |
||
219 | } |
||
220 | } elseif ($token->getValue()[0] === '{') { |
||
221 | $pos = $token->getLine(); |
||
222 | $action = $this->copyAction($gbuffer, $i - 1, '}', $attribute); |
||
223 | } elseif ($token->getType() === Token::PRECTOK) { |
||
224 | $lastTerm = $this->context->internSymbol($this->lexer->getToken()->getValue(), false); |
||
225 | } elseif ($token->getType() === Token::NAME && $this->lexer->peek()->getType() === Token::COLON) { |
||
226 | break; |
||
227 | } elseif ($token->getType() === Token::NAME && $this->lexer->peek()->getValue()[0] === '@') { |
||
228 | $attribute[$i] = $token->getValue(); |
||
229 | $this->lexer->getToken(); |
||
230 | } elseif ($token->getType() === Token::NAME || $token->getType() === Token::STRING) { |
||
231 | if ($action) { |
||
|
|||
232 | $g = $this->context->genNonTerminal(); |
||
233 | $r = new Production($action, $pos); |
||
234 | $r->body = [$g]; |
||
235 | $gbuffer[$i++] = $g; |
||
236 | $attribute[$i] = null; |
||
237 | $r->link = $r->body[0]->value; |
||
238 | $g->value = $this->context->addGram($r); |
||
239 | } |
||
240 | $gbuffer[$i++] = $w = $this->context->internSymbol($token->getValue(), false); |
||
241 | $attribute[$i] = null; |
||
242 | if ($w->isterminal) { |
||
243 | $lastTerm = $w; |
||
244 | } |
||
245 | $action = null; |
||
246 | } else { |
||
247 | break; |
||
248 | } |
||
249 | } |
||
250 | if (!$action) { |
||
251 | if ($i > 1 && $gbuffer[0]->type !== null && $gbuffer[0]->type !== $gbuffer[1]->type) { |
||
252 | throw new ParseException('Stack types are different'); |
||
253 | } |
||
254 | } |
||
255 | $r = new Production($action, $pos); |
||
256 | |||
257 | $r->body = \array_slice($gbuffer, 0, $i); |
||
258 | $r->precedence = $lastTerm->precedence; |
||
259 | $r->associativity = $lastTerm->associativity & Symbol::MASK; |
||
260 | $r->link = $r->body[0]->value; |
||
261 | $gbuffer[0]->value = $this->context->addGram($r); |
||
262 | |||
263 | if ($token->getType() === Token::SEMICOLON) { |
||
264 | $token = $this->lexer->getToken(); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | $this->context->gram(0)->body[] = $this->context->startSymbol; |
||
269 | $this->startPrime->value = null; |
||
270 | foreach ($this->context->nonterminals as $key => $symbol) { |
||
271 | if ($symbol === $this->startPrime) { |
||
272 | continue; |
||
273 | } |
||
274 | if (($j = $symbol->value) === null) { |
||
275 | throw new ParseException("Nonterminal {$symbol->name} used but not defined"); |
||
276 | } |
||
277 | $k = null; |
||
278 | while ($j) { |
||
279 | $w = $j->link; |
||
280 | $j->link = $k; |
||
281 | $k = $j; |
||
282 | $j = $w; |
||
283 | } |
||
284 | $symbol->value = $k; |
||
285 | } |
||
286 | } |
||
287 | |||
474 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: