Conditions | 40 |
Paths | 907 |
Total Lines | 250 |
Code Lines | 119 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 namespace CMPayments\JsonLint; |
||
157 | public function parse($input, $flags = 0) |
||
158 | { |
||
159 | $this->lexer = new Lexer(); |
||
160 | $this->lexer->setInput($input); |
||
161 | |||
162 | if (!is_string($input)) { |
||
163 | |||
164 | $e = new ParseException(ParseException::ERROR_NOT_A_STRING, [gettype($input)]); |
||
165 | |||
166 | // we +1 the line number on which an error occurred to make it human readable |
||
167 | $e->setJsonLineNo(($this->lexer->yLineNo + 1)); |
||
168 | |||
169 | throw $e; |
||
170 | } |
||
171 | |||
172 | $this->failOnBOM($input); |
||
173 | |||
174 | $this->flags = $flags; |
||
175 | |||
176 | $this->stack = [0]; |
||
177 | $this->vStack = [null]; |
||
178 | $this->lStack = []; |
||
179 | |||
180 | $yText = ''; |
||
181 | $yLineNo = $recovering = 0; |
||
182 | $eof = 1; |
||
183 | $terror = 2; |
||
184 | |||
185 | $yLocation = $this->lexer->yLocation; |
||
186 | $this->lStack[] = $yLocation; |
||
187 | |||
188 | $symbol = $preErrorSymbol = $p = null; |
||
189 | $yVal = new stdClass; |
||
190 | |||
191 | $isMultiLine = count(explode("\n", str_replace("\n\r", "\n", $input))) > 1; |
||
192 | |||
193 | while (true) { |
||
194 | |||
195 | // retrieve state number from top of stack |
||
196 | $state = $this->stack[count($this->stack) - 1]; |
||
197 | |||
198 | // use default actions if available |
||
199 | if (isset($this->defaultActions[$state])) { |
||
200 | |||
201 | $action = $this->defaultActions[$state]; |
||
202 | } else { |
||
203 | |||
204 | if ($symbol === null) { |
||
205 | |||
206 | $symbol = $this->lex(); |
||
207 | } |
||
208 | |||
209 | // read action for current state and first input |
||
210 | $action = isset($this->table[$state][$symbol]) ? $this->table[$state][$symbol] : false; |
||
211 | } |
||
212 | |||
213 | // handle parse error |
||
214 | if (!$action || !$action[0]) { |
||
215 | |||
216 | $e = null; |
||
217 | |||
218 | if (!$recovering) { |
||
219 | |||
220 | // Report error |
||
221 | $expected = []; |
||
222 | |||
223 | foreach ($this->table[$state] as $p => $ignore) { |
||
224 | |||
225 | if (isset($this->terminals[$p]) && $p > 2) { |
||
226 | |||
227 | $expected[] = $this->terminals[$p]; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | if (in_array("STRING", $expected) && in_array(substr($this->lexer->yText, 0, 1), ['"', "'"])) { |
||
232 | |||
233 | if (substr($this->lexer->yText, 0, 1) === "'") { |
||
234 | |||
235 | $e = new ParseException(ParseException::ERROR_USED_SINGLE_QUOTES, array_merge(['match' => $this->lexer->match], $this->getExceptionArguments($symbol))); |
||
236 | } elseif (preg_match('{".+?(\\\\[^"bfnrt/\\\\u])}', $this->lexer->getUpcomingInput())) { |
||
237 | |||
238 | $e = new ParseException(ParseException::ERROR_UNESCAPED_BACKSLASH, array_merge(['match' => $this->lexer->match], $this->getExceptionArguments($symbol))); |
||
239 | } elseif (preg_match('{"(?:[^"]+|\\\\")*$}m', $this->lexer->getUpcomingInput())) { |
||
240 | |||
241 | $e = new ParseException(ParseException::ERROR_NOT_TERMINATED_OR_MULTI_LINE, array_merge(['match' => $this->lexer->match], $this->getExceptionArguments($symbol))); |
||
242 | } else { |
||
243 | |||
244 | $e = new ParseException(ParseException::ERROR_INVALID_STRING, $this->getExceptionArguments($symbol)); |
||
245 | } |
||
246 | } |
||
247 | |||
248 | if (is_null($e)) { |
||
249 | |||
250 | $e = new ParseException(ParseException::ERROR_EXPECTED_INPUT_TO_BE_SOMETHING_ELSE, array_merge( |
||
251 | [ |
||
252 | ((count($expected) > 1) ? ' one of' : ''), |
||
253 | implode('\', \'', $expected), |
||
254 | utf8_encode($this->lexer->match) // encode any special characters that might be entered by the user |
||
255 | ], $this->getExceptionArguments($symbol) |
||
256 | )); |
||
257 | } |
||
258 | |||
259 | if (substr(trim($this->lexer->getPastInput()), -1) === ',') { |
||
260 | |||
261 | $e->appendMessage($e->getItemFromVariableArray(ParseException::ERROR_APPEND_TRAILING_COMMA_ERROR)); |
||
262 | |||
263 | // usually we +1 the line number on which an error occurred to make it human readable |
||
264 | // BUT when it involves a trailing comma the error is located on the line above the current one |
||
265 | // IF however the $input IS multi line, we do NOT increase the line number |
||
266 | $e->setJsonLineNo((($isMultiLine) ? $this->lexer->yLineNo : ($this->lexer->yLineNo + 1))); |
||
267 | } else { |
||
268 | |||
269 | // we +1 the line number on which an error occurred to make it human readable |
||
270 | $e->setJsonLineNo(($this->lexer->yLineNo + 1)); |
||
271 | } |
||
272 | |||
273 | $e->setJsonMatch($this->lexer->match); |
||
274 | $e->setJsonToken((!empty($this->terminals[$symbol]) ? $this->terminals[$symbol] : $symbol)); |
||
275 | $e->setJsonColumnNo($this->lexer->yColumnNo); |
||
276 | $e->setJsonExpected($expected); |
||
277 | |||
278 | throw $e; |
||
279 | } |
||
280 | |||
281 | // just recovered from another error |
||
282 | if ($recovering == 3) { |
||
283 | |||
284 | if ($symbol == $eof) { |
||
285 | |||
286 | if (is_null($e)) { |
||
287 | |||
288 | throw new ParseException(ParseException::ERROR_PARSING_HALTED); |
||
289 | } |
||
290 | } |
||
291 | |||
292 | // discard current lookahead and grab another |
||
293 | $yText = $this->lexer->yText; |
||
294 | $yLineNo = $this->lexer->yLineNo; |
||
295 | $symbol = $this->lex(); |
||
296 | } |
||
297 | |||
298 | // try to recover from error |
||
299 | while (true) { |
||
300 | |||
301 | // check for error recovery rule in this state |
||
302 | if (array_key_exists($terror, $this->table[$state])) { |
||
303 | |||
304 | break; |
||
305 | } |
||
306 | |||
307 | if ($state == 0) { |
||
308 | |||
309 | if (is_null($e)) { |
||
310 | |||
311 | throw new ParseException(ParseException::ERROR_PARSING_HALTED); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | $this->popStack(1); |
||
316 | $state = $this->stack[count($this->stack) - 1]; |
||
317 | } |
||
318 | |||
319 | // save the lookahead token |
||
320 | $preErrorSymbol = $symbol; |
||
321 | |||
322 | // insert generic error symbol as new lookahead |
||
323 | $symbol = $terror; |
||
324 | |||
325 | // allow 3 real symbols to be shifted before reporting a new error |
||
326 | $recovering = 3; |
||
327 | $state = $this->stack[count($this->stack) - 1]; |
||
328 | $action = isset($this->table[$state][$terror]) ? $this->table[$state][$terror] : false; |
||
329 | } |
||
330 | |||
331 | // this shouldn't happen, unless resolve defaults are off |
||
332 | if (is_array($action[0]) && is_array($action) && count($action) > 1) { |
||
333 | |||
334 | throw new ParseException(ParseException::ERROR_PARSING_ERROR_MULTIPLE_ACTIONS, [$state, $symbol]); |
||
335 | } |
||
336 | |||
337 | switch ($action[0]) { |
||
338 | |||
339 | // shift |
||
340 | case 1: |
||
341 | $this->stack[] = $symbol; |
||
342 | $this->vStack[] = $this->lexer->yText; |
||
343 | $this->lStack[] = $this->lexer->yLocation; |
||
344 | $this->stack[] = $action[1]; // push state |
||
345 | $symbol = null; |
||
346 | |||
347 | // normal execution/no error |
||
348 | if ($preErrorSymbol === null) { |
||
349 | |||
350 | $yText = $this->lexer->yText; |
||
351 | $yLineNo = $this->lexer->yLineNo; |
||
352 | |||
353 | if ($recovering > 0) { |
||
354 | |||
355 | $recovering--; |
||
356 | } |
||
357 | } else { |
||
358 | |||
359 | // error just occurred, resume old lookahead f/ before error |
||
360 | $symbol = $preErrorSymbol; |
||
361 | $preErrorSymbol = null; |
||
362 | } |
||
363 | break; |
||
364 | |||
365 | // reduce |
||
366 | case 2: |
||
367 | $len = $this->productions[$action[1]][1]; |
||
368 | |||
369 | // perform semantic action |
||
370 | $yVal->token = $this->vStack[count($this->vStack) - $len]; |
||
371 | |||
372 | // default location, uses first token for firsts, last for lasts |
||
373 | $yVal->store = [ |
||
374 | 'last_line' => $this->lStack[count($this->lStack) - 1]['last_line'], |
||
375 | 'last_column' => $this->lStack[count($this->lStack) - 1]['last_column'], |
||
376 | ]; |
||
377 | |||
378 | $result = $this->performAction($yVal, $yText, $yLineNo, $action[1], $this->vStack); |
||
379 | |||
380 | if (!$result instanceof UndefinedException) { |
||
381 | |||
382 | return $result; |
||
383 | } |
||
384 | |||
385 | if ($len) { |
||
386 | |||
387 | $this->popStack($len); |
||
388 | } |
||
389 | |||
390 | // push non terminal (reduce) |
||
391 | $this->stack[] = $this->productions[$action[1]][0]; |
||
392 | $this->vStack[] = $yVal->token; |
||
393 | $this->lStack[] = $yVal->store; |
||
394 | $newState = $this->table[$this->stack[count($this->stack) - 2]][$this->stack[count($this->stack) - 1]]; |
||
395 | $this->stack[] = $newState; |
||
396 | break; |
||
397 | |||
398 | // accept |
||
399 | case 3: |
||
|
|||
400 | |||
401 | return true; |
||
402 | } |
||
403 | } |
||
404 | |||
405 | return true; |
||
406 | } |
||
407 | |||
679 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.