From 0897d3e381198b157865b0e5364e2eb3dbf052c6 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 23 Mar 2021 22:09:05 +0100 Subject: [PATCH] [ticket/16736] Add sniffer for visibilitiy qualifiers before static keyword PHPBB3-16736 --- .../ControlStructures/StaticKeywordSniff.php | 57 +++++++++++++++++++ build/code_sniffer/ruleset-php-legacy.xml | 3 + 2 files changed, 60 insertions(+) create mode 100644 build/code_sniffer/phpbb/Sniffs/ControlStructures/StaticKeywordSniff.php diff --git a/build/code_sniffer/phpbb/Sniffs/ControlStructures/StaticKeywordSniff.php b/build/code_sniffer/phpbb/Sniffs/ControlStructures/StaticKeywordSniff.php new file mode 100644 index 0000000000..1dabe82235 --- /dev/null +++ b/build/code_sniffer/phpbb/Sniffs/ControlStructures/StaticKeywordSniff.php @@ -0,0 +1,57 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +use PHP_CodeSniffer\Files\File; +use PHP_CodeSniffer\Sniffs\Sniff; + +/** + * Checks that the visibility qualifiers are placed after the static keyword + * according to the coding guidelines + */ +class phpbb_Sniffs_ControlStructures_StaticKeywordSniff implements Sniff +{ + /** + * Registers the tokens that this sniff wants to listen for. + */ + public function register() + { + return [ + T_STATIC, + ]; + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the stack passed in $tokens. + * + * @return void + */ + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + $disallowed_before_tokens = [ + T_PUBLIC, + T_PROTECTED, + T_PRIVATE, + ]; + + if (in_array($tokens[$stackPtr - 2]['code'], $disallowed_before_tokens)) + { + $error = 'Access specifier (e.g. public) should follow static scope attribute. Encountered "' . $tokens[$stackPtr - 2]['content'] . '" before static'; + $phpcsFile->addError($error, $stackPtr, 'InvalidStaticFunctionDeclaration'); + } + } +} diff --git a/build/code_sniffer/ruleset-php-legacy.xml b/build/code_sniffer/ruleset-php-legacy.xml index c740c6080f..47d0ca772a 100644 --- a/build/code_sniffer/ruleset-php-legacy.xml +++ b/build/code_sniffer/ruleset-php-legacy.xml @@ -89,4 +89,7 @@ + + +