cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting ClassCommentSniff.php FileCommentSniff.php
/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting FileCommentUnitTest.inc FileCommentUnitTest.php
| From: | Greg Sherwood | Date: | Wed, 24 Jan 2007 22:23:22 +0000 |
| Subject: | cvs: pear /PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting ClassCommentSniff.php FileCommentSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting FileCommentUnitTest.inc FileCommentUnitTest.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-44375@lists.php.net to get a copy of this message | ||
squiz Wed Jan 24 22:23:22 2007 UTC
Modified files:
/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting
ClassCommentSniff.php
FileCommentSniff.php
/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting
FileCommentUnitTest.inc
FileCommentUnitTest.php
Log:
Added newline checks for file and class doc comment; Updated test case
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php?r1=1.8&r2=1.9&diff_format=u Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php:1.8 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php:1.9 --- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php:1.8 Mon Jan 15 00:34:55 2007 +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php Wed Jan 24 22:23:21 2007 @@ -10,7 +10,7 @@ * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence - * @version CVS: $Id: ClassCommentSniff.php,v 1.8 2007/01/15 00:34:55 squiz Exp $ + * @version CVS: $Id: ClassCommentSniff.php,v 1.9 2007/01/24 22:23:21 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ @@ -22,7 +22,8 @@ * * Verifies that : * <ul> - * <li>A file doc comment exists.</li> + * <li>A class doc comment exists.</li> + * <li>There is exactly one blank line before the class comment.</li> * <li>Short description ends with a full stop.</li> * <li>There is a blank line after the short description.</li> * <li>Each paragraph of the long description ends with a full stop.</li> @@ -88,7 +89,6 @@ $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1); $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, "\n"); - //echo "($commentEnd,$stackPtr) class comment Next: $commentNext ... ".print_r($tokens[$commentNext],1); // Distinguish file and class comment. $prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1)); @@ -110,6 +110,22 @@ } }//end if }//end if + + // Exactly one blank line before the class comment + $prevTokenEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true); + if ($prevTokenEnd !== false) { + $blankLineBefore = 0; + for ($i = ($prevTokenEnd + 1); $i < $commentStart; $i++) { + if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === "\n") { + $blankLineBefore++; + } + } + if ($blankLineBefore !== 2) { + $error = 'There must be exactly one blank line before the class comment'; + $phpcsFile->addError($error, ($commentStart - 1)); + } + } + }//end if }//end if http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php?r1=1.8&r2=1.9&diff_format=u Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php:1.8 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php:1.9 --- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php:1.8 Mon Jan 15 00:34:55 2007 +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php Wed Jan 24 22:23:22 2007 @@ -10,7 +10,7 @@ * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence - * @version CVS: $Id: FileCommentSniff.php,v 1.8 2007/01/15 00:34:55 squiz Exp $ + * @version CVS: $Id: FileCommentSniff.php,v 1.9 2007/01/24 22:23:22 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ @@ -23,11 +23,13 @@ * Verifies that : * <ul> * <li>A file doc comment exists.</li> + * <li>There is no blank line between the open tag and the file comment.</li> * <li>Short description ends with a full stop.</li> * <li>There is a blank line after the short description.</li> * <li>Each paragraph of the long description ends with a full stop.</li> * <li>There is a blank line between the description and the tags.</li> * <li>Check the order, indentation and content of each tag.</li> + <li>There is exactly one blank line after the file comment.</li> * </ul> * * @category PHP @@ -94,12 +96,6 @@ // Find the next non whitespace token. $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); - // Ignore vim header. - if ($tokens[$commentStart]['code'] === T_COMMENT) { - if (strstr($tokens[$commentStart]['content'], 'vim:') !== false) { - $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($commentStart + 1), null, true); - } - } if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) { // We are only interested if this is the first open tag. @@ -136,6 +132,28 @@ } } + // No blank line between the open tag and the file comment. + $blankLineBefore = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, false, "\n"); + if ($blankLineBefore !== false && $blankLineBefore < $commentStart) { + $error = 'Extra newline found after the open tag'; + $phpcsFile->addError($error, ($stackPtr + 1)); + } + + // Exactly one blank line after the file comment + $nextTokenStart = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true); + if ($nextTokenStart !== false) { + $blankLineAfter = 0; + for ($i = ($commentEnd + 1); $i < $nextTokenStart; $i++) { + if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === "\n") { + $blankLineAfter++; + } + } + if ($blankLineAfter !== 2) { + $error = 'There must be exactly one blank line after the file comment'; + $phpcsFile->addError($error, ($commentEnd + 1)); + } + } + $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1)); // Parse the header comment docblock. http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc:1.2 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc:1.3 --- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc:1.2 Wed Jan 10 03:14:44 2007 +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc Wed Jan 24 22:23:22 2007 @@ -1,6 +1,6 @@ <?php -/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ + /** * http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php:1.2 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php:1.3 --- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php:1.2 Wed Jan 10 03:14:44 2007 +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php Wed Jan 24 22:23:22 2007 @@ -10,7 +10,7 @@ * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence - * @version CVS: $Id: FileCommentUnitTest.php,v 1.2 2007/01/10 03:14:44 squiz Exp $ + * @version CVS: $Id: FileCommentUnitTest.php,v 1.3 2007/01/24 22:23:22 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ @@ -52,6 +52,7 @@ public function getErrorList() { return array( + 2 => 1, 6 => 3, 8 => 1, 21 => 1, @@ -61,6 +62,7 @@ 26 => 2, 27 => 3, 28 => 2, + 30 => 1, ); }//end getErrorList()
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php?r1=1.8&r2=1.9&diff_format=u Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php:1.8 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php:1.9 --- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php:1.8 Mon Jan 15 00:34:55 2007 +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/ClassCommentSniff.php Wed Jan 24 22:23:21 2007 @@ -10,7 +10,7 @@ * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence - * @version CVS: $Id: ClassCommentSniff.php,v 1.8 2007/01/15 00:34:55 squiz Exp $ + * @version CVS: $Id: ClassCommentSniff.php,v 1.9 2007/01/24 22:23:21 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ @@ -22,7 +22,8 @@ * * Verifies that : * <ul> - * <li>A file doc comment exists.</li> + * <li>A class doc comment exists.</li> + * <li>There is exactly one blank line before the class comment.</li> * <li>Short description ends with a full stop.</li> * <li>There is a blank line after the short description.</li> * <li>Each paragraph of the long description ends with a full stop.</li> @@ -88,7 +89,6 @@ $commentStart = ($phpcsFile->findPrevious(T_DOC_COMMENT, ($commentEnd - 1), null, true) + 1); $commentNext = $phpcsFile->findPrevious(T_WHITESPACE, ($commentEnd + 1), $stackPtr, false, "\n"); - //echo "($commentEnd,$stackPtr) class comment Next: $commentNext ... ".print_r($tokens[$commentNext],1); // Distinguish file and class comment. $prevClassToken = $phpcsFile->findPrevious(T_CLASS, ($stackPtr - 1)); @@ -110,6 +110,22 @@ } }//end if }//end if + + // Exactly one blank line before the class comment + $prevTokenEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($commentStart - 1), null, true); + if ($prevTokenEnd !== false) { + $blankLineBefore = 0; + for ($i = ($prevTokenEnd + 1); $i < $commentStart; $i++) { + if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === "\n") { + $blankLineBefore++; + } + } + if ($blankLineBefore !== 2) { + $error = 'There must be exactly one blank line before the class comment'; + $phpcsFile->addError($error, ($commentStart - 1)); + } + } + }//end if }//end if http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php?r1=1.8&r2=1.9&diff_format=u Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php:1.8 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php:1.9 --- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php:1.8 Mon Jan 15 00:34:55 2007 +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Commenting/FileCommentSniff.php Wed Jan 24 22:23:22 2007 @@ -10,7 +10,7 @@ * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence - * @version CVS: $Id: FileCommentSniff.php,v 1.8 2007/01/15 00:34:55 squiz Exp $ + * @version CVS: $Id: FileCommentSniff.php,v 1.9 2007/01/24 22:23:22 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ @@ -23,11 +23,13 @@ * Verifies that : * <ul> * <li>A file doc comment exists.</li> + * <li>There is no blank line between the open tag and the file comment.</li> * <li>Short description ends with a full stop.</li> * <li>There is a blank line after the short description.</li> * <li>Each paragraph of the long description ends with a full stop.</li> * <li>There is a blank line between the description and the tags.</li> * <li>Check the order, indentation and content of each tag.</li> + <li>There is exactly one blank line after the file comment.</li> * </ul> * * @category PHP @@ -94,12 +96,6 @@ // Find the next non whitespace token. $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); - // Ignore vim header. - if ($tokens[$commentStart]['code'] === T_COMMENT) { - if (strstr($tokens[$commentStart]['content'], 'vim:') !== false) { - $commentStart = $phpcsFile->findNext(T_WHITESPACE, ($commentStart + 1), null, true); - } - } if ($tokens[$commentStart]['code'] === T_CLOSE_TAG) { // We are only interested if this is the first open tag. @@ -136,6 +132,28 @@ } } + // No blank line between the open tag and the file comment. + $blankLineBefore = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, false, "\n"); + if ($blankLineBefore !== false && $blankLineBefore < $commentStart) { + $error = 'Extra newline found after the open tag'; + $phpcsFile->addError($error, ($stackPtr + 1)); + } + + // Exactly one blank line after the file comment + $nextTokenStart = $phpcsFile->findNext(T_WHITESPACE, ($commentEnd + 1), null, true); + if ($nextTokenStart !== false) { + $blankLineAfter = 0; + for ($i = ($commentEnd + 1); $i < $nextTokenStart; $i++) { + if ($tokens[$i]['code'] === T_WHITESPACE && $tokens[$i]['content'] === "\n") { + $blankLineAfter++; + } + } + if ($blankLineAfter !== 2) { + $error = 'There must be exactly one blank line after the file comment'; + $phpcsFile->addError($error, ($commentEnd + 1)); + } + } + $comment = $phpcsFile->getTokensAsString($commentStart, ($commentEnd - $commentStart + 1)); // Parse the header comment docblock. http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc:1.2 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc:1.3 --- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc:1.2 Wed Jan 10 03:14:44 2007 +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.inc Wed Jan 24 22:23:22 2007 @@ -1,6 +1,6 @@ <?php -/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ + /** * http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php diff -u pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php:1.2 pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php:1.3 --- pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php:1.2 Wed Jan 10 03:14:44 2007 +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.php Wed Jan 24 22:23:22 2007 @@ -10,7 +10,7 @@ * @author Marc McIntyre <mmcintyre@squiz.net> * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence - * @version CVS: $Id: FileCommentUnitTest.php,v 1.2 2007/01/10 03:14:44 squiz Exp $ + * @version CVS: $Id: FileCommentUnitTest.php,v 1.3 2007/01/24 22:23:22 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ @@ -52,6 +52,7 @@ public function getErrorList() { return array( + 2 => 1, 6 => 3, 8 => 1, 21 => 1, @@ -61,6 +62,7 @@ 26 => 2, 27 => 3, 28 => 2, + 30 => 1, ); }//end getErrorList()