Skip to content

Fix Comment block is missing Failure when Attribute is present after the docblock #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Magento2/Sniffs/Annotation/MethodArgumentsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions Magento2/Tests/Annotation/MethodArgumentsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function getErrorList()
12 => 1,
21 => 1,
32 => 1,
50 => 1
];
}

Expand Down