Skip to content

Commit acbb0b7

Browse files
committed
Runner::processFile(): make internal error more informative
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) ------------------------------------------------------------------------------------------------------------------------ ```
1 parent ed8e00d commit acbb0b7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/Runner.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,39 @@ public function processFile($file)
652652
}
653653
} catch (\Exception $e) {
654654
$error = 'An error occurred during processing; checking has been aborted. The error message was: '.$e->getMessage();
655+
656+
// Determine which sniff caused the error.
657+
$sniffStack = null;
658+
$nextStack = null;
659+
foreach ($e->getTrace() as $step) {
660+
if (isset($step['file']) === false) {
661+
continue;
662+
}
663+
664+
if (empty($sniffStack) === false) {
665+
$nextStack = $step;
666+
break;
667+
}
668+
669+
if (substr($step['file'], -9) === 'Sniff.php') {
670+
$sniffStack = $step;
671+
continue;
672+
}
673+
}
674+
675+
if (empty($sniffStack) === false) {
676+
if (empty($nextStack) === false
677+
&& isset($nextStack['class']) === true
678+
&& substr($nextStack['class'], -5) === 'Sniff'
679+
) {
680+
$sniffCode = Common::getSniffCode($nextStack['class']);
681+
} else {
682+
$sniffCode = substr(strrchr(str_replace('\\', '/', $sniffStack['file']), '/'), 1);
683+
}
684+
685+
$error .= sprintf(PHP_EOL.'The error originated in the %s sniff on line %s.', $sniffCode, $sniffStack['line']);
686+
}
687+
655688
$file->addErrorOnLine($error, 1, 'Internal.Exception');
656689
}//end try
657690

0 commit comments

Comments
 (0)