diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php index 7bd1f178..95f43253 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php @@ -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. @@ -182,6 +189,20 @@ public function process(File $phpcsFile, $stackPtr) }//end process() + /** + * Determine if a token is a '@phpstan-' control comment. + * + * @param array $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. * diff --git a/tests/Drupal/good/good.php b/tests/Drupal/good/good.php index 42c269ae..c72c5685 100644 --- a/tests/Drupal/good/good.php +++ b/tests/Drupal/good/good.php @@ -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() { +}