88use SlevomatCodingStandard \Helpers \IndentationHelper ;
99use SlevomatCodingStandard \Helpers \TernaryOperatorHelper ;
1010use SlevomatCodingStandard \Helpers \TokenHelper ;
11- use function array_merge ;
12- use function in_array ;
1311use function strlen ;
1412use function substr ;
15- use function trim ;
1613use const T_INLINE_ELSE ;
1714use const T_INLINE_THEN ;
18- use const T_OPEN_TAG ;
19- use const T_OPEN_TAG_WITH_ECHO ;
2015use const T_WHITESPACE ;
2116
2217class 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 );
0 commit comments