diff --git a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php index cf483356..27ac0dfd 100644 --- a/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php +++ b/Magento2/Sniffs/Annotation/MethodArgumentsSniff.php @@ -70,7 +70,23 @@ private function isTokenBeforeClosingCommentTagValid(string $type): bool private function validateCommentBlockExists(File $phpcsFile, int $previousCommentClosePtr, int $stackPtr): bool { $tokens = $phpcsFile->getTokens(); + $attributeFlag = false; for ($tempPtr = $previousCommentClosePtr + 1; $tempPtr < $stackPtr; $tempPtr++) { + $tokenCode = $tokens[$tempPtr]['code']; + + // Ignore attributes e.g. #[\ReturnTypeWillChange] + if ($tokenCode === T_ATTRIBUTE_END) { + $attributeFlag = false; + continue; + } + if ($attributeFlag) { + continue; + } + if ($tokenCode === T_ATTRIBUTE) { + $attributeFlag = true; + continue; + } + if (!$this->isTokenBeforeClosingCommentTagValid($tokens[$tempPtr]['type'])) { return false; } diff --git a/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc index 7add3ae9..6694c8cc 100644 --- a/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc +++ b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc @@ -33,3 +33,21 @@ public function invalidDocBlockShouldNotCauseFatalErrorInSniff(int $number): int { return $number; } + +/** + * Short description. + * + * @param string $text + * @return string + */ +#[\ReturnTypeWillChange] +public function methodWithAttributeAndValidDocblock(string $text): string +{ + return $text; +} + +#[\ReturnTypeWillChange] +public function methodWithAttributeAndWithoutDocblock(string $text): string +{ + return $text; +} diff --git a/Magento2/Tests/Annotation/MethodArgumentsUnitTest.php b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.php index 86854e83..b47c4159 100644 --- a/Magento2/Tests/Annotation/MethodArgumentsUnitTest.php +++ b/Magento2/Tests/Annotation/MethodArgumentsUnitTest.php @@ -18,6 +18,7 @@ public function getErrorList() 12 => 1, 21 => 1, 32 => 1, + 50 => 1 ]; }