From 6687b7e9ab543c69c83b386452b53ba03a473539 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 7 Jun 2015 22:51:33 +0200 Subject: [ticket/13930] Check for correct spacing between keyword & parenthesis The codesniffer should check for a correct spacing between keywords like if or foreach and its opening parenthesis. PHPBB3-13930 --- .../ControlStructures/OpeningParenthesisSniff.php | 62 ++++++++++++++++++++++ build/code_sniffer/ruleset-php-strict.xml | 3 ++ 2 files changed, 65 insertions(+) create mode 100644 build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php (limited to 'build') diff --git a/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php b/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php new file mode 100644 index 0000000000..955405ce01 --- /dev/null +++ b/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php @@ -0,0 +1,62 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +/** + * Checks that there is exactly one space between the keyword and the opening + * parenthesis of a control structures. + */ +class phpbb_Sniffs_ControlStructures_OpeningParenthesisSniff implements PHP_CodeSniffer_Sniff +{ + /** + * Registers the tokens that this sniff wants to listen for. + */ + public function register() + { + return array( + T_IF, + T_FOREACH, + T_WHILE, + T_FOR, + T_SWITCH, + T_ELSEIF, + T_CATCH, + ); + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param PHP_CodeSniffer_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(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + + if ($tokens[$stackPtr + 1]['content'] === '(') + { + $error = 'There should be exactly one space between the keyword and opening parenthesis'; + $phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeOpeningParenthesis'); + return; + } + else if ($tokens[$stackPtr + 1]['content'] !== ' ') + { + $error = 'There should be exactly one space between the keyword and opening parenthesis'; + $phpcsFile->addError($error, $stackPtr, 'IncorrectSpaceBeforeOpeningParenthesis'); + return; + } + } +} diff --git a/build/code_sniffer/ruleset-php-strict.xml b/build/code_sniffer/ruleset-php-strict.xml index 9e2f0664d8..fdc82f2be8 100644 --- a/build/code_sniffer/ruleset-php-strict.xml +++ b/build/code_sniffer/ruleset-php-strict.xml @@ -45,4 +45,7 @@ + + + -- cgit v1.2.1 From d70ad1230b2348ac394715afecdca25e66ddf125 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 8 Jun 2015 10:07:12 +0200 Subject: [ticket/13930] Remove not needed return statements PHPBB3-13930 --- .../phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'build') diff --git a/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php b/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php index 955405ce01..349bccbb02 100644 --- a/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php @@ -50,13 +50,11 @@ class phpbb_Sniffs_ControlStructures_OpeningParenthesisSniff implements PHP_Code { $error = 'There should be exactly one space between the keyword and opening parenthesis'; $phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeOpeningParenthesis'); - return; } else if ($tokens[$stackPtr + 1]['content'] !== ' ') { $error = 'There should be exactly one space between the keyword and opening parenthesis'; $phpcsFile->addError($error, $stackPtr, 'IncorrectSpaceBeforeOpeningParenthesis'); - return; } } } -- cgit v1.2.1 From d4e0c08915662fd93715984510b3b2043e6bfd8d Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 8 Jun 2015 13:40:33 +0200 Subject: [ticket/13930] Use OpeningParenthesisSniff in legacy ruleset PHPBB3-13930 --- build/code_sniffer/ruleset-php-legacy.xml | 3 +++ build/code_sniffer/ruleset-php-strict.xml | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'build') diff --git a/build/code_sniffer/ruleset-php-legacy.xml b/build/code_sniffer/ruleset-php-legacy.xml index b0110e8b12..c740c6080f 100644 --- a/build/code_sniffer/ruleset-php-legacy.xml +++ b/build/code_sniffer/ruleset-php-legacy.xml @@ -86,4 +86,7 @@ + + + diff --git a/build/code_sniffer/ruleset-php-strict.xml b/build/code_sniffer/ruleset-php-strict.xml index fdc82f2be8..9e2f0664d8 100644 --- a/build/code_sniffer/ruleset-php-strict.xml +++ b/build/code_sniffer/ruleset-php-strict.xml @@ -45,7 +45,4 @@ - - - -- cgit v1.2.1