Skip to content

Commit

Permalink
Merge pull request #132 from michalbundyra/bugfix/131
Browse files Browse the repository at this point in the history
Fixes comment placement at the end of the line
  • Loading branch information
michalbundyra committed Nov 27, 2020
2 parents 3199cc5 + b1c0cbc commit 2df266a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
use PHP_CodeSniffer\Util\Tokens;

use function implode;
use function substr;

use const T_CLOSE_TAG;
use const T_COMMENT;
use const T_OPEN_TAG;
use const T_WHITESPACE;

class PlacementSniff implements Sniff
Expand All @@ -34,6 +37,8 @@ public function process(File $phpcsFile, $stackPtr)
&& $tokens[$stackPtr - 1]['code'] !== T_WHITESPACE
&& ($tokens[$stackPtr - 1]['code'] !== T_COMMENT
|| $tokens[$stackPtr - 1]['line'] === $tokens[$stackPtr]['line'])
&& ($tokens[$stackPtr - 1]['code'] !== T_OPEN_TAG
|| substr($tokens[$stackPtr - 1]['content'], -1) !== ' ')
) {
$error = 'Expected at least one space before comment';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingSpaceBefore');
Expand All @@ -44,7 +49,7 @@ public function process(File $phpcsFile, $stackPtr)
}

$lastInLine = $stackPtr;
while ($next = $phpcsFile->findNext(Tokens::$emptyTokens, $lastInLine + 1, null, true)) {
while ($next = $phpcsFile->findNext(Tokens::$emptyTokens + [T_CLOSE_TAG], $lastInLine + 1, null, true)) {
if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
$lastInLine = $next;
continue;
Expand Down
5 changes: 5 additions & 0 deletions test/Sniffs/Commenting/PlacementUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ $foo = 'bar';

// comment1
// comment2

?>

<?php // single line ?>
<?php /* comment */ ?>
5 changes: 5 additions & 0 deletions test/Sniffs/Commenting/PlacementUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ $foo = 'bar';

// comment1
// comment2

?>

<?php // single line ?>
<?php /* comment */ ?>

0 comments on commit 2df266a

Please sign in to comment.