Skip to content

Commit d6a1bf2

Browse files
committed
Generic/NoSilencedErrors: improve the usefulness of the error message
... by showing a snippet of the context in which the error silence operator was found.
1 parent 0e6337e commit d6a1bf2

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Standards/Generic/Sniffs/PHP/NoSilencedErrorsSniff.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,21 @@ public function register()
5353
*/
5454
public function process(File $phpcsFile, $stackPtr)
5555
{
56+
// Prepare the "Found" string to display.
57+
$contextLength = 4;
58+
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr, T_COMMA);
59+
if (($endOfStatement - $stackPtr) < $contextLength) {
60+
$contextLength = ($endOfStatement - $stackPtr);
61+
}
62+
$found = $this->phpcsFile->getTokensAsString($stackPtr, $contextLength);
63+
$found = str_replace(array("\t", "\n", "\r"), ' ', $found) . '...';
64+
5665
if ($this->error === true) {
57-
$error = 'Silencing errors is forbidden';
58-
$phpcsFile->addError($error, $stackPtr, 'Forbidden');
66+
$error = 'Silencing errors is forbidden. Found: %s';
67+
$phpcsFile->addError($error, $stackPtr, 'Forbidden', [$found]);
5968
} else {
60-
$error = 'Silencing errors is discouraged';
61-
$phpcsFile->addWarning($error, $stackPtr, 'Discouraged');
69+
$error = 'Silencing errors is discouraged. Found: %s';
70+
$phpcsFile->addWarning($error, $stackPtr, 'Discouraged', [$found]);
6271
}
6372

6473
}//end process()

0 commit comments

Comments
 (0)