From 7fb3676957fa8e489a49dc446c356fef888b4d60 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 27 Oct 2013 17:14:20 +0100 Subject: [task/code-sniffer] Move code sniffer below build folder. PHPBB3-11980 --- .../phpbb/Sniffs/Commenting/FileCommentSniff.php | 210 +++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php (limited to 'build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php') diff --git a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php new file mode 100644 index 0000000000..68e9e6bb86 --- /dev/null +++ b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php @@ -0,0 +1,210 @@ + +*/ +class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff +{ + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() + { + return array(T_OPEN_TAG); + } + + /** + * 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 null + */ + public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) + { + // We are only interested in the first file comment. + if ($stackPtr !== 0) + { + if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) + { + return; + } + } + + // Fetch next non whitespace token + $tokens = $phpcsFile->getTokens(); + $start = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); + + // Skip empty files + if ($tokens[$start]['code'] === T_CLOSE_TAG) + { + return; + } + // Mark as error if this is not a doc comment + else if ($start === false || $tokens[$start]['code'] !== T_DOC_COMMENT) + { + $phpcsFile->addError('Missing required file doc comment.', $stackPtr); + return; + } + + // Find comment end token + $end = $phpcsFile->findNext(T_DOC_COMMENT, $start + 1, null, true) - 1; + + // If there is no end, skip processing here + if ($end === false) + { + return; + } + + // List of found comment tags + $tags = array(); + + // check comment lines without the first(/**) an last(*/) line + for ($i = $start + 1, $c = ($end - $start); $i <= $c; ++$i) + { + $line = $tokens[$i]['content']; + + // Check that each line starts with a '*' + if (substr($line, 0, 1) !== '*') + { + $message = 'The file doc comment should not be idented.'; + $phpcsFile->addWarning($message, $i); + } + else if (preg_match('/^\*\s+@([\w]+)\s+(.*)$/', $line, $match) !== 0) + { + $tags[$match[1]] = array($match[2], $i); + } + } + + // Check that the first and last line is empty + if (trim($tokens[$start + 1]['content']) !== '*') + { + $message = 'The first file comment line should be empty.'; + $phpcsFile->addWarning($message, ($start + 1)); + } + if (trim($tokens[$end - $start]['content']) !== '*') + { + $message = 'The last file comment line should be empty.'; + $phpcsFile->addWarning($message, ($end - $start)); + } + + $this->processPackage($phpcsFile, $start, $tags); + $this->processVersion($phpcsFile, $start, $tags); + $this->processCopyright($phpcsFile, $start, $tags); + $this->processLicense($phpcsFile, $start, $tags); + + //print_r($tags); + } + + /** + * Checks that the tags array contains a valid package tag + * + * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. + * @param integer The stack pointer for the first comment token. + * @param array(string=>array) $tags The found file doc comment tags. + * + * @return null + */ + protected function processPackage(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) + { + if (!isset($tags['package'])) + { + $message = 'Missing require @package tag in file doc comment.'; + $phpcsFile->addError($message, $ptr); + } + else if (preg_match('/^([\w]+)$/', $tags['package'][0]) === 0) + { + $message = 'Invalid content found for @package tag.'; + $phpcsFile->addWarning($message, $tags['package'][1]); + } + } + + /** + * Checks that the tags array contains a valid version tag + * + * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. + * @param integer The stack pointer for the first comment token. + * @param array(string=>array) $tags The found file doc comment tags. + * + * @return null + */ + protected function processVersion(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) + { + if (!isset($tags['version'])) + { + $message = 'Missing require @version tag in file doc comment.'; + $phpcsFile->addError($message, $ptr); + } + else if (preg_match('/^\$Id:[^\$]+\$$/', $tags['version'][0]) === 0) + { + $message = 'Invalid content found for @version tag, use "$Id: $".'; + $phpcsFile->addError($message, $tags['version'][1]); + } + } + + /** + * Checks that the tags array contains a valid copyright tag + * + * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. + * @param integer The stack pointer for the first comment token. + * @param array(string=>array) $tags The found file doc comment tags. + * + * @return null + */ + protected function processCopyright(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) + { + if (!isset($tags['copyright'])) + { + $message = 'Missing require @copyright tag in file doc comment.'; + $phpcsFile->addError($message, $ptr); + } + else if (preg_match('/^\(c\) 2[0-9]{3} phpBB Group\s*$/', $tags['copyright'][0]) === 0) + { + $message = 'Invalid content found for @copyright tag, use "(c) phpBB Group".'; + $phpcsFile->addError($message, $tags['copyright'][1]); + } + } + + /** + * Checks that the tags array contains a valid license tag + * + * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. + * @param integer The stack pointer for the first comment token. + * @param array(string=>array) $tags The found file doc comment tags. + * + * @return null + */ + protected function processLicense(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) + { + $license = 'http://opensource.org/licenses/gpl-license.php GNU Public License'; + + if (!isset($tags['license'])) + { + $message = 'Missing require @license tag in file doc comment.'; + $phpcsFile->addError($message, $ptr); + } + else if (trim($tags['license'][0]) !== $license) + { + $message = 'Invalid content found for @license tag, use ' + . '"' . $license . '".'; + $phpcsFile->addError($message, $tags['license'][1]); + } + } +} -- cgit v1.2.1 From e39c4f0d2009615a251601881e910f9329483773 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 3 Apr 2014 15:45:59 +0200 Subject: [ticket/12349] Fix Sniffer for @license tag PHPBB3-12349 --- build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php') diff --git a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php index 68e9e6bb86..231e77734e 100644 --- a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php @@ -193,7 +193,7 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff */ protected function processLicense(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) { - $license = 'http://opensource.org/licenses/gpl-license.php GNU Public License'; + $license = 'http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2'; if (!isset($tags['license'])) { -- cgit v1.2.1 From a759704b39fc1c1353f865a633759b1369589b67 Mon Sep 17 00:00:00 2001 From: Yuriy Rusko Date: Tue, 27 May 2014 20:18:06 +0200 Subject: [ticket/12594] Remove @package tags and update file headers PHPBB3-12594 --- .../phpbb/Sniffs/Commenting/FileCommentSniff.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php') diff --git a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php index 231e77734e..fcf53b5061 100644 --- a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php @@ -1,10 +1,13 @@ +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. * */ -- cgit v1.2.1 From 32a2c95f903cbbfad909945887a1cbabd84d5039 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Fri, 20 Jun 2014 15:02:08 +0200 Subject: [ticket/12723] Add Sniff ensuring PHP files use the correct file header PHPBB3-12723 --- .../phpbb/Sniffs/Commenting/FileCommentSniff.php | 222 +++++++++++---------- 1 file changed, 116 insertions(+), 106 deletions(-) (limited to 'build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php') diff --git a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php index fcf53b5061..1f64467d00 100644 --- a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php @@ -13,7 +13,7 @@ /** * Checks that each source file contains the standard header. -* +* * Based on Coding Guidelines 1.ii File Header. * * @package code_sniffer @@ -47,10 +47,10 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff { if ($phpcsFile->findPrevious(T_OPEN_TAG, $stackPtr - 1) !== false) { - return; + return; } } - + // Fetch next non whitespace token $tokens = $phpcsFile->getTokens(); $start = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true); @@ -66,65 +66,68 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff $phpcsFile->addError('Missing required file doc comment.', $stackPtr); return; } - + // Find comment end token $end = $phpcsFile->findNext(T_DOC_COMMENT, $start + 1, null, true) - 1; - + // If there is no end, skip processing here if ($end === false) { return; } - + // List of found comment tags $tags = array(); - + // check comment lines without the first(/**) an last(*/) line - for ($i = $start + 1, $c = ($end - $start); $i <= $c; ++$i) + for ($i = $start + 1, $c = $end - 1; $i <= $c; ++$i) { $line = $tokens[$i]['content']; // Check that each line starts with a '*' if (substr($line, 0, 1) !== '*') { - $message = 'The file doc comment should not be idented.'; + $message = 'The file doc comment should not be idented.'; $phpcsFile->addWarning($message, $i); } else if (preg_match('/^\*\s+@([\w]+)\s+(.*)$/', $line, $match) !== 0) { - $tags[$match[1]] = array($match[2], $i); + if (!isset($tags[$match[1]])) + { + $tags[$match[1]] = array(); + } + + $tags[$match[1]][] = array($match[2], $i); } } - + // Check that the first and last line is empty if (trim($tokens[$start + 1]['content']) !== '*') { $message = 'The first file comment line should be empty.'; - $phpcsFile->addWarning($message, ($start + 1)); - } - if (trim($tokens[$end - $start]['content']) !== '*') - { - $message = 'The last file comment line should be empty.'; - $phpcsFile->addWarning($message, ($end - $start)); - } - - $this->processPackage($phpcsFile, $start, $tags); - $this->processVersion($phpcsFile, $start, $tags); - $this->processCopyright($phpcsFile, $start, $tags); - $this->processLicense($phpcsFile, $start, $tags); - - //print_r($tags); + $phpcsFile->addWarning($message, ($start + 1)); + } + if (trim($tokens[$end - 1]['content']) !== '*') + { + $message = 'The last file comment line should be empty.'; + $phpcsFile->addWarning($message, $end - 1); + } + + //$this->processPackage($phpcsFile, $start, $tags); + //$this->processVersion($phpcsFile, $start, $tags); + $this->processCopyright($phpcsFile, $start, $tags); + $this->processLicense($phpcsFile, $start, $tags); } - + /** - * Checks that the tags array contains a valid package tag - * - * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. - * @param integer The stack pointer for the first comment token. - * @param array(string=>array) $tags The found file doc comment tags. - * - * @return null - */ + * Checks that the tags array contains a valid package tag + * + * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. + * @param integer The stack pointer for the first comment token. + * @param array(string=>array) $tags The found file doc comment tags. + * + * @return null + */ protected function processPackage(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) { if (!isset($tags['package'])) @@ -134,80 +137,87 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff } else if (preg_match('/^([\w]+)$/', $tags['package'][0]) === 0) { - $message = 'Invalid content found for @package tag.'; - $phpcsFile->addWarning($message, $tags['package'][1]); + $message = 'Invalid content found for @package tag.'; + $phpcsFile->addWarning($message, $tags['package'][1]); } } - + /** - * Checks that the tags array contains a valid version tag - * - * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. - * @param integer The stack pointer for the first comment token. - * @param array(string=>array) $tags The found file doc comment tags. - * - * @return null - */ + * Checks that the tags array contains a valid version tag + * + * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. + * @param integer The stack pointer for the first comment token. + * @param array(string=>array) $tags The found file doc comment tags. + * + * @return null + */ protected function processVersion(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) - { - if (!isset($tags['version'])) - { - $message = 'Missing require @version tag in file doc comment.'; - $phpcsFile->addError($message, $ptr); - } - else if (preg_match('/^\$Id:[^\$]+\$$/', $tags['version'][0]) === 0) - { - $message = 'Invalid content found for @version tag, use "$Id: $".'; - $phpcsFile->addError($message, $tags['version'][1]); - } - } - - /** - * Checks that the tags array contains a valid copyright tag - * - * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. - * @param integer The stack pointer for the first comment token. - * @param array(string=>array) $tags The found file doc comment tags. - * - * @return null - */ - protected function processCopyright(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) - { - if (!isset($tags['copyright'])) - { - $message = 'Missing require @copyright tag in file doc comment.'; - $phpcsFile->addError($message, $ptr); - } - else if (preg_match('/^\(c\) 2[0-9]{3} phpBB Group\s*$/', $tags['copyright'][0]) === 0) - { - $message = 'Invalid content found for @copyright tag, use "(c) phpBB Group".'; - $phpcsFile->addError($message, $tags['copyright'][1]); - } - } - - /** - * Checks that the tags array contains a valid license tag - * - * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. - * @param integer The stack pointer for the first comment token. - * @param array(string=>array) $tags The found file doc comment tags. - * - * @return null - */ - protected function processLicense(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) - { - $license = 'http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2'; - - if (!isset($tags['license'])) - { - $message = 'Missing require @license tag in file doc comment.'; - $phpcsFile->addError($message, $ptr); - } - else if (trim($tags['license'][0]) !== $license) - { - $message = 'Invalid content found for @license tag, use ' - . '"' . $license . '".'; - $phpcsFile->addError($message, $tags['license'][1]); - } - } + { + if (!isset($tags['version'])) + { + $message = 'Missing require @version tag in file doc comment.'; + $phpcsFile->addError($message, $ptr); + } + else if (preg_match('/^\$Id:[^\$]+\$$/', $tags['version'][0]) === 0) + { + $message = 'Invalid content found for @version tag, use "$Id: $".'; + $phpcsFile->addError($message, $tags['version'][1]); + } + } + + /** + * Checks that the tags array contains a valid copyright tag + * + * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. + * @param integer The stack pointer for the first comment token. + * @param array(string=>array) $tags The found file doc comment tags. + * + * @return null + */ + protected function processCopyright(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) + { + $copyright = '(c) phpBB Limited '; + + if (!isset($tags['copyright'])) + { + $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]); + } + } + + /** + * Checks that the tags array contains a valid license tag + * + * @param PHP_CodeSniffer_File $phpcsFile The context source file instance. + * @param integer The stack pointer for the first comment token. + * @param array(string=>array) $tags The found file doc comment tags. + * + * @return null + */ + protected function processLicense(PHP_CodeSniffer_File $phpcsFile, $ptr, $tags) + { + $license = 'GNU General Public License, version 2 (GPL-2.0)'; + + if (!isset($tags['license'])) + { + $message = 'Missing require @license tag in file doc comment.'; + $phpcsFile->addError($message, $ptr); + } + else if (sizeof($tags['license']) !== 1) + { + $message = 'It must be only one @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]); + } + } } -- cgit v1.2.1 From e10bf39d08cd3f613296698cbf7b00536f15e8db Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 22 Jun 2014 01:42:43 +0200 Subject: [ticket/12723] Do not reference the coding guidelines section. PHPBB3-12723 --- build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php') diff --git a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php index 1f64467d00..fa7d3b40c1 100644 --- a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php @@ -12,9 +12,8 @@ */ /** -* Checks that each source file contains the standard header. -* -* Based on Coding Guidelines 1.ii File Header. +* Checks that each PHP source file contains a valid header as defined by the +* phpBB Coding Guidelines. * * @package code_sniffer * @author Manuel Pichler -- cgit v1.2.1 From 79ce924bc10cd528b3d9287bfcfaa96f96b88bc2 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 27 Nov 2014 11:20:26 +0100 Subject: [ticket/13381] Allow ' *' in the doc blocks (especially the header files) PHPBB3-13381 --- build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php') diff --git a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php index fa7d3b40c1..8337cf02ee 100644 --- a/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php +++ b/build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php @@ -84,12 +84,12 @@ class phpbb_Sniffs_Commenting_FileCommentSniff implements PHP_CodeSniffer_Sniff $line = $tokens[$i]['content']; // Check that each line starts with a '*' - if (substr($line, 0, 1) !== '*') + if (substr($line, 0, 1) !== '*' && substr($line, 0, 2) !== ' *') { - $message = 'The file doc comment should not be idented.'; + $message = 'The file doc comment should not be indented.'; $phpcsFile->addWarning($message, $i); } - else if (preg_match('/^\*\s+@([\w]+)\s+(.*)$/', $line, $match) !== 0) + else if (preg_match('/^[ ]?\*\s+@([\w]+)\s+(.*)$/', $line, $match) !== 0) { if (!isset($tags[$match[1]])) { -- cgit v1.2.1 From a462f14aa367e9b8f94c4d2da35458353bad8c0f Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 28 Apr 2015 01:01:09 +0200 Subject: [ticket/13790] Update phpcs PHPBB3-13790 --- .../phpbb/Sniffs/Commenting/FileCommentSniff.php | 94 ++++++++++++---------- 1 file changed, 52 insertions(+), 42 deletions(-) (limited to 'build/code_sniffer/phpbb/Sniffs/Commenting/FileCommentSniff.php') 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 '; + $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]); - } } } -- cgit v1.2.1