diff --git a/coder_sniffer/Drupal/Sniffs/Semantics/UnsilencedDeprecationSniff.php b/coder_sniffer/Drupal/Sniffs/Semantics/UnsilencedDeprecationSniff.php new file mode 100644 index 00000000..f9b8dba0 --- /dev/null +++ b/coder_sniffer/Drupal/Sniffs/Semantics/UnsilencedDeprecationSniff.php @@ -0,0 +1,81 @@ + + */ + 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 diff --git a/tests/Drupal/Semantics/UnsilencedDeprecationUnitTest.inc b/tests/Drupal/Semantics/UnsilencedDeprecationUnitTest.inc new file mode 100644 index 00000000..1017fd6d --- /dev/null +++ b/tests/Drupal/Semantics/UnsilencedDeprecationUnitTest.inc @@ -0,0 +1,19 @@ + + */ + 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 + */ + protected function getWarningList(string $testFile): array + { + return []; + + }//end getWarningList() + + +}//end class