diff --git a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php index 3b86b52a30..f19967b715 100644 --- a/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php +++ b/src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php @@ -468,6 +468,30 @@ public function processArgumentList(File $phpcsFile, int $stackPtr, int $indent, } elseif ($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE) { $foundIndent = $tokens[$i]['length']; ++$expectedIndent; + } elseif (($tokens[$i]['code'] === T_COMMENT + || isset(Tokens::PHPCS_ANNOTATION_TOKENS[$tokens[$i]['code']]) === true) + && (($tokens[($i - 1)]['code'] === T_COMMENT + || isset(Tokens::PHPCS_ANNOTATION_TOKENS[$tokens[($i - 1)]['code']]) === true) + && $tokens[($i - 1)]['line'] === $lastLine) + ) { + // This is the second line of a multi-line comment. + // This token _may_ include indentation whitespace if this is part of a block comment. + $trimmedContent = ltrim($tokens[$i]['content']); + $trimmedLength = strlen($trimmedContent); + if ($trimmedLength === 0) { + // This is a blank comment line, so indenting it is pointless. + $lastLine = $tokens[$i]['line']; + continue; + } + + $foundIndent = (strlen($tokens[$i]['content']) - $trimmedLength); + if ($trimmedContent[0] === '*') { + ++$expectedIndent; + } elseif ($foundIndent >= $expectedIndent) { + // Multi-line block comment, not star-aligned. These may contain extra indent. + $lastLine = $tokens[$i]['line']; + continue; + } } if ($expectedIndent !== $foundIndent) { @@ -480,8 +504,13 @@ public function processArgumentList(File $phpcsFile, int $stackPtr, int $indent, $fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data); if ($fix === true) { $spaces = str_repeat(' ', $expectedIndent); + if ($foundIndent === 0) { $phpcsFile->fixer->addContentBefore($i, $spaces); + } elseif ($tokens[$i]['code'] === T_COMMENT + || isset(Tokens::PHPCS_ANNOTATION_TOKENS[$tokens[$i]['code']]) === true + ) { + $phpcsFile->fixer->replaceToken($i, $spaces . ltrim($tokens[$i]['content'])); } else { $phpcsFile->fixer->replaceToken($i, $spaces); } diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc index 6ba3bd9f6a..794afa675e 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc @@ -488,3 +488,55 @@ function foo( $param2 ) : // comment. \Package\Sub\SomeClass {} + +class MultiLineCommentsBetweenParamsIndentOK +{ + public function something( + /* + * Some comment for whatever reason + * phpcs:disable Stnd.Cat -- for reasons + */ + string $paramA, + // phpcs:enable Stnd.Cat + // Comment line 2 + // Comment line 3 + #[NotBlank] + int $paramB, + ) { + } +} + +class MultiLineCommentsBetweenParamsIncorrectIndent +{ + public function something( + /* + * Some comment for whatever reason + * phpcs:disable Stnd.CatA -- this line will only be flagged & fixed when `--ignore-annotations` is used. + */ + string $paramA, +/* + * Some comment for whatever reason + * phpcs:disable Stnd.CatB -- this line will only be flagged & fixed when `--ignore-annotations` is used. + */ +string $paramB, + + // phpcs:enable Stnd.CatA,Stnd.CatB -- this line will only be flagged & fixed when `--ignore-annotations` is used. + // Comment line 2 +// Comment line 3 +#[NotBlank] +int $paramC, + /* + + Some comment surrounded by blank lines (deliberately!) + + */ + ?string $paramD, + /* + + Some comment surrounded by blank lines (deliberately!) + + */ + ?string $paramE, + ) { + } +} diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc.fixed b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc.fixed index 8248ee1dc8..f655baab15 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc.fixed +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.1.inc.fixed @@ -485,3 +485,54 @@ function foo( $param2 ) : // comment. \Package\Sub\SomeClass {} + +class MultiLineCommentsBetweenParamsIndentOK +{ + public function something( + /* + * Some comment for whatever reason + * phpcs:disable Stnd.Cat -- for reasons + */ + string $paramA, + // phpcs:enable Stnd.Cat + // Comment line 2 + // Comment line 3 + #[NotBlank] + int $paramB, + ) { + } +} + +class MultiLineCommentsBetweenParamsIncorrectIndent +{ + public function something( + /* + * Some comment for whatever reason + * phpcs:disable Stnd.CatA -- this line will only be flagged & fixed when `--ignore-annotations` is used. + */ + string $paramA, + /* + * Some comment for whatever reason + * phpcs:disable Stnd.CatB -- this line will only be flagged & fixed when `--ignore-annotations` is used. + */ + string $paramB, + // phpcs:enable Stnd.CatA,Stnd.CatB -- this line will only be flagged & fixed when `--ignore-annotations` is used. + // Comment line 2 + // Comment line 3 + #[NotBlank] + int $paramC, + /* + + Some comment surrounded by blank lines (deliberately!) + + */ + ?string $paramD, + /* + + Some comment surrounded by blank lines (deliberately!) + + */ + ?string $paramE, + ) { + } +} diff --git a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php index 0e0a1ea4be..d4af2c9242 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php +++ b/src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php @@ -109,6 +109,26 @@ public function getErrorList($testFile = '') 475 => 1, 483 => 1, 490 => 2, + 512 => 1, + 513 => 1, + 515 => 1, + 516 => 1, + 517 => 1, + 518 => 1, + 520 => 1, + 521 => 1, + 522 => 1, + 524 => 1, + 525 => 1, + 526 => 1, + 527 => 1, + 528 => 1, + 532 => 1, + 533 => 1, + 534 => 1, + 536 => 1, + 538 => 1, + 539 => 1, ]; case 'FunctionDeclarationUnitTest.4.inc':