Skip to content

Commit 2343186

Browse files
committed
Fix ternary check.
1 parent 8a751c2 commit 2343186

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

PSR2R/Sniffs/ControlStructures/TernarySpacingSniff.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* Asserts single space between ternary operator parts (? and :) and surroundings.
1111
* Also asserts no whitespace between short ternary operator (?:), which was introduced in PHP 5.3.
1212
*
13-
* @see https://github.com/dereuromark/codesniffer-standards/blob/master/MyCakePHP/Sniffs/WhiteSpace/TernarySpacingSniff.php
1413
* @author Mark Scherer
1514
* @license MIT
1615
*/
@@ -39,13 +38,21 @@ public function process(File $phpcsFile, $stackPtr) {
3938
*/
4039
protected function assertSpaceBefore(File $phpcsFile, int $stackPtr): void {
4140
$previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
41+
if (!$previous) {
42+
return;
43+
}
44+
4245
if ($stackPtr - $previous > 1) {
4346
$this->assertSingleSpaceBeforeIfNotMultiline($phpcsFile, $stackPtr, $previous);
4447

4548
return;
4649
}
4750

4851
$tokens = $phpcsFile->getTokens();
52+
if ($tokens[$previous]['code'] === 'PHPCS_T_INLINE_THEN') {
53+
return;
54+
}
55+
4956
$content = $tokens[$stackPtr]['content'];
5057
$error = 'There must be a single space before ternary operator part `' . $content . '`';
5158
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeInlineThen');
@@ -133,6 +140,10 @@ protected function assertNoSpaceBetween(File $phpcsFile, int $thenIndex, int $el
133140
*/
134141
protected function assertSpaceAfter(File $phpcsFile, int $stackPtr): void {
135142
$nextIndex = $phpcsFile->findNext(T_WHITESPACE, $stackPtr + 1, null, true);
143+
if (!$nextIndex) {
144+
return;
145+
}
146+
136147
if ($nextIndex - $stackPtr > 1) {
137148
$this->assertSingleSpaceAfterIfNotMultiline($phpcsFile, $stackPtr, $nextIndex);
138149

0 commit comments

Comments
 (0)