Skip to content

Commit

Permalink
feat(UnsilencedDeprecation): Add sniff for unsilenced trigger_error()…
Browse files Browse the repository at this point in the history
… deprecation warnings (#3412078)
  • Loading branch information
Boegie authored Feb 16, 2024
1 parent 1a1613d commit 40d9024
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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
19 changes: 19 additions & 0 deletions tests/Drupal/Semantics/UnsilencedDeprecationUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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);
19 changes: 19 additions & 0 deletions tests/Drupal/Semantics/UnsilencedDeprecationUnitTest.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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);
45 changes: 45 additions & 0 deletions tests/Drupal/Semantics/UnsilencedDeprecationUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 comments on commit 40d9024

Please sign in to comment.