Skip to content
Snippets Groups Projects
Unverified Commit 40d9024d authored by Boegie's avatar Boegie Committed by GitHub
Browse files

feat(UnsilencedDeprecation): Add sniff for unsilenced trigger_error()...

feat(UnsilencedDeprecation): Add sniff for unsilenced trigger_error() deprecation warnings (#3412078)
parent 1a1613d8
No related branches found
No related tags found
No related merge requests found
<?php
/**
* \Drupal\Sniffs\Semantics\UnsilencedDeprecationSniff.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace Drupal\Sniffs\Semantics;
use PHP_CodeSniffer\Files\File;
/**
* Checks that the trigger_error deprecation is silenced by a preceding '@'.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class UnsilencedDeprecationSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['trigger_error'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
file $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(2);
// If no second argument then quit.
if ($argument === false) {
return;
}
// Only check deprecation messages.
if (strcasecmp($tokens[$argument['start']]['content'], 'E_USER_DEPRECATED') !== 0) {
return;
}
if ($tokens[($stackPtr - 1)]['type'] !== 'T_ASPERAND') {
$error = 'All trigger_error calls used for deprecation must be prefixed by an "@"';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'UnsilencedDeprecation');
if ($fix === true) {
$phpcsFile->fixer->addContentBefore($stackPtr, '@');
}
}
}//end processFunctionCall()
}//end class
<?php
/**
* @file
* Test data for the relaxed version of FunctionUnsilencedDeprecationSniff coding standard.
*/
// No second parameter, so cannot fail it, both silenced and unsilenced.
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
// Not E_USER_DEPRECATED, so cannot fail it, both silenced and unsilenced.
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
// E_USER_DEPRECATED, so silenced is fine...
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_DEPRECATED);
// ... but unsilenced fails.
trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_DEPRECATED);
<?php
/**
* @file
* Test data for the relaxed version of FunctionUnsilencedDeprecationSniff coding standard.
*/
// No second parameter, so cannot fail it, both silenced and unsilenced.
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
// Not E_USER_DEPRECATED, so cannot fail it, both silenced and unsilenced.
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
// E_USER_DEPRECATED, so silenced is fine...
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_DEPRECATED);
// ... but unsilenced fails.
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_DEPRECATED);
<?php
namespace Drupal\Test\Semantics;
use Drupal\Test\CoderSniffUnitTest;
class UnsilencedDeprecationUnitTest extends CoderSniffUnitTest
{
/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
protected function getErrorList(string $testFile): array
{
return [19 => 1];
}//end getErrorList()
/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
protected function getWarningList(string $testFile): array
{
return [];
}//end getWarningList()
}//end class
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment