diff --git a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php index 4fb332cf48..8f54a36945 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php @@ -47,10 +47,24 @@ public function process(File $phpcsFile, $stackPtr) return; } - $find = Tokens::$methodPrefixes; - $find[] = T_WHITESPACE; + $ignore = Tokens::$methodPrefixes; + $ignore[T_WHITESPACE] = T_WHITESPACE; + + for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) { + if (isset($ignore[$tokens[$commentEnd]['code']]) === true) { + continue; + } + + if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END + && isset($tokens[$commentEnd]['attribute_opener']) === true + ) { + $commentEnd = $tokens[$commentEnd]['attribute_opener']; + continue; + } + + break; + } - $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) { // Function doesn't have a doc comment or is using the wrong type of comment. return; diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.inc index 7e94bb2653..0faab6c5bb 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.inc @@ -509,3 +509,17 @@ namespace { } } } + +$anon = new class { + /** + * Tag and token number mismatch. + * + * @throws PHP_Exception1 + * @throws PHP_Exception2 + */ + #[AnAttribute] + #[AnotherAttribute] + public function JumpOverAttributesToFindDocblock() { + throw new PHP_Exception1('Error'); + } +}; diff --git a/src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.php b/src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.php index 178b85a267..e026e21dcc 100644 --- a/src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.php +++ b/src/Standards/Squiz/Tests/Commenting/FunctionCommentThrowTagUnitTest.php @@ -42,6 +42,7 @@ public function getErrorList() 219 => 1, 287 => 1, 397 => 1, + 519 => 1, ]; }//end getErrorList()