@@ -86,7 +86,6 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
86
86
if ($ tokens [$ contentBefore ]['code ' ] !== T_CLOSE_PARENTHESIS ) {
87
87
$ error = 'Inline shorthand IF statement requires brackets around comparison ' ;
88
88
$ phpcsFile ->addError ($ error , $ stackPtr , 'NoBrackets ' );
89
- return ;
90
89
}
91
90
92
91
$ spaceBefore = ($ tokens [$ stackPtr ]['column ' ] - ($ tokens [$ contentBefore ]['column ' ] + $ tokens [$ contentBefore ]['length ' ]));
@@ -96,26 +95,38 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
96
95
$ phpcsFile ->addError ($ error , $ stackPtr , 'SpacingBeforeThen ' , $ data );
97
96
}
98
97
99
- $ spaceAfter = (($ tokens [$ contentAfter ]['column ' ]) - ($ tokens [$ stackPtr ]['column ' ] + 1 ));
100
- if ($ spaceAfter !== 1 ) {
101
- $ error = 'Inline shorthand IF statement requires 1 space after THEN; %s found ' ;
102
- $ data = array ($ spaceAfter );
103
- $ phpcsFile ->addError ($ error , $ stackPtr , 'SpacingAfterThen ' , $ data );
104
- }
105
-
106
- // Make sure the ELSE has the correct spacing.
107
- $ inlineElse = $ phpcsFile ->findNext (T_INLINE_ELSE , ($ stackPtr + 1 ), $ statementEnd , false );
108
- $ contentBefore = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ inlineElse - 1 ), null , true );
109
- $ contentAfter = $ phpcsFile ->findNext (T_WHITESPACE , ($ inlineElse + 1 ), null , true );
110
-
111
- $ spaceBefore = ($ tokens [$ inlineElse ]['column ' ] - ($ tokens [$ contentBefore ]['column ' ] + $ tokens [$ contentBefore ]['length ' ]));
112
- if ($ spaceBefore !== 1 ) {
113
- $ error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found ' ;
114
- $ data = array ($ spaceBefore );
115
- $ phpcsFile ->addError ($ error , $ inlineElse , 'SpacingBeforeElse ' , $ data );
116
- }
117
-
118
- $ spaceAfter = (($ tokens [$ contentAfter ]['column ' ]) - ($ tokens [$ inlineElse ]['column ' ] + 1 ));
98
+ // If there is no content between the ? and the : operators, then they are
99
+ // trying to replicate an elvis operator, even though PHP doesn't have one.
100
+ // In this case, we want no spaces between the two operators so ?: looks like
101
+ // an operator itself.
102
+ $ next = $ phpcsFile ->findNext (T_WHITESPACE , ($ stackPtr + 1 ), null , true );
103
+ if ($ tokens [$ next ]['code ' ] === T_INLINE_ELSE ) {
104
+ $ inlineElse = $ next ;
105
+ if ($ inlineElse !== ($ stackPtr + 1 )) {
106
+ $ error = 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE ' ;
107
+ $ phpcsFile ->addError ($ error , $ stackPtr , 'ElvisSpacing ' );
108
+ }
109
+ } else {
110
+ $ spaceAfter = (($ tokens [$ contentAfter ]['column ' ]) - ($ tokens [$ stackPtr ]['column ' ] + 1 ));
111
+ if ($ spaceAfter !== 1 ) {
112
+ $ error = 'Inline shorthand IF statement requires 1 space after THEN; %s found ' ;
113
+ $ data = array ($ spaceAfter );
114
+ $ phpcsFile ->addError ($ error , $ stackPtr , 'SpacingAfterThen ' , $ data );
115
+ }
116
+
117
+ // Make sure the ELSE has the correct spacing.
118
+ $ inlineElse = $ phpcsFile ->findNext (T_INLINE_ELSE , ($ stackPtr + 1 ), $ statementEnd , false );
119
+ $ contentBefore = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ inlineElse - 1 ), null , true );
120
+ $ spaceBefore = ($ tokens [$ inlineElse ]['column ' ] - ($ tokens [$ contentBefore ]['column ' ] + $ tokens [$ contentBefore ]['length ' ]));
121
+ if ($ spaceBefore !== 1 ) {
122
+ $ error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found ' ;
123
+ $ data = array ($ spaceBefore );
124
+ $ phpcsFile ->addError ($ error , $ inlineElse , 'SpacingBeforeElse ' , $ data );
125
+ }
126
+ }//end if
127
+
128
+ $ contentAfter = $ phpcsFile ->findNext (T_WHITESPACE , ($ inlineElse + 1 ), null , true );
129
+ $ spaceAfter = (($ tokens [$ contentAfter ]['column ' ]) - ($ tokens [$ inlineElse ]['column ' ] + 1 ));
119
130
if ($ spaceAfter !== 1 ) {
120
131
$ error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found ' ;
121
132
$ data = array ($ spaceAfter );
0 commit comments