PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Runtime exception originating from sniff fixers are not handled

Open morozov opened this issue 5 years ago • 2 comments

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:

  1. 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.
  2. 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.

morozov avatar Feb 15 '20 18:02 morozov

The discussion in #2851 is related to this and contains some additional considerations to take into account.

jrfnl avatar May 07 '23 02:05 jrfnl

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

the-csaba avatar Jul 25 '23 07:07 the-csaba