|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCSExtra, a collection of sniffs and standards for use with PHP_CodeSniffer. |
| 4 | + * |
| 5 | + * @package PHPCSExtra |
| 6 | + * @copyright 2023 PHPCSExtra Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCSStandards/PHPCSExtra |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHPCSExtra\Universal\Sniffs\Operators; |
| 12 | + |
| 13 | +use PHP_CodeSniffer\Files\File; |
| 14 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 15 | +use PHP_CodeSniffer\Util\Tokens; |
| 16 | + |
| 17 | +/** |
| 18 | + * Enforces that the concatenation operator in multi-line concatenations is in a preferred position, |
| 19 | + * either always at the start of the next line or always at the end of the previous line. |
| 20 | + * |
| 21 | + * Note: this sniff has no opinion on spacing before/after the concatenation operator. |
| 22 | + * It will normalize based on the "one space before/after" PSR-12 industry standard. |
| 23 | + * If different spacing is preferred, use the `Squiz.Strings.ConcatenationSpacing` to enforce/correct that. |
| 24 | + * |
| 25 | + * @since 1.2.0 |
| 26 | + */ |
| 27 | +final class ConcatPositionSniff implements Sniff |
| 28 | +{ |
| 29 | + |
| 30 | + /** |
| 31 | + * The phrase to use for the metric recorded by this sniff. |
| 32 | + * |
| 33 | + * @since 1.2.0 |
| 34 | + * |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + const METRIC_NAME = 'Multi-line concatenation operator position'; |
| 38 | + |
| 39 | + /** |
| 40 | + * Position indication: start of next line. |
| 41 | + * |
| 42 | + * @since 1.2.0 |
| 43 | + * |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + const POSITION_START = 'start'; |
| 47 | + |
| 48 | + /** |
| 49 | + * Position indication: end of previous line. |
| 50 | + * |
| 51 | + * @since 1.2.0 |
| 52 | + * |
| 53 | + * @var string |
| 54 | + */ |
| 55 | + const POSITION_END = 'end'; |
| 56 | + |
| 57 | + /** |
| 58 | + * Position indication: neither start of next line nor end of previous line. |
| 59 | + * |
| 60 | + * @since 1.2.0 |
| 61 | + * |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + const POSITION_STANDALONE = 'stand-alone'; |
| 65 | + |
| 66 | + /** |
| 67 | + * Preferred position for the concatenation operator. |
| 68 | + * |
| 69 | + * Valid values are: 'start' and 'end'. |
| 70 | + * Defaults to 'start'. |
| 71 | + * |
| 72 | + * @since 1.2.0 |
| 73 | + * |
| 74 | + * @var string |
| 75 | + */ |
| 76 | + public $allowOnly = self::POSITION_START; |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns an array of tokens this test wants to listen for. |
| 80 | + * |
| 81 | + * @since 1.2.0 |
| 82 | + * |
| 83 | + * @return array<int|string> |
| 84 | + */ |
| 85 | + public function register() |
| 86 | + { |
| 87 | + return [\T_STRING_CONCAT]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Processes this test, when one of its tokens is encountered. |
| 92 | + * |
| 93 | + * @since 1.2.0 |
| 94 | + * |
| 95 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 96 | + * @param int $stackPtr The position of the current token |
| 97 | + * in the stack passed in $tokens. |
| 98 | + * |
| 99 | + * @return int|void Integer stack pointer to skip forward or void to continue |
| 100 | + * normal file processing. |
| 101 | + */ |
| 102 | + public function process(File $phpcsFile, $stackPtr) |
| 103 | + { |
| 104 | + /* |
| 105 | + * Validate the setting. |
| 106 | + */ |
| 107 | + if ($this->allowOnly !== self::POSITION_END) { |
| 108 | + // Use the default. |
| 109 | + $this->allowOnly = self::POSITION_START; |
| 110 | + } |
| 111 | + |
| 112 | + $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
| 113 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
| 114 | + |
| 115 | + if ($nextNonEmpty === false) { |
| 116 | + // Parse error/live coding. |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + $tokens = $phpcsFile->getTokens(); |
| 121 | + if ($tokens[$prevNonEmpty]['line'] === $tokens[$nextNonEmpty]['line']) { |
| 122 | + // Not multi-line concatenation. Not our target. |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + $position = self::POSITION_STANDALONE; |
| 127 | + if ($tokens[$prevNonEmpty]['line'] === $tokens[$stackPtr]['line']) { |
| 128 | + $position = self::POSITION_END; |
| 129 | + } elseif ($tokens[$nextNonEmpty]['line'] === $tokens[$stackPtr]['line']) { |
| 130 | + $position = self::POSITION_START; |
| 131 | + } |
| 132 | + |
| 133 | + // Record metric. |
| 134 | + $phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, $position); |
| 135 | + |
| 136 | + if ($this->allowOnly === $position) { |
| 137 | + // All okay. |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + $fix = $phpcsFile->addFixableError( |
| 142 | + 'The concatenation operator for multi-line concatenations should always be at the %s of a line.', |
| 143 | + $stackPtr, |
| 144 | + 'Incorrect', |
| 145 | + [$this->allowOnly] |
| 146 | + ); |
| 147 | + |
| 148 | + if ($fix === true) { |
| 149 | + if ($this->allowOnly === self::POSITION_END) { |
| 150 | + $phpcsFile->fixer->beginChangeset(); |
| 151 | + |
| 152 | + // Move the concat operator. |
| 153 | + $phpcsFile->fixer->replaceToken($stackPtr, ''); |
| 154 | + $phpcsFile->fixer->addContent($prevNonEmpty, ' .'); |
| 155 | + |
| 156 | + if ($position === self::POSITION_START |
| 157 | + && $tokens[($stackPtr + 1)]['code'] === \T_WHITESPACE |
| 158 | + ) { |
| 159 | + // Remove trailing space. |
| 160 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
| 161 | + } elseif ($position === self::POSITION_STANDALONE) { |
| 162 | + // Remove potential indentation space. |
| 163 | + if ($tokens[($stackPtr - 1)]['code'] === \T_WHITESPACE) { |
| 164 | + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
| 165 | + } |
| 166 | + |
| 167 | + // Remove new line. |
| 168 | + if ($tokens[($stackPtr + 1)]['code'] === \T_WHITESPACE) { |
| 169 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + $phpcsFile->fixer->endChangeset(); |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + // Fixer for allowOnly === self::POSITION_START. |
| 178 | + $phpcsFile->fixer->beginChangeset(); |
| 179 | + |
| 180 | + // Move the concat operator. |
| 181 | + $phpcsFile->fixer->replaceToken($stackPtr, ''); |
| 182 | + $phpcsFile->fixer->addContentBefore($nextNonEmpty, '. '); |
| 183 | + |
| 184 | + if ($position === self::POSITION_END |
| 185 | + && $tokens[($stackPtr - 1)]['code'] === \T_WHITESPACE |
| 186 | + ) { |
| 187 | + // Remove trailing space. |
| 188 | + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
| 189 | + } elseif ($position === self::POSITION_STANDALONE) { |
| 190 | + // Remove potential indentation space. |
| 191 | + if ($tokens[($stackPtr - 1)]['code'] === \T_WHITESPACE) { |
| 192 | + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); |
| 193 | + } |
| 194 | + |
| 195 | + // Remove new line. |
| 196 | + if ($tokens[($stackPtr + 1)]['code'] === \T_WHITESPACE) { |
| 197 | + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + $phpcsFile->fixer->endChangeset(); |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments