Conditions | 117 |
Paths | > 20000 |
Total Lines | 602 |
Code Lines | 339 |
Lines | 40 |
Ratio | 6.64 % |
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 |
||
268 | public function tokenize($string) |
||
269 | { |
||
270 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
271 | echo "\t*** START JS TOKENIZING ***".PHP_EOL; |
||
272 | } |
||
273 | |||
274 | $maxTokenLength = 0; |
||
275 | foreach ($this->tokenValues as $token => $values) { |
||
276 | if (strlen($token) > $maxTokenLength) { |
||
277 | $maxTokenLength = strlen($token); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | $tokens = array(); |
||
282 | $inString = ''; |
||
283 | $stringChar = null; |
||
284 | $inComment = ''; |
||
285 | $buffer = ''; |
||
286 | $preStringBuffer = ''; |
||
287 | $cleanBuffer = false; |
||
288 | |||
289 | $commentTokenizer = new Comment(); |
||
290 | |||
291 | $tokens[] = array( |
||
292 | 'code' => T_OPEN_TAG, |
||
293 | 'type' => 'T_OPEN_TAG', |
||
294 | 'content' => '', |
||
295 | ); |
||
296 | |||
297 | // Convert newlines to single characters for ease of |
||
298 | // processing. We will change them back later. |
||
299 | $string = str_replace($this->eolChar, "\n", $string); |
||
300 | |||
301 | $chars = str_split($string); |
||
302 | $numChars = count($chars); |
||
303 | for ($i = 0; $i < $numChars; $i++) { |
||
304 | $char = $chars[$i]; |
||
305 | |||
306 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
307 | $content = Util\Common::prepareForOutput($char); |
||
308 | $bufferContent = Util\Common::prepareForOutput($buffer); |
||
309 | |||
310 | if ($inString !== '') { |
||
311 | echo "\t"; |
||
312 | } |
||
313 | |||
314 | if ($inComment !== '') { |
||
315 | echo "\t"; |
||
316 | } |
||
317 | |||
318 | echo "\tProcess char $i => $content (buffer: $bufferContent)".PHP_EOL; |
||
319 | }//end if |
||
320 | |||
321 | if ($inString === '' && $inComment === '' && $buffer !== '') { |
||
322 | // If the buffer only has whitespace and we are about to |
||
323 | // add a character, store the whitespace first. |
||
324 | View Code Duplication | if (trim($char) !== '' && trim($buffer) === '') { |
|
325 | $tokens[] = array( |
||
326 | 'code' => T_WHITESPACE, |
||
327 | 'type' => 'T_WHITESPACE', |
||
328 | 'content' => str_replace("\n", $this->eolChar, $buffer), |
||
329 | ); |
||
330 | |||
331 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
332 | $content = Util\Common::prepareForOutput($buffer); |
||
333 | echo "\t=> Added token T_WHITESPACE ($content)".PHP_EOL; |
||
334 | } |
||
335 | |||
336 | $buffer = ''; |
||
337 | } |
||
338 | |||
339 | // If the buffer is not whitespace and we are about to |
||
340 | // add a whitespace character, store the content first. |
||
341 | if ($inString === '' |
||
342 | && $inComment === '' |
||
343 | && trim($char) === '' |
||
344 | && trim($buffer) !== '' |
||
345 | ) { |
||
346 | $tokens[] = array( |
||
347 | 'code' => T_STRING, |
||
348 | 'type' => 'T_STRING', |
||
349 | 'content' => str_replace("\n", $this->eolChar, $buffer), |
||
350 | ); |
||
351 | |||
352 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
353 | $content = Util\Common::prepareForOutput($buffer); |
||
354 | echo "\t=> Added token T_STRING ($content)".PHP_EOL; |
||
355 | } |
||
356 | |||
357 | $buffer = ''; |
||
358 | } |
||
359 | }//end if |
||
360 | |||
361 | // Process strings. |
||
362 | if ($inComment === '' && isset($this->stringTokens[$char]) === true) { |
||
363 | if ($inString === $char) { |
||
364 | // This could be the end of the string, but make sure it |
||
365 | // is not escaped first. |
||
366 | $escapes = 0; |
||
367 | for ($x = ($i - 1); $x >= 0; $x--) { |
||
368 | if ($chars[$x] !== '\\') { |
||
369 | break; |
||
370 | } |
||
371 | |||
372 | $escapes++; |
||
373 | } |
||
374 | |||
375 | if ($escapes === 0 || ($escapes % 2) === 0) { |
||
376 | // There is an even number escape chars, |
||
377 | // so this is not escaped, it is the end of the string. |
||
378 | $tokens[] = array( |
||
379 | 'code' => T_CONSTANT_ENCAPSED_STRING, |
||
380 | 'type' => 'T_CONSTANT_ENCAPSED_STRING', |
||
381 | 'content' => str_replace("\n", $this->eolChar, $buffer).$char, |
||
382 | ); |
||
383 | |||
384 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
385 | echo "\t\t* found end of string *".PHP_EOL; |
||
386 | $content = Util\Common::prepareForOutput($buffer.$char); |
||
387 | echo "\t=> Added token T_CONSTANT_ENCAPSED_STRING ($content)".PHP_EOL; |
||
388 | } |
||
389 | |||
390 | $buffer = ''; |
||
391 | $preStringBuffer = ''; |
||
392 | $inString = ''; |
||
393 | $stringChar = null; |
||
394 | continue; |
||
395 | }//end if |
||
396 | } else if ($inString === '') { |
||
397 | $inString = $char; |
||
398 | $stringChar = $i; |
||
399 | $preStringBuffer = $buffer; |
||
400 | |||
401 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
402 | echo "\t\t* looking for string closer *".PHP_EOL; |
||
403 | } |
||
404 | }//end if |
||
405 | }//end if |
||
406 | |||
407 | if ($inString !== '' && $char === "\n") { |
||
408 | // Unless this newline character is escaped, the string did not |
||
409 | // end before the end of the line, which means it probably |
||
410 | // wasn't a string at all (maybe a regex). |
||
411 | if ($chars[($i - 1)] !== '\\') { |
||
412 | $i = $stringChar; |
||
413 | $buffer = $preStringBuffer; |
||
414 | $preStringBuffer = ''; |
||
415 | $inString = ''; |
||
416 | $stringChar = null; |
||
417 | $char = $chars[$i]; |
||
418 | |||
419 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
420 | echo "\t\t* found newline before end of string, bailing *".PHP_EOL; |
||
421 | } |
||
422 | } |
||
423 | } |
||
424 | |||
425 | $buffer .= $char; |
||
426 | |||
427 | // We don't look for special tokens inside strings, |
||
428 | // so if we are in a string, we can continue here now |
||
429 | // that the current char is in the buffer. |
||
430 | if ($inString !== '') { |
||
431 | continue; |
||
432 | } |
||
433 | |||
434 | // Special case for T_DIVIDE which can actually be |
||
435 | // the start of a regular expression. |
||
436 | if ($buffer === $char && $char === '/' && $chars[($i + 1)] !== '*') { |
||
437 | $regex = $this->getRegexToken( |
||
438 | $i, |
||
439 | $string, |
||
440 | $chars, |
||
441 | $tokens, |
||
442 | $this->eolChar |
||
443 | ); |
||
444 | |||
445 | if ($regex !== null) { |
||
446 | $tokens[] = array( |
||
447 | 'code' => T_REGULAR_EXPRESSION, |
||
448 | 'type' => 'T_REGULAR_EXPRESSION', |
||
449 | 'content' => $regex['content'], |
||
450 | ); |
||
451 | |||
452 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
453 | $content = Util\Common::prepareForOutput($regex['content']); |
||
454 | echo "\t=> Added token T_REGULAR_EXPRESSION ($content)".PHP_EOL; |
||
455 | } |
||
456 | |||
457 | $i = $regex['end']; |
||
458 | $buffer = ''; |
||
459 | $cleanBuffer = false; |
||
460 | continue; |
||
461 | }//end if |
||
462 | }//end if |
||
463 | |||
464 | // Check for known tokens, but ignore tokens found that are not at |
||
465 | // the end of a string, like FOR and this.FORmat. |
||
466 | if (isset($this->tokenValues[strtolower($buffer)]) === true |
||
467 | && (preg_match('|[a-zA-z0-9_]|', $char) === 0 |
||
468 | || isset($chars[($i + 1)]) === false |
||
469 | || preg_match('|[a-zA-z0-9_]|', $chars[($i + 1)]) === 0) |
||
470 | ) { |
||
471 | $matchedToken = false; |
||
472 | $lookAheadLength = ($maxTokenLength - strlen($buffer)); |
||
473 | |||
474 | if ($lookAheadLength > 0) { |
||
475 | // The buffer contains a token type, but we need |
||
476 | // to look ahead at the next chars to see if this is |
||
477 | // actually part of a larger token. For example, |
||
478 | // FOR and FOREACH. |
||
479 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
480 | echo "\t\t* buffer possibly contains token, looking ahead $lookAheadLength chars *".PHP_EOL; |
||
481 | } |
||
482 | |||
483 | $charBuffer = $buffer; |
||
484 | for ($x = 1; $x <= $lookAheadLength; $x++) { |
||
485 | if (isset($chars[($i + $x)]) === false) { |
||
486 | break; |
||
487 | } |
||
488 | |||
489 | $charBuffer .= $chars[($i + $x)]; |
||
490 | |||
491 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
492 | $content = Util\Common::prepareForOutput($charBuffer); |
||
493 | echo "\t\t=> Looking ahead $x chars => $content".PHP_EOL; |
||
494 | } |
||
495 | |||
496 | if (isset($this->tokenValues[strtolower($charBuffer)]) === true) { |
||
497 | // We've found something larger that matches |
||
498 | // so we can ignore this char. Except for 1 very specific |
||
499 | // case where a comment like /**/ needs to tokenize as |
||
500 | // T_COMMENT and not T_DOC_COMMENT. |
||
501 | $oldType = $this->tokenValues[strtolower($buffer)]; |
||
502 | $newType = $this->tokenValues[strtolower($charBuffer)]; |
||
503 | if ($oldType === 'T_COMMENT' |
||
504 | && $newType === 'T_DOC_COMMENT' |
||
505 | && $chars[($i + $x + 1)] === '/' |
||
506 | ) { |
||
507 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
508 | echo "\t\t* look ahead ignored T_DOC_COMMENT, continuing *".PHP_EOL; |
||
509 | } |
||
510 | } else { |
||
511 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
512 | echo "\t\t* look ahead found more specific token ($newType), ignoring $i *".PHP_EOL; |
||
513 | } |
||
514 | |||
515 | $matchedToken = true; |
||
516 | break; |
||
517 | } |
||
518 | }//end if |
||
519 | }//end for |
||
520 | }//end if |
||
521 | |||
522 | if ($matchedToken === false) { |
||
523 | if (PHP_CodeSniffer_VERBOSITY > 1 && $lookAheadLength > 0) { |
||
524 | echo "\t\t* look ahead found nothing *".PHP_EOL; |
||
525 | } |
||
526 | |||
527 | $value = $this->tokenValues[strtolower($buffer)]; |
||
528 | $tokens[] = array( |
||
529 | 'code' => constant($value), |
||
530 | 'type' => $value, |
||
531 | 'content' => $buffer, |
||
532 | ); |
||
533 | |||
534 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
535 | $content = Util\Common::prepareForOutput($buffer); |
||
536 | echo "\t=> Added token $value ($content)".PHP_EOL; |
||
537 | } |
||
538 | |||
539 | $cleanBuffer = true; |
||
540 | }//end if |
||
541 | } else if (isset($this->tokenValues[strtolower($char)]) === true) { |
||
542 | // No matter what token we end up using, we don't |
||
543 | // need the content in the buffer any more because we have |
||
544 | // found a valid token. |
||
545 | $newContent = substr(str_replace("\n", $this->eolChar, $buffer), 0, -1); |
||
546 | if ($newContent !== '') { |
||
547 | $tokens[] = array( |
||
548 | 'code' => T_STRING, |
||
549 | 'type' => 'T_STRING', |
||
550 | 'content' => $newContent, |
||
551 | ); |
||
552 | |||
553 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
554 | $content = Util\Common::prepareForOutput(substr($buffer, 0, -1)); |
||
555 | echo "\t=> Added token T_STRING ($content)".PHP_EOL; |
||
556 | } |
||
557 | } |
||
558 | |||
559 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
560 | echo "\t\t* char is token, looking ahead ".($maxTokenLength - 1).' chars *'.PHP_EOL; |
||
561 | } |
||
562 | |||
563 | // The char is a token type, but we need to look ahead at the |
||
564 | // next chars to see if this is actually part of a larger token. |
||
565 | // For example, = and ===. |
||
566 | $charBuffer = $char; |
||
567 | $matchedToken = false; |
||
568 | for ($x = 1; $x <= $maxTokenLength; $x++) { |
||
569 | if (isset($chars[($i + $x)]) === false) { |
||
570 | break; |
||
571 | } |
||
572 | |||
573 | $charBuffer .= $chars[($i + $x)]; |
||
574 | |||
575 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
576 | $content = Util\Common::prepareForOutput($charBuffer); |
||
577 | echo "\t\t=> Looking ahead $x chars => $content".PHP_EOL; |
||
578 | } |
||
579 | |||
580 | if (isset($this->tokenValues[strtolower($charBuffer)]) === true) { |
||
581 | // We've found something larger that matches |
||
582 | // so we can ignore this char. |
||
583 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
584 | $type = $this->tokenValues[strtolower($charBuffer)]; |
||
585 | echo "\t\t* look ahead found more specific token ($type), ignoring $i *".PHP_EOL; |
||
586 | } |
||
587 | |||
588 | $matchedToken = true; |
||
589 | break; |
||
590 | } |
||
591 | }//end for |
||
592 | |||
593 | if ($matchedToken === false) { |
||
594 | $value = $this->tokenValues[strtolower($char)]; |
||
595 | $tokens[] = array( |
||
596 | 'code' => constant($value), |
||
597 | 'type' => $value, |
||
598 | 'content' => $char, |
||
599 | ); |
||
600 | |||
601 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
602 | echo "\t\t* look ahead found nothing *".PHP_EOL; |
||
603 | $content = Util\Common::prepareForOutput($char); |
||
604 | echo "\t=> Added token $value ($content)".PHP_EOL; |
||
605 | } |
||
606 | |||
607 | $cleanBuffer = true; |
||
608 | } else { |
||
609 | $buffer = $char; |
||
610 | }//end if |
||
611 | }//end if |
||
612 | |||
613 | // Keep track of content inside comments. |
||
614 | if ($inComment === '' |
||
615 | && array_key_exists($buffer, $this->commentTokens) === true |
||
616 | ) { |
||
617 | // This is not really a comment if the content |
||
618 | // looks like \// (i.e., it is escaped). |
||
619 | if (isset($chars[($i - 2)]) === true && $chars[($i - 2)] === '\\') { |
||
620 | $lastToken = array_pop($tokens); |
||
621 | $lastContent = $lastToken['content']; |
||
622 | View Code Duplication | if (PHP_CodeSniffer_VERBOSITY > 1) { |
|
623 | $value = $this->tokenValues[strtolower($lastContent)]; |
||
624 | $content = Util\Common::prepareForOutput($lastContent); |
||
625 | echo "\t=> Removed token $value ($content)".PHP_EOL; |
||
626 | } |
||
627 | |||
628 | $lastChars = str_split($lastContent); |
||
629 | $lastNumChars = count($lastChars); |
||
630 | for ($x = 0; $x < $lastNumChars; $x++) { |
||
631 | $lastChar = $lastChars[$x]; |
||
632 | $value = $this->tokenValues[strtolower($lastChar)]; |
||
633 | $tokens[] = array( |
||
634 | 'code' => constant($value), |
||
635 | 'type' => $value, |
||
636 | 'content' => $lastChar, |
||
637 | ); |
||
638 | |||
639 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
640 | $content = Util\Common::prepareForOutput($lastChar); |
||
641 | echo "\t=> Added token $value ($content)".PHP_EOL; |
||
642 | } |
||
643 | } |
||
644 | } else { |
||
645 | // We have started a comment. |
||
646 | $inComment = $buffer; |
||
647 | |||
648 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
649 | echo "\t\t* looking for end of comment *".PHP_EOL; |
||
650 | } |
||
651 | }//end if |
||
652 | } else if ($inComment !== '') { |
||
653 | if ($this->commentTokens[$inComment] === null) { |
||
654 | // Comment ends at the next newline. |
||
655 | if (strpos($buffer, "\n") !== false) { |
||
656 | $inComment = ''; |
||
657 | } |
||
658 | } else { |
||
659 | if ($this->commentTokens[$inComment] === $buffer) { |
||
660 | $inComment = ''; |
||
661 | } |
||
662 | } |
||
663 | |||
664 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
665 | if ($inComment === '') { |
||
666 | echo "\t\t* found end of comment *".PHP_EOL; |
||
667 | } |
||
668 | } |
||
669 | |||
670 | View Code Duplication | if ($inComment === '' && $cleanBuffer === false) { |
|
671 | $tokens[] = array( |
||
672 | 'code' => T_STRING, |
||
673 | 'type' => 'T_STRING', |
||
674 | 'content' => str_replace("\n", $this->eolChar, $buffer), |
||
675 | ); |
||
676 | |||
677 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
678 | $content = Util\Common::prepareForOutput($buffer); |
||
679 | echo "\t=> Added token T_STRING ($content)".PHP_EOL; |
||
680 | } |
||
681 | |||
682 | $buffer = ''; |
||
683 | } |
||
684 | }//end if |
||
685 | |||
686 | if ($cleanBuffer === true) { |
||
687 | $buffer = ''; |
||
688 | $cleanBuffer = false; |
||
689 | } |
||
690 | }//end for |
||
691 | |||
692 | if (empty($buffer) === false) { |
||
693 | // Buffer contains whitespace from the end of the file. |
||
694 | $tokens[] = array( |
||
695 | 'code' => T_WHITESPACE, |
||
696 | 'type' => 'T_WHITESPACE', |
||
697 | 'content' => str_replace("\n", $this->eolChar, $buffer), |
||
698 | ); |
||
699 | |||
700 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
701 | $content = Util\Common::prepareForOutput($buffer); |
||
702 | echo "\t=> Added token T_WHITESPACE ($content)".PHP_EOL; |
||
703 | } |
||
704 | } |
||
705 | |||
706 | $tokens[] = array( |
||
707 | 'code' => T_CLOSE_TAG, |
||
708 | 'type' => 'T_CLOSE_TAG', |
||
709 | 'content' => '', |
||
710 | ); |
||
711 | |||
712 | /* |
||
713 | Now that we have done some basic tokenizing, we need to |
||
714 | modify the tokens to join some together and split some apart |
||
715 | so they match what the PHP tokenizer does. |
||
716 | */ |
||
717 | |||
718 | $finalTokens = array(); |
||
719 | $newStackPtr = 0; |
||
720 | $numTokens = count($tokens); |
||
721 | for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) { |
||
722 | $token = $tokens[$stackPtr]; |
||
723 | |||
724 | /* |
||
725 | Look for comments and join the tokens together. |
||
726 | */ |
||
727 | |||
728 | if ($token['code'] === T_COMMENT || $token['code'] === T_DOC_COMMENT) { |
||
729 | $newContent = ''; |
||
730 | $tokenContent = $token['content']; |
||
731 | $endContent = $this->commentTokens[$tokenContent]; |
||
732 | while ($tokenContent !== $endContent) { |
||
733 | if ($endContent === null |
||
734 | && strpos($tokenContent, $this->eolChar) !== false |
||
735 | ) { |
||
736 | // A null end token means the comment ends at the end of |
||
737 | // the line so we look for newlines and split the token. |
||
738 | $tokens[$stackPtr]['content'] = substr( |
||
739 | $tokenContent, |
||
740 | (strpos($tokenContent, $this->eolChar) + strlen($this->eolChar)) |
||
741 | ); |
||
742 | |||
743 | $tokenContent = substr( |
||
744 | $tokenContent, |
||
745 | 0, |
||
746 | (strpos($tokenContent, $this->eolChar) + strlen($this->eolChar)) |
||
747 | ); |
||
748 | |||
749 | // If the substr failed, skip the token as the content |
||
750 | // will now be blank. |
||
751 | if ($tokens[$stackPtr]['content'] !== false |
||
752 | && $tokens[$stackPtr]['content'] !== '' |
||
753 | ) { |
||
754 | $stackPtr--; |
||
755 | } |
||
756 | |||
757 | break; |
||
758 | }//end if |
||
759 | |||
760 | $stackPtr++; |
||
761 | $newContent .= $tokenContent; |
||
762 | if (isset($tokens[$stackPtr]) === false) { |
||
763 | break; |
||
764 | } |
||
765 | |||
766 | $tokenContent = $tokens[$stackPtr]['content']; |
||
767 | }//end while |
||
768 | |||
769 | if ($token['code'] === T_DOC_COMMENT) { |
||
770 | $commentTokens = $commentTokenizer->tokenizeString($newContent.$tokenContent, $this->eolChar, $newStackPtr); |
||
771 | foreach ($commentTokens as $commentToken) { |
||
772 | $finalTokens[$newStackPtr] = $commentToken; |
||
773 | $newStackPtr++; |
||
774 | } |
||
775 | |||
776 | continue; |
||
777 | } else { |
||
778 | // Save the new content in the current token so |
||
779 | // the code below can chop it up on newlines. |
||
780 | $token['content'] = $newContent.$tokenContent; |
||
781 | } |
||
782 | }//end if |
||
783 | |||
784 | /* |
||
785 | If this token has newlines in its content, split each line up |
||
786 | and create a new token for each line. We do this so it's easier |
||
787 | to ascertain where errors occur on a line. |
||
788 | Note that $token[1] is the token's content. |
||
789 | */ |
||
790 | |||
791 | if (strpos($token['content'], $this->eolChar) !== false) { |
||
792 | $tokenLines = explode($this->eolChar, $token['content']); |
||
793 | $numLines = count($tokenLines); |
||
794 | |||
795 | for ($i = 0; $i < $numLines; $i++) { |
||
796 | $newToken['content'] = $tokenLines[$i]; |
||
797 | View Code Duplication | if ($i === ($numLines - 1)) { |
|
798 | if ($tokenLines[$i] === '') { |
||
799 | break; |
||
800 | } |
||
801 | } else { |
||
802 | $newToken['content'] .= $this->eolChar; |
||
803 | } |
||
804 | |||
805 | $newToken['type'] = $token['type']; |
||
806 | $newToken['code'] = $token['code']; |
||
807 | $finalTokens[$newStackPtr] = $newToken; |
||
808 | $newStackPtr++; |
||
809 | } |
||
810 | } else { |
||
811 | $finalTokens[$newStackPtr] = $token; |
||
812 | $newStackPtr++; |
||
813 | }//end if |
||
814 | |||
815 | // Convert numbers, including decimals. |
||
816 | if ($token['code'] === T_STRING |
||
817 | || $token['code'] === T_OBJECT_OPERATOR |
||
818 | ) { |
||
819 | $newContent = ''; |
||
820 | $oldStackPtr = $stackPtr; |
||
821 | while (preg_match('|^[0-9\.]+$|', $tokens[$stackPtr]['content']) !== 0) { |
||
822 | $newContent .= $tokens[$stackPtr]['content']; |
||
823 | $stackPtr++; |
||
824 | } |
||
825 | |||
826 | if ($newContent !== '' && $newContent !== '.') { |
||
827 | $finalTokens[($newStackPtr - 1)]['content'] = $newContent; |
||
828 | if (ctype_digit($newContent) === true) { |
||
829 | $finalTokens[($newStackPtr - 1)]['code'] = constant('T_LNUMBER'); |
||
830 | $finalTokens[($newStackPtr - 1)]['type'] = 'T_LNUMBER'; |
||
831 | } else { |
||
832 | $finalTokens[($newStackPtr - 1)]['code'] = constant('T_DNUMBER'); |
||
833 | $finalTokens[($newStackPtr - 1)]['type'] = 'T_DNUMBER'; |
||
834 | } |
||
835 | |||
836 | $stackPtr--; |
||
837 | continue; |
||
838 | } else { |
||
839 | $stackPtr = $oldStackPtr; |
||
840 | } |
||
841 | }//end if |
||
842 | |||
843 | // Convert the token after an object operator into a string, in most cases. |
||
844 | if ($token['code'] === T_OBJECT_OPERATOR) { |
||
845 | for ($i = ($stackPtr + 1); $i < $numTokens; $i++) { |
||
846 | if (isset(Util\Tokens::$emptyTokens[$tokens[$i]['code']]) === true) { |
||
847 | continue; |
||
848 | } |
||
849 | |||
850 | if ($tokens[$i]['code'] !== T_PROTOTYPE |
||
851 | && $tokens[$i]['code'] !== T_LNUMBER |
||
852 | && $tokens[$i]['code'] !== T_DNUMBER |
||
853 | ) { |
||
854 | $tokens[$i]['code'] = T_STRING; |
||
855 | $tokens[$i]['type'] = 'T_STRING'; |
||
856 | } |
||
857 | |||
858 | break; |
||
859 | } |
||
860 | } |
||
861 | }//end for |
||
862 | |||
863 | if (PHP_CodeSniffer_VERBOSITY > 1) { |
||
864 | echo "\t*** END TOKENIZING ***".PHP_EOL; |
||
865 | } |
||
866 | |||
867 | return $finalTokens; |
||
868 | |||
869 | }//end tokenize() |
||
870 | |||
1161 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.