Skip to content
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

PHP 8.0 | Squiz/BlockComment: prevent false positives with attributes #3398

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
14 changes: 12 additions & 2 deletions src/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,18 @@ public function process(File $phpcsFile, $stackPtr)
// If this is a function/class/interface doc block comment, skip it.
// We are only interested in inline doc block comments.
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) {
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
$ignore = [
$nextToken = $stackPtr;
do {
$nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($nextToken + 1), null, true);
if ($tokens[$nextToken]['code'] === T_ATTRIBUTE) {
$nextToken = $tokens[$nextToken]['attribute_closer'];
continue;
}

break;
} while (true);

$ignore = [
T_CLASS => true,
T_INTERFACE => true,
T_TRAIT => true,
Expand Down
16 changes: 16 additions & 0 deletions src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,19 @@ $contentToEcho
* No blank line allowed above the comment if it's the first non-empty token after a PHP open tag.
*/
$contentToEcho

/**
* Comment should be ignored, even though there is an attribute between the docblock and the class declaration.
*/

#[AttributeA]

final class MyClass
{
/**
* Comment should be ignored, even though there is an attribute between the docblock and the function declaration
*/
#[AttributeA]
#[AttributeB]
final public function test() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,19 @@ $contentToEcho
* No blank line allowed above the comment if it's the first non-empty token after a PHP open tag.
*/
$contentToEcho

/**
* Comment should be ignored, even though there is an attribute between the docblock and the class declaration.
*/

#[AttributeA]

final class MyClass
{
/**
* Comment should be ignored, even though there is an attribute between the docblock and the function declaration
*/
#[AttributeA]
#[AttributeB]
final public function test() {}
}