Skip to content

Commit

Permalink
fix(FunctionComment): Allow PHPCS ignore directives before functions
Browse files Browse the repository at this point in the history
  • Loading branch information
klausi committed Jan 8, 2025
1 parent 04a4563 commit 4f76f04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 0 additions & 5 deletions coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public function process(File $phpcsFile, $stackPtr)
$name = $tokens[$stackPtr]['content'];
$classCodeStart = $stackPtr;

$previousContent = null;
for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
if (isset($find[$tokens[$commentEnd]['code']]) === true) {
if (isset(Tokens::$phpcsCommentTokens[$tokens[$commentEnd]['code']]) === true) {
Expand All @@ -73,10 +72,6 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}

if ($previousContent === null) {
$previousContent = $commentEnd;
}

if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
&& isset($tokens[$commentEnd]['attribute_opener']) === true
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$ignore = Tokens::$methodPrefixes;
$ignore = (Tokens::$methodPrefixes + Tokens::$phpcsCommentTokens);
$ignore[T_WHITESPACE] = T_WHITESPACE;
$functionCodeStart = $stackPtr;

for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
if (isset($ignore[$tokens[$commentEnd]['code']]) === true) {
if (isset(Tokens::$phpcsCommentTokens[$tokens[$commentEnd]['code']]) === true) {
$functionCodeStart = $commentEnd;
}

continue;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Drupal/good/good.php
Original file line number Diff line number Diff line change
Expand Up @@ -1914,3 +1914,11 @@ enum PUROSELY_WRONG_BUT_OK: int {
case One = 1;
case Two = 2;
}

/**
* Doc block is here and an ignore directive is ok.
*/
// phpcs:ignore Drupal.NamingConventions.ValidClassName
function phpcs_ignore_comment() {

}

0 comments on commit 4f76f04

Please sign in to comment.