Skip to content

Commit 32f7d88

Browse files
authored
Merge pull request #906 from PHPCSStandards/feature/slevomat-1739
Fixer: improve debug information
2 parents eb6b059 + 8e1fc94 commit 32f7d88

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/Fixer.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace PHP_CodeSniffer;
1414

15+
use InvalidArgumentException;
1516
use PHP_CodeSniffer\Exceptions\RuntimeException;
1617
use PHP_CodeSniffer\Files\File;
1718
use PHP_CodeSniffer\Util\Common;
@@ -402,7 +403,7 @@ public function beginChangeset()
402403
if ($bt[1]['class'] === __CLASS__) {
403404
$sniff = 'Fixer';
404405
} else {
405-
$sniff = Common::getSniffCode($bt[1]['class']);
406+
$sniff = $this->getSniffCodeForDebug($bt[1]['class']);
406407
}
407408

408409
$line = $bt[0]['line'];
@@ -487,7 +488,7 @@ public function rollbackChangeset()
487488
$line = $bt[0]['line'];
488489
}
489490

490-
$sniff = Common::getSniffCode($sniff);
491+
$sniff = $this->getSniffCodeForDebug($sniff);
491492

492493
$numChanges = count($this->changeset);
493494

@@ -544,7 +545,7 @@ public function replaceToken($stackPtr, $content)
544545
$line = $bt[0]['line'];
545546
}
546547

547-
$sniff = Common::getSniffCode($sniff);
548+
$sniff = $this->getSniffCodeForDebug($sniff);
548549

549550
$tokens = $this->currentFile->getTokens();
550551
$type = $tokens[$stackPtr]['type'];
@@ -659,7 +660,7 @@ public function revertToken($stackPtr)
659660
$line = $bt[0]['line'];
660661
}
661662

662-
$sniff = Common::getSniffCode($sniff);
663+
$sniff = $this->getSniffCodeForDebug($sniff);
663664

664665
$tokens = $this->currentFile->getTokens();
665666
$type = $tokens[$stackPtr]['type'];
@@ -843,4 +844,24 @@ public function changeCodeBlockIndent($start, $end, $change)
843844
}//end changeCodeBlockIndent()
844845

845846

847+
/**
848+
* Get the sniff code for the current sniff or the class name if the passed class is not a sniff.
849+
*
850+
* @param string $className Class name.
851+
*
852+
* @return string
853+
*/
854+
private function getSniffCodeForDebug($className)
855+
{
856+
try {
857+
$sniffCode = Common::getSniffCode($className);
858+
return $sniffCode;
859+
} catch (InvalidArgumentException $e) {
860+
// Sniff code could not be determined. This may be an abstract sniff class or a helper class.
861+
return $className;
862+
}
863+
864+
}//end getSniffCodeForDebug()
865+
866+
846867
}//end class

0 commit comments

Comments
 (0)