|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace InfinityloopCodingStandard\Sniffs\ControlStructures; |
| 4 | + |
| 5 | +use PHP_CodeSniffer\Files\File; |
| 6 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 7 | +use SlevomatCodingStandard\Helpers\TokenHelper; |
| 8 | +use function array_merge; |
| 9 | +use function in_array; |
| 10 | +use function strlen; |
| 11 | +use function substr; |
| 12 | +use const T_COALESCE; |
| 13 | +use const T_OPEN_TAG; |
| 14 | +use const T_OPEN_TAG_WITH_ECHO; |
| 15 | +use const T_WHITESPACE; |
| 16 | + |
| 17 | +class RequireMultiLineNullCoalesceSniff implements Sniff |
| 18 | +{ |
| 19 | + |
| 20 | + public const CODE_MULTI_LINE_NULL_COALESCE_OPERATOR_NOT_USED = 'MultiLineNullCoalesceOperatorNotUsed'; |
| 21 | + |
| 22 | + private const TAB_INDENT = "\t"; |
| 23 | + private const SPACES_INDENT = ' '; |
| 24 | + |
| 25 | + /** |
| 26 | + * @return array<int, (int|string)> |
| 27 | + */ |
| 28 | + public function register(): array |
| 29 | + { |
| 30 | + return [ |
| 31 | + T_COALESCE, |
| 32 | + ]; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
| 37 | + * @param File $phpcsFile |
| 38 | + * @param int $coalescePointer |
| 39 | + */ |
| 40 | + public function process(File $phpcsFile, $coalescePointer): void |
| 41 | + { |
| 42 | + $tokens = $phpcsFile->getTokens(); |
| 43 | + |
| 44 | + /** @var int $variablePointer */ |
| 45 | + $variablePointer = TokenHelper::findPrevious($phpcsFile, T_VARIABLE, $coalescePointer + 1); |
| 46 | + |
| 47 | + if ($tokens[$coalescePointer]['line'] !== $tokens[$variablePointer]['line']) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $fix = $phpcsFile->addFixableError('Null coalesce operator should be reformatted to next line.', $coalescePointer, self::CODE_MULTI_LINE_NULL_COALESCE_OPERATOR_NOT_USED); |
| 52 | + |
| 53 | + if (!$fix) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + $endOfLineBeforeCoalescePointer = $this->getEndOfLineBefore($phpcsFile, $coalescePointer); |
| 58 | + |
| 59 | + $indentation = $this->getIndentation($phpcsFile, $endOfLineBeforeCoalescePointer); |
| 60 | + |
| 61 | + $phpcsFile->fixer->beginChangeset(); |
| 62 | + $phpcsFile->fixer->addContentBefore($coalescePointer, $phpcsFile->eolChar . $indentation); |
| 63 | + $phpcsFile->fixer->endChangeset(); |
| 64 | + } |
| 65 | + |
| 66 | + private function getEndOfLineBefore(File $phpcsFile, int $pointer): int |
| 67 | + { |
| 68 | + $tokens = $phpcsFile->getTokens(); |
| 69 | + |
| 70 | + $endOfLineBefore = null; |
| 71 | + |
| 72 | + $startPointer = $pointer - 1; |
| 73 | + while (true) { |
| 74 | + $possibleEndOfLinePointer = TokenHelper::findPrevious( |
| 75 | + $phpcsFile, |
| 76 | + array_merge([T_WHITESPACE, T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO], TokenHelper::$inlineCommentTokenCodes), |
| 77 | + $startPointer |
| 78 | + ); |
| 79 | + if ($tokens[$possibleEndOfLinePointer]['code'] === T_WHITESPACE && $tokens[$possibleEndOfLinePointer]['content'] === $phpcsFile->eolChar) { |
| 80 | + $endOfLineBefore = $possibleEndOfLinePointer; |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + if ($tokens[$possibleEndOfLinePointer]['code'] === T_OPEN_TAG || $tokens[$possibleEndOfLinePointer]['code'] === T_OPEN_TAG_WITH_ECHO) { |
| 85 | + $endOfLineBefore = $possibleEndOfLinePointer; |
| 86 | + break; |
| 87 | + } |
| 88 | + |
| 89 | + if ( |
| 90 | + in_array($tokens[$possibleEndOfLinePointer]['code'], TokenHelper::$inlineCommentTokenCodes, true) |
| 91 | + && substr($tokens[$possibleEndOfLinePointer]['content'], -1) === $phpcsFile->eolChar |
| 92 | + ) { |
| 93 | + $endOfLineBefore = $possibleEndOfLinePointer; |
| 94 | + break; |
| 95 | + } |
| 96 | + |
| 97 | + $startPointer = $possibleEndOfLinePointer - 1; |
| 98 | + } |
| 99 | + |
| 100 | + /** @var int $endOfLineBefore */ |
| 101 | + $endOfLineBefore = $endOfLineBefore; |
| 102 | + return $endOfLineBefore; |
| 103 | + } |
| 104 | + |
| 105 | + private function getIndentation(File $phpcsFile, int $endOfLinePointer): string |
| 106 | + { |
| 107 | + $pointerAfterWhitespace = TokenHelper::findNextExcluding($phpcsFile, T_WHITESPACE, $endOfLinePointer + 1); |
| 108 | + $actualIndentation = TokenHelper::getContent($phpcsFile, $endOfLinePointer + 1, $pointerAfterWhitespace - 1); |
| 109 | + |
| 110 | + if (strlen($actualIndentation) !== 0) { |
| 111 | + return $actualIndentation . (substr($actualIndentation, -1) === self::TAB_INDENT ? self::TAB_INDENT : self::SPACES_INDENT); |
| 112 | + } |
| 113 | + |
| 114 | + $tabPointer = TokenHelper::findPreviousContent($phpcsFile, T_WHITESPACE, self::TAB_INDENT, $endOfLinePointer - 1); |
| 115 | + return $tabPointer !== null ? self::TAB_INDENT : self::SPACES_INDENT; |
| 116 | + } |
| 117 | +} |
0 commit comments