|
10 | 10 | * Asserts single space between ternary operator parts (? and :) and surroundings.
|
11 | 11 | * Also asserts no whitespace between short ternary operator (?:), which was introduced in PHP 5.3.
|
12 | 12 | *
|
13 |
| - * @see https://github.com/dereuromark/codesniffer-standards/blob/master/MyCakePHP/Sniffs/WhiteSpace/TernarySpacingSniff.php |
14 | 13 | * @author Mark Scherer
|
15 | 14 | * @license MIT
|
16 | 15 | */
|
@@ -39,13 +38,21 @@ public function process(File $phpcsFile, $stackPtr) {
|
39 | 38 | */
|
40 | 39 | protected function assertSpaceBefore(File $phpcsFile, int $stackPtr): void {
|
41 | 40 | $previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
|
| 41 | + if (!$previous) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
42 | 45 | if ($stackPtr - $previous > 1) {
|
43 | 46 | $this->assertSingleSpaceBeforeIfNotMultiline($phpcsFile, $stackPtr, $previous);
|
44 | 47 |
|
45 | 48 | return;
|
46 | 49 | }
|
47 | 50 |
|
48 | 51 | $tokens = $phpcsFile->getTokens();
|
| 52 | + if ($tokens[$previous]['code'] === 'PHPCS_T_INLINE_THEN') { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
49 | 56 | $content = $tokens[$stackPtr]['content'];
|
50 | 57 | $error = 'There must be a single space before ternary operator part `' . $content . '`';
|
51 | 58 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeInlineThen');
|
@@ -133,6 +140,10 @@ protected function assertNoSpaceBetween(File $phpcsFile, int $thenIndex, int $el
|
133 | 140 | */
|
134 | 141 | protected function assertSpaceAfter(File $phpcsFile, int $stackPtr): void {
|
135 | 142 | $nextIndex = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
|
| 143 | + if (!$nextIndex) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
136 | 147 | if ($nextIndex - $stackPtr > 1) {
|
137 | 148 | $this->assertSingleSpaceAfterIfNotMultiline($phpcsFile, $stackPtr, $nextIndex);
|
138 | 149 |
|
|
0 commit comments