diff options
Diffstat (limited to 'build/code_sniffer/phpbb')
4 files changed, 289 insertions, 73 deletions
diff --git a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php index 8337cf02ee..8c0ec853ff 100644 --- a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php @@ -60,14 +60,14 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff return; } // Mark as error if this is not a doc comment - else if ($start === false || $tokens[$start]['code'] !== T_DOC_COMMENT) + else if ($start === false || $tokens[$start]['code'] !== T_DOC_COMMENT_OPEN_TAG) { $phpcsFile->addError('Missing required file doc comment.', $stackPtr); return; } // Find comment end token - $end = $phpcsFile->findNext(T_DOC_COMMENT, $start + 1, null, true) - 1; + $end = $tokens[$start]['comment_closer']; // If there is no end, skip processing here if ($end === false) @@ -75,38 +75,30 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff return; } - // List of found comment tags - $tags = array(); - // check comment lines without the first(/**) an last(*/) line - for ($i = $start + 1, $c = $end - 1; $i <= $c; ++$i) + for ($token = $start + 1, $c = $end - 2; $token <= $c; ++$token) { - $line = $tokens[$i]['content']; - // Check that each line starts with a '*' - if (substr($line, 0, 1) !== '*' && substr($line, 0, 2) !== ' *') + if ($tokens[$token]['column'] === 1 && (($tokens[$token]['content'] !== '*' && $tokens[$token]['content'] !== ' ') || ($tokens[$token]['content'] === ' ' && $tokens[$token + 1]['content'] !== '*'))) { $message = 'The file doc comment should not be indented.'; - $phpcsFile->addWarning($message, $i); - } - else if (preg_match('/^[ ]?\*\s+@([\w]+)\s+(.*)$/', $line, $match) !== 0) - { - if (!isset($tags[$match[1]])) - { - $tags[$match[1]] = array(); - } - - $tags[$match[1]][] = array($match[2], $i); + $phpcsFile->addWarning($message, $token); } } // Check that the first and last line is empty - if (trim($tokens[$start + 1]['content']) !== '*') + // /**T_WHITESPACE + // (T_WHITESPACE)*T_WHITESPACE + // (T_WHITESPACE)* ... + // (T_WHITESPACE)*T_WHITESPACE + // T_WHITESPACE*/ + if (!(($tokens[$start + 2]['content'] !== '*' && $tokens[$start + 4]['content'] !== '*') || ($tokens[$start + 3]['content'] !== '*' && $tokens[$start + 6]['content'] !== '*'))) { $message = 'The first file comment line should be empty.'; $phpcsFile->addWarning($message, ($start + 1)); } - if (trim($tokens[$end - 1]['content']) !== '*') + + if ($tokens[$end - 3]['content'] !== '*' && $tokens[$end - 6]['content'] !== '*') { $message = 'The last file comment line should be empty.'; $phpcsFile->addWarning($message, $end - 1); @@ -114,8 +106,8 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff //$this->processPackage($phpcsFile, $start, $tags); //$this->processVersion($phpcsFile, $start, $tags); - $this->processCopyright($phpcsFile, $start, $tags); - $this->processLicense($phpcsFile, $start, $tags); + $this->processCopyright($phpcsFile, $start, $tokens[$start]['comment_tags']); + $this->processLicense($phpcsFile, $start, $tokens[$start]['comment_tags']); } /** @@ -176,17 +168,24 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff protected function processCopyright(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) { $copyright = '(c) phpBB Limited <https://www.phpbb.com>'; + $tokens = $phpcsFile->getTokens(); - if (!isset($tags['copyright'])) + foreach ($tags as $tag) { - $message = 'Missing require @copyright tag in file doc comment.'; - $phpcsFile->addError($message, $ptr); - } - else if ($tags['copyright'][0][0] !== $copyright) - { - $message = 'Invalid content found for the first @copyright tag, use "' . $copyright . '".'; - $phpcsFile->addError($message, $tags['copyright'][0][1]); + if ($tokens[$tag]['content'] === '@copyright') + { + if ($tokens[$tag + 2]['content'] !== $copyright) + { + $message = 'Invalid content found for the first @copyright tag, use "' . $copyright . '".'; + $phpcsFile->addError($message, $tags['copyright'][0][1]); + } + + return; + } } + + $message = 'Missing require @copyright tag in file doc comment.'; + $phpcsFile->addError($message, $ptr); } /** @@ -201,22 +200,33 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff protected function processLicense(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) { $license = 'GNU General Public License, version 2 (GPL-2.0)'; + $tokens = $phpcsFile->getTokens(); - if (!isset($tags['license'])) + $found = false; + foreach ($tags as $tag) { - $message = 'Missing require @license tag in file doc comment.'; - $phpcsFile->addError($message, $ptr); + if ($tokens[$tag]['content'] === '@license') + { + if ($found) + { + $message = 'It must be only one @license tag in file doc comment.'; + $phpcsFile->addError($message, $ptr); + } + + $found = true; + + if ($tokens[$tag + 2]['content'] !== $license) + { + $message = 'Invalid content found for @license tag, use "' . $license . '".'; + $phpcsFile->addError($message, $tags['license'][0][1]); + } + } } - else if (sizeof($tags['license']) !== 1) + + if (!$found) { - $message = 'It must be only one @license tag in file doc comment.'; + $message = 'Missing require @license tag in file doc comment.'; $phpcsFile->addError($message, $ptr); } - else if (trim($tags['license'][0][0]) !== $license) - { - $message = 'Invalid content found for @license tag, use ' - . '"' . $license . '".'; - $phpcsFile->addError($message, $tags['license'][0][1]); - } } } diff --git a/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningBraceBsdAllmanSniff.php b/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningBraceBsdAllmanSniff.php new file mode 100644 index 0000000000..885c38c5b4 --- /dev/null +++ b/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningBraceBsdAllmanSniff.php @@ -0,0 +1,143 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @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 the opening brace of a control structures is on the line after. + * From Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff + */ +class phpbb_Sniffs_ControlStructures_OpeningBraceBsdAllmanSniff implements PHP_CodeSniffer_Sniff +{ + /** + * Registers the tokens that this sniff wants to listen for. + */ + public function register() + { + return array( + T_IF, + T_ELSE, + T_FOREACH, + T_WHILE, + T_DO, + T_FOR, + T_SWITCH, + ); + } + + /** + * 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 (isset($tokens[$stackPtr]['scope_opener']) === false) + { + return; + } + + /* + * ... + * } + * else if () + * { + * ... + */ + if ($tokens[$stackPtr]['code'] === T_ELSE && $tokens[$stackPtr + 2]['code'] === T_IF) + { + return; + } + + $openingBrace = $tokens[$stackPtr]['scope_opener']; + + /* + * ... + * do + * { + * <code> + * } while(); + * ... + * } + * else + * { + * ... + */ + if ($tokens[$stackPtr]['code'] === T_DO ||$tokens[$stackPtr]['code'] === T_ELSE) + { + $cs_line = $tokens[$stackPtr]['line']; + } + else + { + // The end of the function occurs at the end of the argument list. Its + // like this because some people like to break long function declarations + // over multiple lines. + $cs_line = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line']; + } + + $braceLine = $tokens[$openingBrace]['line']; + + $lineDifference = ($braceLine - $cs_line); + + if ($lineDifference === 0) + { + $error = 'Opening brace should be on a new line'; + $phpcsFile->addError($error, $openingBrace, 'BraceOnSameLine'); + return; + } + + if ($lineDifference > 1) + { + $error = 'Opening brace should be on the line after the declaration; found %s blank line(s)'; + $data = array(($lineDifference - 1)); + $phpcsFile->addError($error, $openingBrace, 'BraceSpacing', $data); + return; + } + + // We need to actually find the first piece of content on this line, + // as if this is a method with tokens before it (public, static etc) + // or an if with an else before it, then we need to start the scope + // checking from there, rather than the current token. + $lineStart = $stackPtr; + while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) + { + if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) + { + break; + } + } + + // We found a new line, now go forward and find the first non-whitespace + // token. + $lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true); + + // The opening brace is on the correct line, now it needs to be + // checked to be correctly indented. + $startColumn = $tokens[$lineStart]['column']; + $braceIndent = $tokens[$openingBrace]['column']; + + if ($braceIndent !== $startColumn) + { + $error = 'Opening brace indented incorrectly; expected %s spaces, found %s'; + $data = array( + ($startColumn - 1), + ($braceIndent - 1), + ); + $phpcsFile->addError($error, $openingBrace, 'BraceIndent', $data); + } + } +} 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..349bccbb02 --- /dev/null +++ b/build/code_sniffer/phpbb/Sniffs/ControlStructures/OpeningParenthesisSniff.php @@ -0,0 +1,60 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @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'); + } + else if ($tokens[$stackPtr + 1]['content'] !== ' ') + { + $error = 'There should be exactly one space between the keyword and opening parenthesis'; + $phpcsFile->addError($error, $stackPtr, 'IncorrectSpaceBeforeOpeningParenthesis'); + } + } +} diff --git a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php index 18cb8ba82e..7ffd1aadd6 100644 --- a/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Namespaces/UnusedUseSniff.php @@ -138,6 +138,7 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff // Check docblocks $find = array( T_COMMENT, + T_DOC_COMMENT_CLOSE_TAG, T_DOC_COMMENT, T_CLASS, T_FUNCTION, @@ -147,43 +148,31 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff $comment_end = $phpcsFile->findPrevious($find, ($function_declaration - 1)); if ($comment_end !== false) { - if (!$tokens[$comment_end]['code'] !== T_DOC_COMMENT) + if ($tokens[$comment_end]['code'] === T_DOC_COMMENT_CLOSE_TAG) { - $comment_start = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($comment_end - 1), null, true) + 1); - $comment = $phpcsFile->getTokensAsString($comment_start, ($comment_end - $comment_start + 1)); - - try - { - $comment_parser = new PHP_CodeSniffer_CommentParser_FunctionCommentParser($comment, $phpcsFile); - $comment_parser->parse(); - - // Check @param - foreach ($comment_parser->getParams() as $param) { - $type = $param->getType(); - $types = explode('|', str_replace('[]', '', $type)); - foreach ($types as $type) - { - $ok = $this->check($phpcsFile, $type, $class_name_full, $class_name_short, $param->getLine() + $comment_start) ? true : $ok; - } + $comment_start = $tokens[$comment_end]['comment_opener']; + foreach ($tokens[$comment_start]['comment_tags'] as $tag) { + if ($tokens[$tag]['content'] !== '@param' && $tokens[$tag]['content'] !== '@return' && $tokens[$tag]['content'] !== '@throws') { + continue; } - // Check @return - $return = $comment_parser->getReturn(); - if ($return !== null) + $classes = $tokens[($tag + 2)]['content']; + $space = strpos($classes, ' '); + if ($space !== false) { + $classes = substr($classes, 0, $space); + } + + $tab = strpos($classes, "\t"); + if ($tab !== false) { + $classes = substr($classes, 0, $tab); + } + + $classes = explode('|', str_replace('[]', '', $classes)); + foreach ($classes as $class) { - $type = $return->getValue(); - $types = explode('|', str_replace('[]', '', $type)); - foreach ($types as $type) - { - $ok = $this->check($phpcsFile, $type, $class_name_full, $class_name_short, $return->getLine() + $comment_start) ? true : $ok; - } + $ok = $this->check($phpcsFile, $class, $class_name_full, $class_name_short, $tokens[$tag + 2]['line']) ? true : $ok; } } - catch (PHP_CodeSniffer_CommentParser_ParserException $e) - { - $line = ($e->getLineWithinComment() + $comment_start); - $phpcsFile->addError($e->getMessage(), $line, 'FailedParse'); - } } } @@ -195,6 +184,20 @@ class phpbb_Sniffs_Namespaces_UnusedUseSniff implements PHP_CodeSniffer_Sniff } } + // Checks in catch blocks + $old_catch = $stackPtr; + while (($catch = $phpcsFile->findNext(T_CATCH, ($old_catch + 1))) !== false) + { + $old_catch = $catch; + + $caught_class_name_start = $phpcsFile->findNext(array(T_NS_SEPARATOR, T_STRING), $catch + 1); + $caught_class_name_end = $phpcsFile->findNext($find, $caught_class_name_start + 1, null, true); + + $caught_class_name = trim($phpcsFile->getTokensAsString($caught_class_name_start, ($caught_class_name_end - $caught_class_name_start))); + + $ok = $this->check($phpcsFile, $caught_class_name, $class_name_full, $class_name_short, $catch) ? true : $ok; + } + if (!$ok) { $error = 'There must not be unused USE statements.'; |