Skip to content

Commit ce8194d

Browse files
authored
fix(FunctionComment): Fix handling of multiple attribute lines (#3345952)
1 parent 1609318 commit ce8194d

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,23 @@ public function register()
8989
public function process(File $phpcsFile, $stackPtr)
9090
{
9191
$tokens = $phpcsFile->getTokens();
92-
$find = Tokens::$methodPrefixes;
93-
$find[] = T_WHITESPACE;
92+
$ignore = Tokens::$methodPrefixes;
93+
$ignore[T_WHITESPACE] = T_WHITESPACE;
94+
$functionCodeStart = $stackPtr;
9495

95-
$beforeFunction = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
96-
if ($tokens[$beforeFunction]['code'] === T_ATTRIBUTE_END
97-
&& $tokens[$tokens[$beforeFunction]['attribute_opener']]['code'] === T_ATTRIBUTE
98-
) {
99-
// It's an attribute, such as #[\ReturnTypeWillChange].
100-
$attributeLines = ($tokens[$beforeFunction]['line'] - $tokens[$tokens[$beforeFunction]['attribute_opener']]['line'] + 1);
101-
$commentEnd = $phpcsFile->findPrevious($find, ($tokens[$beforeFunction]['attribute_opener'] - 1), null, true);
102-
} else {
103-
$attributeLines = 0;
104-
$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
96+
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
97+
if (isset($ignore[$tokens[$commentEnd]['code']]) === true) {
98+
continue;
99+
}
100+
101+
if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
102+
&& isset($tokens[$commentEnd]['attribute_opener']) === true
103+
) {
104+
$commentEnd = $functionCodeStart = $tokens[$commentEnd]['attribute_opener'];
105+
continue;
106+
}
107+
108+
break;
105109
}
106110

107111
$beforeCommentEnd = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($commentEnd - 1), null, true);
@@ -162,7 +166,7 @@ public function process(File $phpcsFile, $stackPtr)
162166
}
163167
}//end foreach
164168

165-
if ($tokens[$commentEnd]['line'] !== ($tokens[$stackPtr]['line'] - $attributeLines - 1)) {
169+
if ($tokens[$commentEnd]['line'] !== ($tokens[$functionCodeStart]['line'] - 1)) {
166170
$error = 'There must be no blank lines after the function comment';
167171
$fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter');
168172
if ($fix === true) {

tests/Drupal/Commenting/FunctionCommentUnitTest.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,18 @@ class Test40 {
544544
}
545545

546546
}
547+
548+
/**
549+
* Test PHP attributes.
550+
*/
551+
class Test41 {
552+
553+
/**
554+
* Method docblock.
555+
*/
556+
#[Some\Attribute(foo: 'bar')]
557+
#[Other\Attribute(baz: 'qux')]
558+
public function method() {
559+
}
560+
561+
}

tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,18 @@ class Test40 {
570570
}
571571

572572
}
573+
574+
/**
575+
* Test PHP attributes.
576+
*/
577+
class Test41 {
578+
579+
/**
580+
* Method docblock.
581+
*/
582+
#[Some\Attribute(foo: 'bar')]
583+
#[Other\Attribute(baz: 'qux')]
584+
public function method() {
585+
}
586+
587+
}

0 commit comments

Comments
 (0)