Skip to content

Commit d987eac

Browse files
committed
RequireTernaryOperatorSniff: Fixer should not remove comments
1 parent 51d4580 commit d987eac

File tree

4 files changed

+114
-6
lines changed

4 files changed

+114
-6
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/RequireTernaryOperatorSniff.php

Lines changed: 42 additions & 2 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\IdentificatorHelper;
89
use SlevomatCodingStandard\Helpers\TokenHelper;
910
use function array_key_exists;
@@ -81,7 +82,21 @@ public function process(File $phpcsFile, $ifPointer): void
8182

8283
private function checkIfWithReturns(File $phpcsFile, int $ifPointer, int $elsePointer, int $returnInIf, int $returnInElse): void
8384
{
84-
$fix = $phpcsFile->addFixableError('Use ternary operator.', $ifPointer, self::CODE_TERNARY_OPERATOR_NOT_USED);
85+
$ifContainsComment = $this->containsComment($phpcsFile, $ifPointer);
86+
$elseContainsComment = $this->containsComment($phpcsFile, $elsePointer);
87+
88+
$errorParameters = [
89+
'Use ternary operator.',
90+
$ifPointer,
91+
self::CODE_TERNARY_OPERATOR_NOT_USED,
92+
];
93+
94+
if ($ifContainsComment || $elseContainsComment) {
95+
$phpcsFile->addError(...$errorParameters);
96+
return;
97+
}
98+
99+
$fix = $phpcsFile->addFixableError(...$errorParameters);
85100

86101
if (!$fix) {
87102
return;
@@ -157,7 +172,21 @@ private function checkIfWithAssignments(
157172
return;
158173
}
159174

160-
$fix = $phpcsFile->addFixableError('Use ternary operator.', $ifPointer, self::CODE_TERNARY_OPERATOR_NOT_USED);
175+
$ifContainsComment = $this->containsComment($phpcsFile, $ifPointer);
176+
$elseContainsComment = $this->containsComment($phpcsFile, $elsePointer);
177+
178+
$errorParameters = [
179+
'Use ternary operator.',
180+
$ifPointer,
181+
self::CODE_TERNARY_OPERATOR_NOT_USED,
182+
];
183+
184+
if ($ifContainsComment || $elseContainsComment) {
185+
$phpcsFile->addError(...$errorParameters);
186+
return;
187+
}
188+
189+
$fix = $phpcsFile->addFixableError(...$errorParameters);
161190

162191
if (!$fix) {
163192
return;
@@ -213,4 +242,15 @@ private function isCompatibleScope(File $phpcsFile, int $scopeOpenerPointer, int
213242
return $pointerAfterSemicolon === $scopeCloserPointer;
214243
}
215244

245+
private function containsComment(File $phpcsFile, int $scopeOwnerPointer): bool
246+
{
247+
$tokens = $phpcsFile->getTokens();
248+
return TokenHelper::findNext(
249+
$phpcsFile,
250+
Tokens::$commentTokens,
251+
$tokens[$scopeOwnerPointer]['scope_opener'] + 1,
252+
$tokens[$scopeOwnerPointer]['scope_closer']
253+
) !== null;
254+
}
255+
216256
}

tests/Sniffs/ControlStructures/RequireTernaryOperatorSniffTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ public function testErrors(): void
1616
{
1717
$report = self::checkFile(__DIR__ . '/data/requireTernaryOperatorErrors.php');
1818

19-
self::assertSame(4, $report->getErrorCount());
19+
self::assertSame(8, $report->getErrorCount());
2020

2121
self::assertSniffError($report, 4, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
22-
self::assertSniffError($report, 11, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
23-
self::assertSniffError($report, 22, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
24-
self::assertSniffError($report, 31, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
22+
self::assertSniffError($report, 12, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
23+
self::assertSniffError($report, 21, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
24+
self::assertSniffError($report, 29, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
25+
self::assertSniffError($report, 35, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
26+
self::assertSniffError($report, 42, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
27+
self::assertSniffError($report, 54, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
28+
self::assertSniffError($report, 63, RequireTernaryOperatorSniff::CODE_TERNARY_OPERATOR_NOT_USED);
2529

2630
self::assertAllFixedInFile($report);
2731
}

tests/Sniffs/ControlStructures/data/requireTernaryOperatorErrors.fixed.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,40 @@ function () {
44
return true ? true : false;
55
};
66

7+
function () {
8+
if (true) {
9+
// Comment
10+
return true;
11+
} else {
12+
return false;
13+
}
14+
};
15+
16+
function () {
17+
if (true) {
18+
return true;
19+
} else {
20+
// Comment
21+
return false;
22+
}
23+
};
24+
725
$a = doSomething() ? 'a' : 'aa';
826

27+
if (doAnything()) {
28+
// Comment
29+
$a = 'a';
30+
} else {
31+
$a = 'aa';
32+
}
33+
34+
if (doNothing()) {
35+
$a = 'a';
36+
} else {
37+
// Comment
38+
$a = 'aa';
39+
}
40+
941
class Whatever
1042
{
1143

tests/Sniffs/ControlStructures/data/requireTernaryOperatorErrors.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,44 @@ function () {
88
}
99
};
1010

11+
function () {
12+
if (true) {
13+
// Comment
14+
return true;
15+
} else {
16+
return false;
17+
}
18+
};
19+
20+
function () {
21+
if (true) {
22+
return true;
23+
} else {
24+
// Comment
25+
return false;
26+
}
27+
};
28+
1129
if (doSomething()) {
1230
$a = 'a';
1331
} else {
1432
$a = 'aa';
1533
}
1634

35+
if (doAnything()) {
36+
// Comment
37+
$a = 'a';
38+
} else {
39+
$a = 'aa';
40+
}
41+
42+
if (doNothing()) {
43+
$a = 'a';
44+
} else {
45+
// Comment
46+
$a = 'aa';
47+
}
48+
1749
class Whatever
1850
{
1951

0 commit comments

Comments
 (0)