Skip to content

Issue #3516489 disregard phpstan-ignore comments in front of function declaration #263

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
merged 13 commits into from
May 24, 2025
Merged
23 changes: 22 additions & 1 deletion coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,15 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}

// If there is a phpstan-ignore inline comment disregard it and continue searching backwards
// to find the function comment.
if ($this->tokenIsPhpstanComment($tokens[$commentEnd]) === true) {
$functionCodeStart = $commentEnd;
continue;
}

break;
}
}//end for

// Constructor methods are exempt from requiring a docblock.
// @see https://www.drupal.org/project/coder/issues/3400560.
Expand Down Expand Up @@ -182,6 +189,20 @@ public function process(File $phpcsFile, $stackPtr)
}//end process()


/**
* Determine if a token is a '@phpstan-' control comment.
*
* @param array<mixed> $token The token to be checked.
*
* @return bool True if the token contains a @phpstan comment.
*/
public static function tokenIsPhpstanComment($token)
{
return ($token['code'] === T_COMMENT && strpos($token['content'], ' @phpstan-') !== false);

}//end tokenIsPhpstanComment()


/**
* Process the return comment of this function comment.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Drupal/good/good.php
Original file line number Diff line number Diff line change
Expand Up @@ -2003,3 +2003,20 @@ public function &get($instance_id) {
}

}

/**
* Test for @phpstan-ignore-next-line.
*
* Coder issue https://www.drupal.org/project/coder/issues/3516489
*/
// @phpstan-ignore-next-line missingType.return
public function ignore_phpstan_comment() {
}

/**
* Test for @phpstan-ignore with attribute before.
*/
#[ExampleAttribute('foo', 'bar')]
// @phpstan-ignore-next-line missingType.return
public function ignore_phpstan_comment_with_attribute_before() {
}