@@ -92,7 +92,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
92
92
if ($ spaceBefore !== 1 ) {
93
93
$ error = 'Inline shorthand IF statement requires 1 space before THEN; %s found ' ;
94
94
$ data = array ($ spaceBefore );
95
- $ phpcsFile ->addError ($ error , $ stackPtr , 'SpacingBeforeThen ' , $ data );
95
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'SpacingBeforeThen ' , $ data );
96
+ if ($ fix === true ) {
97
+ if ($ spaceBefore === 0 ) {
98
+ $ phpcsFile ->fixer ->addContentBefore ($ stackPtr , ' ' );
99
+ } else {
100
+ $ phpcsFile ->fixer ->replaceToken (($ stackPtr - 1 ), ' ' );
101
+ }
102
+ }
96
103
}
97
104
98
105
// If there is no content between the ? and the : operators, then they are
@@ -104,14 +111,22 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
104
111
$ inlineElse = $ next ;
105
112
if ($ inlineElse !== ($ stackPtr + 1 )) {
106
113
$ error = 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE ' ;
107
- $ phpcsFile ->addError ($ error , $ stackPtr , 'ElvisSpacing ' );
114
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'ElvisSpacing ' );
115
+ if ($ fix === true ) {
116
+ $ phpcsFile ->fixer ->replaceToken (($ stackPtr + 1 ), '' );
117
+ }
108
118
}
109
119
} else {
110
120
$ spaceAfter = (($ tokens [$ contentAfter ]['column ' ]) - ($ tokens [$ stackPtr ]['column ' ] + 1 ));
111
121
if ($ spaceAfter !== 1 ) {
112
122
$ error = 'Inline shorthand IF statement requires 1 space after THEN; %s found ' ;
113
123
$ data = array ($ spaceAfter );
114
- $ phpcsFile ->addError ($ error , $ stackPtr , 'SpacingAfterThen ' , $ data );
124
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'SpacingAfterThen ' , $ data );
125
+ if ($ spaceAfter === 0 ) {
126
+ $ phpcsFile ->fixer ->addContent ($ stackPtr , ' ' );
127
+ } else {
128
+ $ phpcsFile ->fixer ->replaceToken (($ stackPtr + 1 ), ' ' );
129
+ }
115
130
}
116
131
117
132
// Make sure the ELSE has the correct spacing.
@@ -121,7 +136,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
121
136
if ($ spaceBefore !== 1 ) {
122
137
$ error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found ' ;
123
138
$ data = array ($ spaceBefore );
124
- $ phpcsFile ->addError ($ error , $ inlineElse , 'SpacingBeforeElse ' , $ data );
139
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ inlineElse , 'SpacingBeforeElse ' , $ data );
140
+ if ($ fix === true ) {
141
+ if ($ spaceBefore === 0 ) {
142
+ $ phpcsFile ->fixer ->addContentBefore ($ inlineElse , ' ' );
143
+ } else {
144
+ $ phpcsFile ->fixer ->replaceToken (($ inlineElse - 1 ), ' ' );
145
+ }
146
+ }
125
147
}
126
148
}//end if
127
149
@@ -130,7 +152,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
130
152
if ($ spaceAfter !== 1 ) {
131
153
$ error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found ' ;
132
154
$ data = array ($ spaceAfter );
133
- $ phpcsFile ->addError ($ error , $ inlineElse , 'SpacingAfterElse ' , $ data );
155
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ inlineElse , 'SpacingAfterElse ' , $ data );
156
+ if ($ spaceAfter === 0 ) {
157
+ $ phpcsFile ->fixer ->addContent ($ inlineElse , ' ' );
158
+ } else {
159
+ $ phpcsFile ->fixer ->replaceToken (($ inlineElse + 1 ), ' ' );
160
+ }
134
161
}
135
162
136
163
}//end process()
0 commit comments