Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}
}
20 changes: 20 additions & 0 deletions src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down