Skip to content

Commit e8d5f31

Browse files
authored
fix(FunctionComment): Allow phpstan-ignore comments in front of function declarations (##3516489 by jonathan1055)
1 parent dd57647 commit e8d5f31

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,15 @@ public function process(File $phpcsFile, $stackPtr)
9595
continue;
9696
}
9797

98+
// If there is a phpstan-ignore inline comment disregard it and continue searching backwards
99+
// to find the function comment.
100+
if ($this->tokenIsPhpstanComment($tokens[$commentEnd]) === true) {
101+
$functionCodeStart = $commentEnd;
102+
continue;
103+
}
104+
98105
break;
99-
}
106+
}//end for
100107

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

184191

192+
/**
193+
* Determine if a token is a '@phpstan-' control comment.
194+
*
195+
* @param array<mixed> $token The token to be checked.
196+
*
197+
* @return bool True if the token contains a @phpstan comment.
198+
*/
199+
public static function tokenIsPhpstanComment($token)
200+
{
201+
return ($token['code'] === T_COMMENT && strpos($token['content'], ' @phpstan-') !== false);
202+
203+
}//end tokenIsPhpstanComment()
204+
205+
185206
/**
186207
* Process the return comment of this function comment.
187208
*

tests/Drupal/good/good.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,3 +2003,20 @@ public function &get($instance_id) {
20032003
}
20042004

20052005
}
2006+
2007+
/**
2008+
* Test for @phpstan-ignore-next-line.
2009+
*
2010+
* Coder issue https://www.drupal.org/project/coder/issues/3516489
2011+
*/
2012+
// @phpstan-ignore-next-line missingType.return
2013+
public function ignore_phpstan_comment() {
2014+
}
2015+
2016+
/**
2017+
* Test for @phpstan-ignore with attribute before.
2018+
*/
2019+
#[ExampleAttribute('foo', 'bar')]
2020+
// @phpstan-ignore-next-line missingType.return
2021+
public function ignore_phpstan_comment_with_attribute_before() {
2022+
}

0 commit comments

Comments
 (0)