Skip to content

Commit

Permalink
Squiz/BlockComment: don't autofix mid-line inline comments
Browse files Browse the repository at this point in the history
This was causing fixer conflicts with other sniffs dealing with code whitespace rules.
  • Loading branch information
jrfnl committed Feb 27, 2018
1 parent a15dd0a commit d735ce8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Standards/Squiz/Sniffs/Commenting/BlockCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,17 @@ public function process(File $phpcsFile, $stackPtr)

if (count($commentLines) === 1) {
$error = 'Single line block comment not allowed; use inline ("// text") comment instead';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SingleLine');
if ($fix === true) {
$comment = '// '.$commentText.$phpcsFile->eolChar;
$phpcsFile->fixer->replaceToken($stackPtr, $comment);

// Only fix comments when they are the last token on a line.
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($tokens[$stackPtr]['line'] !== $tokens[$nextNonEmpty]['line']) {
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SingleLine');
if ($fix === true) {
$comment = '// '.$commentText.$phpcsFile->eolChar;
$phpcsFile->fixer->replaceToken($stackPtr, $comment);
}
} else {
$phpcsFile->addError($error, $stackPtr, 'SingleLine');
}

return;
Expand Down
4 changes: 4 additions & 0 deletions src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,7 @@ class TabTest {
/*
*
*/

// Mid-line inline comment style should not be auto-fixed.
if (true || /* test */ -1 == $b) {}
$y = 10 + /* test */ -2;
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,7 @@ class TabTest {
* Test distinguishing between two distinct block comments which directly follow each other.
*/


// Mid-line inline comment style should not be auto-fixed.
if (true || /* test */ -1 == $b) {}
$y = 10 + /* test */ -2;
2 changes: 2 additions & 0 deletions src/Standards/Squiz/Tests/Commenting/BlockCommentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function getErrorList()
214 => 1,
226 => 1,
227 => 1,
232 => 1,
233 => 1,
];

return $errors;
Expand Down

0 comments on commit d735ce8

Please sign in to comment.