Skip to content

Commit

Permalink
Merge pull request #3908 from fredden/auto-fix/function-comment/spaci…
Browse files Browse the repository at this point in the history
…ng-after

Auto-fix PEAR.Commenting.FunctionComment.SpacingAfter
  • Loading branch information
jrfnl authored Nov 2, 2023
2 parents 321d252 + 9a5b392 commit 9187910
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,25 @@ public function process(File $phpcsFile, $stackPtr)
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line']
) {
$error = 'There must be no blank lines after the function comment';
$phpcsFile->addError($error, $commentEnd, 'SpacingAfter');
$fix = $phpcsFile->addFixableError($error, $commentEnd, 'SpacingAfter');

if ($fix === true) {
$phpcsFile->fixer->beginChangeset();

while ($i < $stackPtr
&& $tokens[$i]['code'] === T_WHITESPACE
&& $tokens[$i]['line'] !== $tokens[($i + 1)]['line']
) {
$phpcsFile->fixer->replaceToken($i++, '');
}

$phpcsFile->fixer->endChangeset();
}

break;
}
}
}
}//end for
}//end if

$commentStart = $tokens[$commentEnd]['comment_opener'];
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
Expand Down
34 changes: 34 additions & 0 deletions src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,37 @@ class Something implements JsonSerializable {

public function blankLineDetectionC() {}
}

class SpacingAfter {
/**
* There are multiple blank lines between this comment and the next function.
*
* @return void
*/








public function multipleBlankLines() {}

/**
* There are multiple blank lines, and some "empty" lines with only
* spaces/tabs between this comment and the next function.
*
* @return void
*/









public function multipleLinesSomeEmpty() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ class Something implements JsonSerializable {
*
* @return mixed
*/

#[ReturnTypeWillChange]
public function blankLineDetectionA() {}

Expand All @@ -463,16 +462,30 @@ class Something implements JsonSerializable {
* @return mixed
*/
#[ReturnTypeWillChange]

public function blankLineDetectionB() {}

/**
* Blank line between both docblock and attribute and attribute and function declaration.
*
* @return mixed
*/

#[ReturnTypeWillChange]

public function blankLineDetectionC() {}
}

class SpacingAfter {
/**
* There are multiple blank lines between this comment and the next function.
*
* @return void
*/
public function multipleBlankLines() {}

/**
* There are multiple blank lines, and some "empty" lines with only
* spaces/tabs between this comment and the next function.
*
* @return void
*/
public function multipleLinesSomeEmpty() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function getErrorList()
455 => 1,
464 => 1,
473 => 1,
485 => 1,
501 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 9187910

Please sign in to comment.