diff --git a/src/Runner.php b/src/Runner.php index cfd844732f..20c2eddf44 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -13,6 +13,7 @@ namespace PHP_CodeSniffer; use Exception; +use InvalidArgumentException; use PHP_CodeSniffer\Exceptions\DeepExitException; use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\DummyFile; @@ -688,16 +689,23 @@ public function processFile($file) } if (empty($sniffStack) === false) { - if (empty($nextStack) === false - && isset($nextStack['class']) === true - && substr($nextStack['class'], -5) === 'Sniff' - ) { - $sniffCode = Common::getSniffCode($nextStack['class']); - } else { + $sniffCode = ''; + try { + if (empty($nextStack) === false + && isset($nextStack['class']) === true + && substr($nextStack['class'], -5) === 'Sniff' + ) { + $sniffCode = 'the '.Common::getSniffCode($nextStack['class']).' sniff'; + } + } catch (InvalidArgumentException $e) { + // Sniff code could not be determined. This may be an abstract sniff class. + } + + if ($sniffCode === '') { $sniffCode = substr(strrchr(str_replace('\\', '/', $sniffStack['file']), '/'), 1); } - $error .= sprintf(PHP_EOL.'The error originated in the %s sniff on line %s.', $sniffCode, $sniffStack['line']); + $error .= sprintf(PHP_EOL.'The error originated in %s on line %s.', $sniffCode, $sniffStack['line']); } $file->addErrorOnLine($error, 1, 'Internal.Exception'); diff --git a/tests/Core/Util/Common/GetSniffCodeTest.php b/tests/Core/Util/Common/GetSniffCodeTest.php index d27593416d..7cf9b16edf 100644 --- a/tests/Core/Util/Common/GetSniffCodeTest.php +++ b/tests/Core/Util/Common/GetSniffCodeTest.php @@ -108,6 +108,7 @@ public static function dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTes 'Unqualified class name' => ['ClassName'], 'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'], 'Fully qualified class name, doesn\'t end on Sniff or UnitTest' => ['Fully\\Sniffs\\Qualified\\ClassName'], + 'Fully qualified class name, ends on Sniff, but isn\'t' => ['Fully\\Sniffs\\AbstractSomethingSniff'], ]; }//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()