Skip to content

Commit 5349ce9

Browse files
authored
Merge pull request #936 from PHPCSStandards/feature/squiz-functioncommentthrowtag-improve-comment-tolerance
Squiz/FunctionCommentThrowTag: bug fix - jump over attributes
2 parents 3c90cda + f358e6f commit 5349ce9

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,24 @@ public function process(File $phpcsFile, $stackPtr)
4747
return;
4848
}
4949

50-
$find = Tokens::$methodPrefixes;
51-
$find[] = T_WHITESPACE;
50+
$ignore = Tokens::$methodPrefixes;
51+
$ignore[T_WHITESPACE] = T_WHITESPACE;
52+
53+
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
54+
if (isset($ignore[$tokens[$commentEnd]['code']]) === true) {
55+
continue;
56+
}
57+
58+
if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
59+
&& isset($tokens[$commentEnd]['attribute_opener']) === true
60+
) {
61+
$commentEnd = $tokens[$commentEnd]['attribute_opener'];
62+
continue;
63+
}
64+
65+
break;
66+
}
5267

53-
$commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
5468
if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
5569
// Function doesn't have a doc comment or is using the wrong type of comment.
5670
return;

src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,17 @@ namespace {
509509
}
510510
}
511511
}
512+
513+
$anon = new class {
514+
/**
515+
* Tag and token number mismatch.
516+
*
517+
* @throws PHP_Exception1
518+
* @throws PHP_Exception2
519+
*/
520+
#[AnAttribute]
521+
#[AnotherAttribute]
522+
public function JumpOverAttributesToFindDocblock() {
523+
throw new PHP_Exception1('Error');
524+
}
525+
};

src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function getErrorList()
4242
219 => 1,
4343
287 => 1,
4444
397 => 1,
45+
519 => 1,
4546
];
4647

4748
}//end getErrorList()

0 commit comments

Comments
 (0)