|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SlevomatCodingStandard\Sniffs\ControlStructures; |
| 4 | + |
| 5 | +use PHP_CodeSniffer\Files\File; |
| 6 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 7 | +use SlevomatCodingStandard\Helpers\FixerHelper; |
| 8 | +use SlevomatCodingStandard\Helpers\IndentationHelper; |
| 9 | +use SlevomatCodingStandard\Helpers\TernaryOperatorHelper; |
| 10 | +use SlevomatCodingStandard\Helpers\TokenHelper; |
| 11 | +use function array_merge; |
| 12 | +use function in_array; |
| 13 | +use function strlen; |
| 14 | +use function substr; |
| 15 | +use function trim; |
| 16 | +use const T_INLINE_ELSE; |
| 17 | +use const T_INLINE_THEN; |
| 18 | +use const T_OPEN_TAG; |
| 19 | +use const T_OPEN_TAG_WITH_ECHO; |
| 20 | +use const T_WHITESPACE; |
| 21 | + |
| 22 | +class DisallowTrailingMultiLineTernaryOperatorSniff implements Sniff |
| 23 | +{ |
| 24 | + |
| 25 | + public const CODE_TRAILING_MULTI_LINE_TERNARY_OPERATOR_USED = 'TrailingMultiLineTernaryOperatorUsed'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @return array<int, (int|string)> |
| 29 | + */ |
| 30 | + public function register(): array |
| 31 | + { |
| 32 | + return [ |
| 33 | + T_INLINE_THEN, |
| 34 | + ]; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
| 39 | + * @param int $inlineThenPointer |
| 40 | + */ |
| 41 | + public function process(File $phpcsFile, $inlineThenPointer): void |
| 42 | + { |
| 43 | + $tokens = $phpcsFile->getTokens(); |
| 44 | + |
| 45 | + $nextPointer = TokenHelper::findNextEffective($phpcsFile, $inlineThenPointer + 1); |
| 46 | + if ($tokens[$nextPointer]['code'] === T_INLINE_ELSE) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 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) === '') { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $fix = $phpcsFile->addFixableError( |
| 66 | + 'Ternary operator should be reformatted as leading the line.', |
| 67 | + $inlineThenPointer, |
| 68 | + self::CODE_TRAILING_MULTI_LINE_TERNARY_OPERATOR_USED |
| 69 | + ); |
| 70 | + |
| 71 | + if (!$fix) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $indentation = $this->getIndentation($phpcsFile, $endOfLineBeforeInlineThenPointer); |
| 76 | + $pointerBeforeInlineThen = TokenHelper::findPreviousEffective($phpcsFile, $inlineThenPointer - 1); |
| 77 | + $pointerAfterInlineThen = TokenHelper::findNextExcluding($phpcsFile, [T_WHITESPACE], $inlineThenPointer + 1); |
| 78 | + $pointerBeforeInlineElse = TokenHelper::findPreviousEffective($phpcsFile, $inlineElsePointer - 1); |
| 79 | + $pointerAfterInlineElse = TokenHelper::findNextExcluding($phpcsFile, [T_WHITESPACE], $inlineElsePointer + 1); |
| 80 | + |
| 81 | + $phpcsFile->fixer->beginChangeset(); |
| 82 | + |
| 83 | + FixerHelper::removeBetween($phpcsFile, $pointerBeforeInlineThen, $inlineThenPointer); |
| 84 | + FixerHelper::removeBetween($phpcsFile, $inlineThenPointer, $pointerAfterInlineThen); |
| 85 | + |
| 86 | + $phpcsFile->fixer->addContentBefore($inlineThenPointer, $phpcsFile->eolChar . $indentation); |
| 87 | + $phpcsFile->fixer->addContentBefore($pointerAfterInlineThen, ' '); |
| 88 | + |
| 89 | + FixerHelper::removeBetween($phpcsFile, $pointerBeforeInlineElse, $inlineElsePointer); |
| 90 | + FixerHelper::removeBetween($phpcsFile, $inlineElsePointer, $pointerAfterInlineElse); |
| 91 | + |
| 92 | + $phpcsFile->fixer->addContentBefore($inlineElsePointer, $phpcsFile->eolChar . $indentation); |
| 93 | + $phpcsFile->fixer->addContentBefore($pointerAfterInlineElse, ' '); |
| 94 | + |
| 95 | + $phpcsFile->fixer->endChangeset(); |
| 96 | + } |
| 97 | + |
| 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 | + |
| 135 | + private function getIndentation(File $phpcsFile, int $endOfLinePointer): string |
| 136 | + { |
| 137 | + $pointerAfterWhitespace = TokenHelper::findNextNonWhitespace($phpcsFile, $endOfLinePointer + 1); |
| 138 | + $actualIndentation = TokenHelper::getContent($phpcsFile, $endOfLinePointer + 1, $pointerAfterWhitespace - 1); |
| 139 | + |
| 140 | + if (strlen($actualIndentation) !== 0) { |
| 141 | + return $actualIndentation . ( |
| 142 | + substr($actualIndentation, -1) === IndentationHelper::TAB_INDENT |
| 143 | + ? IndentationHelper::TAB_INDENT |
| 144 | + : IndentationHelper::SPACES_INDENT |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + $tabPointer = TokenHelper::findPreviousContent($phpcsFile, T_WHITESPACE, IndentationHelper::TAB_INDENT, $endOfLinePointer - 1); |
| 149 | + return $tabPointer !== null ? IndentationHelper::TAB_INDENT : IndentationHelper::SPACES_INDENT; |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments