PHP_CodeSniffer
PHP_CodeSniffer copied to clipboard
Runtime exception originating from sniff fixers are not handled
See the original discussion starting https://github.com/squizlabs/PHP_CodeSniffer/issues/2851#issuecomment-582101613.
Currently, the runtime exceptions originating from sniff fixers are not handled by the runner and cause the application to crash. The downsides of such behavior are:
- When fixing a large codebase, instead of fixing everything which is possible to fix, a user has to address the reason for the runtime exception and start the fixing process again. It can take a few iterations and a lot of time.
- If a 3rd-party coding standard is used, there’s no context information for a developer to understand which exactly sniff/fixer failed to report the bug to the proper project.
The discussion in #2851 is related to this and contains some additional considerations to take into account.
I found a case where a code (contains syntax error) produce crash in phpcbf but handled in phpcs utility. It may help investigating.
test.php:
<?php
trait A {
private static $a = "output\n";
public static function getA(): string {
return self::$a;
}
}
class B {
use A {
A::$a as $b;
};
private static $a = "new\n";
}
$b = new B();
echo $b->getA();
❯ phpcbf test.php
Fatal error: Uncaught PHP_CodeSniffer\Exceptions\RuntimeException: $stackPtr is not a class member var in phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Files/File.php:3
Stack trace:
#0 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Standards/PEAR/Sniffs/NamingConventions/ValidVariableNameSniff.php(3): PHP_CodeSniffer\Files\File->getMemberProperties(67)
#1 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Sniffs/AbstractVariableSniff.php(3): PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidVariableNameSniff->processMemberVar(Object(PHP_CodeSniffer\Files\LocalFile), 67)
#2 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Sniffs/AbstractScopeSniff.php(3): PHP_CodeSniffer\Sniffs\AbstractVariableSniff->processTokenWithinScope(Object(PHP_CodeSniffer\Files\LocalFile), 67, 51)
#3 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Files/File.php(3): PHP_CodeSniffer\Sniffs\AbstractScopeSniff->process(Object(PHP_CodeSniffer\Files\LocalFile), 67)
#4 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Files/LocalFile.php(3): PHP_CodeSniffer\Files\File->process()
#5 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Fixer.php(3): PHP_CodeSniffer\Files\LocalFile->process()
#6 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Reports/Cbf.php(3): PHP_CodeSniffer\Fixer->fixFile()
#7 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Reporter.php(3): PHP_CodeSniffer\Reports\Cbf->generateFileReport(Array, Object(PHP_CodeSniffer\Files\LocalFile), false, 329)
#8 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Runner.php(3): PHP_CodeSniffer\Reporter->cacheFileReport(Object(PHP_CodeSniffer\Files\LocalFile), Object(PHP_CodeSniffer\Config))
#9 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Runner.php(3): PHP_CodeSniffer\Runner->processFile(Object(PHP_CodeSniffer\Files\LocalFile))
#10 phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Runner.php(3): PHP_CodeSniffer\Runner->run()
#11 /opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf(6): PHP_CodeSniffer\Runner->runPHPCBF()
#12 {main}
thrown in phar:///opt/homebrew/Cellar/php-code-sniffer/3.7.2/bin/phpcbf/src/Files/File.php on line 3
❯ phpcs test.php
FILE: test.php
---------------------------------------------------------------------------------------------------------------------------------------------
FOUND 25 ERRORS AFFECTING 12 LINES
---------------------------------------------------------------------------------------------------------------------------------------------
1 | ERROR | [ ] An error occurred during processing; checking has been aborted. The error message was: $stackPtr is not a class member var
2 | ERROR | [ ] Missing file doc comment
3 | ERROR | [ ] Missing doc comment for trait A
3 | ERROR | [x] Opening brace of a trait must be on the line after the definition
4 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
4 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 1
4 | ERROR | [ ] Private member variable "a" must be prefixed with an underscore
6 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
6 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 1
6 | ERROR | [ ] Missing doc comment for function getA()
6 | ERROR | [x] Opening brace should be on a new line
7 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
7 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 2
8 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
8 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 1
11 | ERROR | [ ] Missing doc comment for class B
11 | ERROR | [x] Opening brace of a class must be on the line after the definition
12 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
12 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 1
13 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
13 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 2
14 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
14 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 1
16 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
16 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 1
---------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 19 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------------------------------------------------------
Time: 16ms; Memory: 6MB