From 4f76f041548b7bbe79e16e16fd423b8d1de47ba1 Mon Sep 17 00:00:00 2001 From: Klaus Purer Date: Wed, 8 Jan 2025 13:54:37 +0100 Subject: [PATCH] fix(FunctionComment): Allow PHPCS ignore directives before functions --- .../Drupal/Sniffs/Commenting/ClassCommentSniff.php | 5 ----- .../Drupal/Sniffs/Commenting/FunctionCommentSniff.php | 6 +++++- tests/Drupal/good/good.php | 8 ++++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php index 75de9c9b..f4cda761 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/ClassCommentSniff.php @@ -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) { @@ -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 ) { diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php index cc55d038..7bd1f178 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php @@ -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; } diff --git a/tests/Drupal/good/good.php b/tests/Drupal/good/good.php index bd8d911b..d1d24b1d 100644 --- a/tests/Drupal/good/good.php +++ b/tests/Drupal/good/good.php @@ -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() { + +}