Skip to content

Commit ff50956

Browse files
committed
Fixer: improve debug information
If an error/fixer conflict occurs while running the PHPCBF, additional information about the issue will be displayed in verbose mode, however, the code creating the debug info did not take into account that part of the fixer code may be in helper classes, instead of directly in the sniff class. This should fix that and make the debug information more, well... informational ;-) Related to slevomat/coding-standard 1739
1 parent 78fbef6 commit ff50956

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;
@@ -399,7 +400,7 @@ public function beginChangeset()
399400
if ($bt[1]['class'] === __CLASS__) {
400401
$sniff = 'Fixer';
401402
} else {
402-
$sniff = Common::getSniffCode($bt[1]['class']);
403+
$sniff = $this->getSniffCodeForDebug($bt[1]['class']);
403404
}
404405

405406
$line = $bt[0]['line'];
@@ -478,7 +479,7 @@ public function rollbackChangeset()
478479
$line = $bt[0]['line'];
479480
}
480481

481-
$sniff = Common::getSniffCode($sniff);
482+
$sniff = $this->getSniffCodeForDebug($sniff);
482483

483484
$numChanges = count($this->changeset);
484485

@@ -531,7 +532,7 @@ public function replaceToken($stackPtr, $content)
531532
$line = $bt[0]['line'];
532533
}
533534

534-
$sniff = Common::getSniffCode($sniff);
535+
$sniff = $this->getSniffCodeForDebug($sniff);
535536

536537
$tokens = $this->currentFile->getTokens();
537538
$type = $tokens[$stackPtr]['type'];
@@ -636,7 +637,7 @@ public function revertToken($stackPtr)
636637
$line = $bt[0]['line'];
637638
}
638639

639-
$sniff = Common::getSniffCode($sniff);
640+
$sniff = $this->getSniffCodeForDebug($sniff);
640641

641642
$tokens = $this->currentFile->getTokens();
642643
$type = $tokens[$stackPtr]['type'];
@@ -820,4 +821,24 @@ public function changeCodeBlockIndent($start, $end, $change)
820821
}//end changeCodeBlockIndent()
821822

822823

824+
/**
825+
* Get the sniff code for the current sniff or the class name if the passed class is not a sniff.
826+
*
827+
* @param string $className Class name.
828+
*
829+
* @return string
830+
*/
831+
private function getSniffCodeForDebug($className)
832+
{
833+
try {
834+
$sniffCode = Common::getSniffCode($className);
835+
return $sniffCode;
836+
} catch (InvalidArgumentException $e) {
837+
// Sniff code could not be determined. This may be an abstract sniff class or a helper class.
838+
return $className;
839+
}
840+
841+
}//end getSniffCodeForDebug()
842+
843+
823844
}//end class

0 commit comments

Comments
 (0)