diff --git a/src/Fixer.php b/src/Fixer.php index b429825f06..093e4fce5d 100644 --- a/src/Fixer.php +++ b/src/Fixer.php @@ -12,6 +12,7 @@ namespace PHP_CodeSniffer; +use InvalidArgumentException; use PHP_CodeSniffer\Exceptions\RuntimeException; use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Util\Common; @@ -402,7 +403,7 @@ public function beginChangeset() if ($bt[1]['class'] === __CLASS__) { $sniff = 'Fixer'; } else { - $sniff = Common::getSniffCode($bt[1]['class']); + $sniff = $this->getSniffCodeForDebug($bt[1]['class']); } $line = $bt[0]['line']; @@ -487,7 +488,7 @@ public function rollbackChangeset() $line = $bt[0]['line']; } - $sniff = Common::getSniffCode($sniff); + $sniff = $this->getSniffCodeForDebug($sniff); $numChanges = count($this->changeset); @@ -544,7 +545,7 @@ public function replaceToken($stackPtr, $content) $line = $bt[0]['line']; } - $sniff = Common::getSniffCode($sniff); + $sniff = $this->getSniffCodeForDebug($sniff); $tokens = $this->currentFile->getTokens(); $type = $tokens[$stackPtr]['type']; @@ -659,7 +660,7 @@ public function revertToken($stackPtr) $line = $bt[0]['line']; } - $sniff = Common::getSniffCode($sniff); + $sniff = $this->getSniffCodeForDebug($sniff); $tokens = $this->currentFile->getTokens(); $type = $tokens[$stackPtr]['type']; @@ -843,4 +844,24 @@ public function changeCodeBlockIndent($start, $end, $change) }//end changeCodeBlockIndent() + /** + * Get the sniff code for the current sniff or the class name if the passed class is not a sniff. + * + * @param string $className Class name. + * + * @return string + */ + private function getSniffCodeForDebug($className) + { + try { + $sniffCode = Common::getSniffCode($className); + return $sniffCode; + } catch (InvalidArgumentException $e) { + // Sniff code could not be determined. This may be an abstract sniff class or a helper class. + return $className; + } + + }//end getSniffCodeForDebug() + + }//end class