squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.67k stars 1.48k forks source link

Infinite loop in ValidClassNameSniff #3603

Closed neerolyte closed 2 years ago

neerolyte commented 2 years ago

Describe the bug

Some PHP syntax errors can cause PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ValidClassNameSniff to loop forever.

Code sample

<?php class Foo implements Interface {}

Custom ruleset N/A

To reproduce Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run ./vendor/bin/phpcs test.php --standard=PSR12 -vvv
  3. See it gets stuck at:
                Process token 1: T_CLASS => class
                        Processing PHP_CodeSniffer\Standards\PSR12\Sniffs\Classes\OpeningBraceSpaceSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR12\Sniffs\Classes\ClosingBraceSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ValidClassNameSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseKeywordSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR2\Sniffs\Classes\ClassDeclarationSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\ScopeClosingBraceSniff... DONE in 0 seconds
                Process token 2: T_WHITESPACE => ·
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 3: T_STRING => Foo
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions\UpperCaseConstantNameSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR2\Sniffs\Methods\FunctionCallSignatureSniff... DONE in 0 seconds
                Process token 4: T_WHITESPACE => ·
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 5: T_IMPLEMENTS => implements
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseKeywordSniff... DONE in 0 seconds
                Process token 6: T_WHITESPACE => ·
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 7: T_INTERFACE => Interface
                        Processing PHP_CodeSniffer\Standards\PSR12\Sniffs\Classes\OpeningBraceSpaceSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR12\Sniffs\Classes\ClosingBraceSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR1\Sniffs\Classes\ClassDeclarationSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\Classes\ValidClassNameSniff...

Expected behavior

The syntax error should be detected and not trigger an infinite loop

Versions (please complete the following information):

Additional context

neerolyte commented 2 years ago

This is working master. git bisect suggests 2b7bdb344d1527bcdb157e6ee60c6ce9fcca07ba was the fix.

gsherwood commented 2 years ago

Was just looking at this and suspected it might have been that change. Thanks.