Skip to content

Commit cad109a

Browse files
committed
Squiz FunctionCommentSniff now reports a missing function comment if it finds a standard code comment instead. It does this by assuming standard comment tokens found on the same line as code are actually commenting the code on the line and not code below the line, like a doc comment does.
1 parent b1a24b2 commit cad109a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

CodeSniffer/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,17 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
139139
$code = $tokens[$commentEnd]['code'];
140140

141141
if ($code === T_COMMENT) {
142-
$error = 'You must use "/**" style comments for a function comment';
143-
$phpcsFile->addError($error, $stackPtr, 'WrongStyle');
142+
// The function might actually be missing a comment, and this last comment
143+
// found is just commenting a bit of code on a line. So if it is not the
144+
// only thing on the line, assume we found nothing.
145+
$prevContent = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $commentEnd);
146+
if ($tokens[$commentEnd]['line'] === $tokens[$commentEnd]['line']) {
147+
$error = 'Missing function doc comment';
148+
$phpcsFile->addError($error, $stackPtr, 'Missing');
149+
} else {
150+
$error = 'You must use "/**" style comments for a function comment';
151+
$phpcsFile->addError($error, $stackPtr, 'WrongStyle');
152+
}
144153
return;
145154
} else if ($code !== T_DOC_COMMENT) {
146155
$error = 'Missing function doc comment';

0 commit comments

Comments
 (0)