Skip to content

Commit f026751

Browse files
committed
SlevomatCodingStandard.ControlStructures.DisallowTrailingMultiLineTernaryOperator: Report really only trailing ternary operator
1 parent ed4a5ed commit f026751

File tree

4 files changed

+10
-55
lines changed

4 files changed

+10
-55
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/DisallowTrailingMultiLineTernaryOperatorSniff.php

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
use SlevomatCodingStandard\Helpers\IndentationHelper;
99
use SlevomatCodingStandard\Helpers\TernaryOperatorHelper;
1010
use SlevomatCodingStandard\Helpers\TokenHelper;
11-
use function array_merge;
12-
use function in_array;
1311
use function strlen;
1412
use function substr;
15-
use function trim;
1613
use const T_INLINE_ELSE;
1714
use const T_INLINE_THEN;
18-
use const T_OPEN_TAG;
19-
use const T_OPEN_TAG_WITH_ECHO;
2015
use const T_WHITESPACE;
2116

2217
class DisallowTrailingMultiLineTernaryOperatorSniff implements Sniff
@@ -47,18 +42,7 @@ public function process(File $phpcsFile, $inlineThenPointer): void
4742
return;
4843
}
4944

50-
$inlineElsePointer = TernaryOperatorHelper::getElsePointer($phpcsFile, $inlineThenPointer);
51-
52-
if ($tokens[$inlineThenPointer]['line'] === $tokens[$inlineElsePointer]['line']) {
53-
return;
54-
}
55-
56-
$endOfLineBeforeInlineThenPointer = $this->getEndOfLineBefore($phpcsFile, $inlineThenPointer);
57-
$endOfLineBeforeInlineElsePointer = $this->getEndOfLineBefore($phpcsFile, $inlineElsePointer);
58-
59-
$contentBeforeThen = TokenHelper::getContent($phpcsFile, $endOfLineBeforeInlineThenPointer + 1, $inlineThenPointer - 1);
60-
$contentBeforeElse = TokenHelper::getContent($phpcsFile, $endOfLineBeforeInlineElsePointer + 1, $inlineElsePointer - 1);
61-
if (trim($contentBeforeElse) === '' && trim($contentBeforeThen) === '') {
45+
if ($tokens[$inlineThenPointer]['line'] === $tokens[$nextPointer]['line']) {
6246
return;
6347
}
6448

@@ -72,6 +56,9 @@ public function process(File $phpcsFile, $inlineThenPointer): void
7256
return;
7357
}
7458

59+
$inlineElsePointer = TernaryOperatorHelper::getElsePointer($phpcsFile, $inlineThenPointer);
60+
$endOfLineBeforeInlineThenPointer = TokenHelper::findLastTokenOnPreviousLine($phpcsFile, $inlineThenPointer);
61+
7562
$indentation = $this->getIndentation($phpcsFile, $endOfLineBeforeInlineThenPointer);
7663
$pointerBeforeInlineThen = TokenHelper::findPreviousEffective($phpcsFile, $inlineThenPointer - 1);
7764
$pointerAfterInlineThen = TokenHelper::findNextExcluding($phpcsFile, [T_WHITESPACE], $inlineThenPointer + 1);
@@ -95,43 +82,6 @@ public function process(File $phpcsFile, $inlineThenPointer): void
9582
$phpcsFile->fixer->endChangeset();
9683
}
9784

98-
private function getEndOfLineBefore(File $phpcsFile, int $pointer): int
99-
{
100-
$tokens = $phpcsFile->getTokens();
101-
102-
$endOfLineBefore = null;
103-
104-
$startPointer = $pointer - 1;
105-
while (true) {
106-
$possibleEndOfLinePointer = TokenHelper::findPrevious(
107-
$phpcsFile,
108-
array_merge([T_WHITESPACE, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO], TokenHelper::$inlineCommentTokenCodes),
109-
$startPointer
110-
);
111-
if (
112-
$tokens[$possibleEndOfLinePointer]['code'] === T_WHITESPACE
113-
&& $tokens[$possibleEndOfLinePointer]['content'] === $phpcsFile->eolChar
114-
) {
115-
$endOfLineBefore = $possibleEndOfLinePointer;
116-
break;
117-
}
118-
119-
if (
120-
in_array($tokens[$possibleEndOfLinePointer]['code'], TokenHelper::$inlineCommentTokenCodes, true)
121-
&& substr($tokens[$possibleEndOfLinePointer]['content'], -1) === $phpcsFile->eolChar
122-
) {
123-
$endOfLineBefore = $possibleEndOfLinePointer;
124-
break;
125-
}
126-
127-
$startPointer = $possibleEndOfLinePointer - 1;
128-
}
129-
130-
/** @var int $endOfLineBefore */
131-
$endOfLineBefore = $endOfLineBefore;
132-
return $endOfLineBefore;
133-
}
134-
13585
private function getIndentation(File $phpcsFile, int $endOfLinePointer): string
13686
{
13787
$pointerAfterWhitespace = TokenHelper::findNextNonWhitespace($phpcsFile, $endOfLinePointer + 1);

tests/Sniffs/ControlStructures/DisallowTrailingMultiLineTernaryOperatorSniffTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function testErrors(): void
2626
self::assertSniffError($report, 21, DisallowTrailingMultiLineTernaryOperatorSniff::CODE_TRAILING_MULTI_LINE_TERNARY_OPERATOR_USED);
2727
self::assertSniffError($report, 25, DisallowTrailingMultiLineTernaryOperatorSniff::CODE_TRAILING_MULTI_LINE_TERNARY_OPERATOR_USED);
2828
self::assertSniffError($report, 29, DisallowTrailingMultiLineTernaryOperatorSniff::CODE_TRAILING_MULTI_LINE_TERNARY_OPERATOR_USED);
29+
self::assertSniffError($report, 33, DisallowTrailingMultiLineTernaryOperatorSniff::CODE_TRAILING_MULTI_LINE_TERNARY_OPERATOR_USED);
2930

3031
self::assertAllFixedInFile($report);
3132
}

tests/Sniffs/ControlStructures/data/disallowTrailingMultiLineTernaryOperatorErrors.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ function () { return; };
3030
'bbbb' :
3131
(false ? 1 : 2);
3232

33-
$array[$b === 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' ? 'bbbb'
33+
$array[$b === 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' ?
34+
'bbbb'
3435
: 'ccccccc'] = 'b';

tests/Sniffs/ControlStructures/data/disallowTrailingMultiLineTernaryOperatorNoErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@
5656

5757
$a = $b === 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' ? 'bbbb' :
5858
'ccccccccccccc';
59+
60+
$a = $b ? 'bbb'
61+
: 'ccc';

0 commit comments

Comments
 (0)