Skip to content

Commit 48fb9b2

Browse files
schlndhkukulich
authored andcommitted
SlevomatCodingStandard.Commenting.DisallowCommentAfterCodeSniff: don't break multi-line string during auto-fix
1 parent ff1c4f0 commit 48fb9b2

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

SlevomatCodingStandard/Sniffs/Commenting/DisallowCommentAfterCodeSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use PHP_CodeSniffer\Sniffs\Sniff;
7+
use PHP_CodeSniffer\Util\Tokens;
78
use SlevomatCodingStandard\Helpers\CommentHelper;
89
use SlevomatCodingStandard\Helpers\IndentationHelper;
910
use SlevomatCodingStandard\Helpers\TokenHelper;
@@ -98,6 +99,17 @@ public function process(File $phpcsFile, $commentPointer): void
9899
);
99100
} elseif ($tokens[$firstNonWhitespacePointerOnLine]['code'] === T_CLOSE_CURLY_BRACKET) {
100101
$phpcsFile->fixer->addContent($firstNonWhiteSpacePointerBeforeComment, $phpcsFile->eolChar . $indentation . $commentContent);
102+
} elseif (isset(Tokens::$stringTokens[$tokens[$firstPointerOnLine]['code']])) {
103+
$prevNonStringToken = TokenHelper::findPreviousExcluding(
104+
$phpcsFile,
105+
[T_WHITESPACE] + Tokens::$stringTokens,
106+
$firstPointerOnLine - 1
107+
);
108+
$firstTokenOnNonStringTokenLine = TokenHelper::findFirstTokenOnLine($phpcsFile, $prevNonStringToken);
109+
$firstNonWhitespacePointerOnNonStringTokenLine = TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $prevNonStringToken);
110+
$prevLineIndentation = IndentationHelper::getIndentation($phpcsFile, $firstNonWhitespacePointerOnNonStringTokenLine);
111+
$phpcsFile->fixer->addContentBefore($firstTokenOnNonStringTokenLine, $prevLineIndentation . $commentContent);
112+
$phpcsFile->fixer->addNewline($firstNonWhiteSpacePointerBeforeComment);
101113
} else {
102114
$phpcsFile->fixer->addContentBefore($firstPointerOnLine, $indentation . $commentContent);
103115
$phpcsFile->fixer->addNewline($firstNonWhiteSpacePointerBeforeComment);

tests/Sniffs/Commenting/DisallowCommentAfterCodeSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/disallowCommentAfterCodeErrors.php');
1919

20-
self::assertSame(19, $report->getErrorCount());
20+
self::assertSame(23, $report->getErrorCount());
2121

22-
foreach ([7, 9, 11, 13, 15, 19, 25, 27, 30, 35, 36, 39, 40, 41, 42, 43, 44, 45, 48] as $line) {
22+
foreach ([7, 9, 11, 13, 15, 19, 25, 27, 30, 35, 36, 39, 40, 41, 42, 43, 44, 45, 50, 53, 58, 61, 63] as $line) {
2323
self::assertSniffError($report, $line, DisallowCommentAfterCodeSniff::CODE_DISALLOWED_COMMENT_AFTER_CODE);
2424
}
2525

tests/Sniffs/Commenting/data/disallowCommentAfterCodeErrors.fixed.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ public function method($items)
6363
return;
6464
}
6565

66+
// comment
67+
doSomething("
68+
aaa
69+
");
70+
// comment
71+
doSomething('
72+
aaa
73+
');
74+
$a = rand()
75+
? 5
76+
// comment
77+
: '
78+
aaa
79+
';
80+
// comment
81+
doSomething("
82+
{$a}
83+
");
84+
6685
// True
6786
return true;
6887
}

tests/Sniffs/Commenting/data/disallowCommentAfterCodeErrors.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ public function method($items)
4545
return; // Return
4646
}
4747

48+
doSomething("
49+
aaa
50+
"); // comment
51+
doSomething('
52+
aaa
53+
'); // comment
54+
$a = rand()
55+
? 5
56+
: '
57+
aaa
58+
'; // comment
59+
doSomething("
60+
{$a}
61+
"); // comment
62+
4863
return true; // True
4964
}
5065

0 commit comments

Comments
 (0)