PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Runner::processFile(): make internal error more informative

Open jrfnl opened this issue 3 years ago • 1 comments

When sniffs use traits/utility classes/helper functions, the message for the Internal.Exception can be unclear as it may point to the trait/utility classes/helper function as the culprit without any indication of which sniff caused the error.

This can cause bugs in sniffs to be reported by end-users in the wrong repository, but even when reported in the right repository, it makes the report unclear and means the maintainer will need to do more work to try and reproduce the issue.

This PR attempts to make the Internal.Exception error message more informative by pinpointing the sniff which caused the issue to occur. While this may result in duplicate information when no traits/utility classes/helper functions are involved, for those cases when those are involved, I believe this will be a useful change.

Example output for a (simulated) error without this change:

------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------
 1 | ERROR | An error occurred during processing; checking has been aborted. The error message was: Undefined variable
   |       | $tabWidth in
   |       | path/to/PHP_CodeSniffer/src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php
   |       | on line 64 (Internal.Exception)
------------------------------------------------------------------------------------------------------------------------

Example output for the same (simulated) error with this change:

------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------
 1 | ERROR | An error occurred during processing; checking has been aborted. The error message was: Undefined variable
   |       | $tabWidth in
   |       | path/to/PHP_CodeSniffer/src/Standards/Generic/Sniffs/WhiteSpace/DisallowSpaceIndentSniff.php
   |       | on line 64
   |       | The error originated in the Generic.WhiteSpace.DisallowSpaceIndent sniff on line 64.
   |       | (Internal.Exception)
------------------------------------------------------------------------------------------------------------------------

Example output for a (simulated) error without this change for a sniff using a helper function:

------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------
 1 | ERROR | An error occurred during processing; checking has been aborted. The error message was: The
   |       | PHPCSUtils\Tokens\Collections::functionDeclarationTokensBC() method is deprecated since PHPCSUtils
   |       | 1.0.0-alpha4. Use the PHPCSUtils\Tokens\Collections::functionDeclarationTokens() method instead. in
   |       | path/to/PHPCSUtils/PHPCSUtils/Tokens/Collections.php on line 753
   |       | (Internal.Exception)
------------------------------------------------------------------------------------------------------------------------

Example output for the same (simulated) error with this change for a sniff using a helper function:

------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------
 1 | ERROR | An error occurred during processing; checking has been aborted. The error message was: The
   |       | PHPCSUtils\Tokens\Collections::functionDeclarationTokensBC() method is deprecated since PHPCSUtils
   |       | 1.0.0-alpha4. Use the PHPCSUtils\Tokens\Collections::functionDeclarationTokens() method instead. in
   |       | path/to/PHPCSUtils/PHPCSUtils/Tokens/Collections.php on line 753
   |       | The error originated in the PHPCompatibility.Interfaces.NewInterfaces sniff on line 201.
   |       | (Internal.Exception)
------------------------------------------------------------------------------------------------------------------------

jrfnl avatar Nov 21 '22 02:11 jrfnl

Found a logic error in the original code. Fixed it and force pushed the commit again.

jrfnl avatar Dec 15 '22 09:12 jrfnl

FYI: this fix is included in today's PHP_CodeSniffer 3.8.0 release.

As per #3932, development on PHP_CodeSniffer will continue in the PHPCSStandards/PHP_CodeSniffer repository. If you want to stay informed, you may want to start "watching" that repo (or watching releases from that repo).

jrfnl avatar Dec 09 '23 00:12 jrfnl