Skip to content

Commit cd30e62

Browse files
committed
FunctionCommentSniff: sync with upstream to fix attributes support
1 parent b51ed0d commit cd30e62

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

Swivl/Sniffs/Commenting/FunctionCommentSniff.php

+31-4
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,24 @@ public function process(File $phpcsFile, $stackPtr)
5454

5555
$tokens = $phpcsFile->getTokens();
5656
$ignore = Tokens::$methodPrefixes;
57-
$ignore[] = T_WHITESPACE;
57+
$ignore[T_WHITESPACE] = T_WHITESPACE;
58+
59+
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
60+
if (isset($ignore[$tokens[$commentEnd]['code']]) === true) {
61+
continue;
62+
}
63+
64+
if (
65+
$tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
66+
&& isset($tokens[$commentEnd]['attribute_opener']) === true
67+
) {
68+
$commentEnd = $tokens[$commentEnd]['attribute_opener'];
69+
continue;
70+
}
71+
72+
break;
73+
}
5874

59-
$commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
6075
if ($tokens[$commentEnd]['code'] === T_COMMENT) {
6176
// Inline comments might just be closing comments for
6277
// control structures or functions instead of function comments
@@ -102,8 +117,20 @@ public function process(File $phpcsFile, $stackPtr)
102117
}
103118

104119
if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - 1)) {
105-
$error = 'There must be no blank lines after the function comment';
106-
$phpcsFile->addError($error, $commentEnd, 'SpacingAfter');
120+
for ($i = ($commentEnd + 1); $i < $stackPtr; $i++) {
121+
if ($tokens[$i]['column'] !== 1) {
122+
continue;
123+
}
124+
125+
if (
126+
$tokens[$i]['code'] === T_WHITESPACE
127+
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line']
128+
) {
129+
$error = 'There must be no blank lines after the function comment';
130+
$phpcsFile->addError($error, $commentEnd, 'SpacingAfter');
131+
break;
132+
}
133+
}
107134
}
108135

109136
$commentStart = $tokens[$commentEnd]['comment_opener'];

0 commit comments

Comments
 (0)