-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UnsilencedDeprecation): Add sniff for unsilenced trigger_error()…
… deprecation warnings (#3412078)
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
coder_sniffer/Drupal/Sniffs/Semantics/UnsilencedDeprecationSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
tests/Drupal/Semantics/UnsilencedDeprecationUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |