Skip to content

Commit 40d9024

Browse files
authored
feat(UnsilencedDeprecation): Add sniff for unsilenced trigger_error() deprecation warnings (#3412078)
1 parent 1a1613d commit 40d9024

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* \Drupal\Sniffs\Semantics\UnsilencedDeprecationSniff.
4+
*
5+
* @category PHP
6+
* @package PHP_CodeSniffer
7+
* @link http://pear.php.net/package/PHP_CodeSniffer
8+
*/
9+
10+
namespace Drupal\Sniffs\Semantics;
11+
12+
use PHP_CodeSniffer\Files\File;
13+
14+
/**
15+
* Checks that the trigger_error deprecation is silenced by a preceding '@'.
16+
*
17+
* @category PHP
18+
* @package PHP_CodeSniffer
19+
* @link http://pear.php.net/package/PHP_CodeSniffer
20+
*/
21+
class UnsilencedDeprecationSniff extends FunctionCall
22+
{
23+
24+
25+
/**
26+
* Returns an array of function names this test wants to listen for.
27+
*
28+
* @return array<string>
29+
*/
30+
public function registerFunctionNames()
31+
{
32+
return ['trigger_error'];
33+
34+
}//end registerFunctionNames()
35+
36+
37+
/**
38+
* Processes this function call.
39+
*
40+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
41+
* @param int $stackPtr The position of the function call in
42+
* the stack.
43+
* @param int $openBracket The position of the opening
44+
* parenthesis in the stack.
45+
* @param int $closeBracket The position of the closing
46+
* parenthesis in the stack.
47+
*
48+
* @return void
49+
*/
50+
public function processFunctionCall(
51+
file $phpcsFile,
52+
$stackPtr,
53+
$openBracket,
54+
$closeBracket
55+
) {
56+
57+
$tokens = $phpcsFile->getTokens();
58+
$argument = $this->getArgument(2);
59+
60+
// If no second argument then quit.
61+
if ($argument === false) {
62+
return;
63+
}
64+
65+
// Only check deprecation messages.
66+
if (strcasecmp($tokens[$argument['start']]['content'], 'E_USER_DEPRECATED') !== 0) {
67+
return;
68+
}
69+
70+
if ($tokens[($stackPtr - 1)]['type'] !== 'T_ASPERAND') {
71+
$error = 'All trigger_error calls used for deprecation must be prefixed by an "@"';
72+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'UnsilencedDeprecation');
73+
if ($fix === true) {
74+
$phpcsFile->fixer->addContentBefore($stackPtr, '@');
75+
}
76+
}
77+
78+
}//end processFunctionCall()
79+
80+
81+
}//end class
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Test data for the relaxed version of FunctionUnsilencedDeprecationSniff coding standard.
6+
*/
7+
8+
// No second parameter, so cannot fail it, both silenced and unsilenced.
9+
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
10+
trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
11+
12+
// Not E_USER_DEPRECATED, so cannot fail it, both silenced and unsilenced.
13+
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
14+
trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
15+
16+
// E_USER_DEPRECATED, so silenced is fine...
17+
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_DEPRECATED);
18+
// ... but unsilenced fails.
19+
trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_DEPRECATED);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Test data for the relaxed version of FunctionUnsilencedDeprecationSniff coding standard.
6+
*/
7+
8+
// No second parameter, so cannot fail it, both silenced and unsilenced.
9+
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
10+
trigger_error('CommentTestBase is deprecated in drupal 8.4.0');
11+
12+
// Not E_USER_DEPRECATED, so cannot fail it, both silenced and unsilenced.
13+
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
14+
trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_SOMETHING_ELSE);
15+
16+
// E_USER_DEPRECATED, so silenced is fine...
17+
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_DEPRECATED);
18+
// ... but unsilenced fails.
19+
@trigger_error('CommentTestBase is deprecated in drupal 8.4.0', E_USER_DEPRECATED);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Drupal\Test\Semantics;
4+
5+
use Drupal\Test\CoderSniffUnitTest;
6+
7+
class UnsilencedDeprecationUnitTest extends CoderSniffUnitTest
8+
{
9+
10+
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* The key of the array should represent the line number and the value
15+
* should represent the number of errors that should occur on that line.
16+
*
17+
* @param string $testFile The name of the file being tested.
18+
*
19+
* @return array<int, int>
20+
*/
21+
protected function getErrorList(string $testFile): array
22+
{
23+
return [19 => 1];
24+
25+
}//end getErrorList()
26+
27+
28+
/**
29+
* Returns the lines where warnings should occur.
30+
*
31+
* The key of the array should represent the line number and the value
32+
* should represent the number of warnings that should occur on that line.
33+
*
34+
* @param string $testFile The name of the file being tested.
35+
*
36+
* @return array<int, int>
37+
*/
38+
protected function getWarningList(string $testFile): array
39+
{
40+
return [];
41+
42+
}//end getWarningList()
43+
44+
45+
}//end class

0 commit comments

Comments
 (0)