@@ -134,107 +134,110 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
134
134
135
135
$ phpcsFile ->recordMetric ($ stackPtr , 'Control structure defined inline ' , 'yes ' );
136
136
137
- if ($ fix === true ) {
138
- $ phpcsFile ->fixer ->beginChangeset ();
139
- if (isset ($ tokens [$ stackPtr ]['parenthesis_closer ' ]) === true ) {
140
- $ closer = $ tokens [$ stackPtr ]['parenthesis_closer ' ];
141
- } else {
142
- $ closer = $ stackPtr ;
137
+ // Stop here if we are not fixing the error.
138
+ if ($ fix !== true ) {
139
+ return ;
140
+ }
141
+
142
+ $ phpcsFile ->fixer ->beginChangeset ();
143
+ if (isset ($ tokens [$ stackPtr ]['parenthesis_closer ' ]) === true ) {
144
+ $ closer = $ tokens [$ stackPtr ]['parenthesis_closer ' ];
145
+ } else {
146
+ $ closer = $ stackPtr ;
147
+ }
148
+
149
+ if ($ tokens [($ closer + 1 )]['code ' ] === T_WHITESPACE
150
+ || $ tokens [($ closer + 1 )]['code ' ] === T_SEMICOLON
151
+ ) {
152
+ $ phpcsFile ->fixer ->addContent ($ closer , ' { ' );
153
+ } else {
154
+ $ phpcsFile ->fixer ->addContent ($ closer , ' { ' );
155
+ }
156
+
157
+ $ lastNonEmpty = $ closer ;
158
+ for ($ end = ($ closer + 1 ); $ end < $ phpcsFile ->numTokens ; $ end ++) {
159
+ if ($ tokens [$ end ]['code ' ] === T_SEMICOLON ) {
160
+ break ;
143
161
}
144
162
145
- if ($ tokens [($ closer + 1 )]['code ' ] === T_WHITESPACE
146
- || $ tokens [($ closer + 1 )]['code ' ] === T_SEMICOLON
147
- ) {
148
- $ phpcsFile ->fixer ->addContent ($ closer , ' { ' );
149
- } else {
150
- $ phpcsFile ->fixer ->addContent ($ closer , ' { ' );
163
+ if ($ tokens [$ end ]['code ' ] === T_CLOSE_TAG ) {
164
+ $ end = $ lastNonEmpty ;
165
+ break ;
151
166
}
152
167
153
- $ lastNonEmpty = $ closer ;
154
- for ($ end = ($ closer + 1 ); $ end < $ phpcsFile ->numTokens ; $ end ++) {
155
- if ($ tokens [$ end ]['code ' ] === T_SEMICOLON ) {
156
- break ;
157
- }
168
+ if (isset ($ tokens [$ end ]['scope_opener ' ]) === true ) {
169
+ $ type = $ tokens [$ end ]['code ' ];
170
+ $ end = $ tokens [$ end ]['scope_closer ' ];
171
+ if ($ type === T_DO || $ type === T_IF || $ type === T_ELSEIF ) {
172
+ $ next = $ phpcsFile ->findNext (PHP_CodeSniffer_Tokens::$ emptyTokens , ($ end + 1 ), null , true );
173
+ if ($ next === false ) {
174
+ break ;
175
+ }
158
176
159
- if ($ tokens [$ end ]['code ' ] === T_CLOSE_TAG ) {
160
- $ end = $ lastNonEmpty ;
161
- break ;
162
- }
177
+ $ nextType = $ tokens [$ next ]['code ' ];
163
178
164
- if (isset ($ tokens [$ end ]['scope_opener ' ]) === true ) {
165
- $ type = $ tokens [$ end ]['code ' ];
166
- $ end = $ tokens [$ end ]['scope_closer ' ];
167
- if ($ type === T_DO || $ type === T_IF || $ type === T_ELSEIF ) {
168
- $ next = $ phpcsFile ->findNext (PHP_CodeSniffer_Tokens::$ emptyTokens , ($ end + 1 ), null , true );
169
- if ($ next === false ) {
170
- break ;
171
- }
172
-
173
- $ nextType = $ tokens [$ next ]['code ' ];
174
-
175
- // Let additional conditions loop and find their ending.
176
- if (($ type === T_IF
177
- || $ type === T_ELSEIF )
178
- && ($ nextType === T_ELSEIF
179
- || $ nextType === T_ELSE )
180
- ) {
181
- continue ;
182
- }
183
-
184
- // Account for DO... WHILE conditions.
185
- if ($ type === T_DO && $ nextType === T_WHILE ) {
186
- $ end = $ phpcsFile ->findNext (T_SEMICOLON , ($ next + 1 ));
187
- }
188
- }//end if
189
-
190
- break ;
191
- }//end if
179
+ // Let additional conditions loop and find their ending.
180
+ if (($ type === T_IF
181
+ || $ type === T_ELSEIF )
182
+ && ($ nextType === T_ELSEIF
183
+ || $ nextType === T_ELSE )
184
+ ) {
185
+ continue ;
186
+ }
192
187
193
- if ($ tokens [$ end ]['code ' ] !== T_WHITESPACE ) {
194
- $ lastNonEmpty = $ end ;
195
- }
196
- }//end for
188
+ // Account for DO... WHILE conditions.
189
+ if ($ type === T_DO && $ nextType === T_WHILE ) {
190
+ $ end = $ phpcsFile ->findNext (T_SEMICOLON , ($ next + 1 ));
191
+ }
192
+ }//end if
197
193
198
- $ next = $ phpcsFile ->findNext (T_WHITESPACE , ($ closer + 1 ), ($ end + 1 ), true );
194
+ break ;
195
+ }//end if
199
196
200
- // Account for a comment on the end of the line.
201
- for ($ endLine = $ end ; $ endLine < $ phpcsFile ->numTokens ; $ endLine ++) {
202
- if (isset ($ tokens [($ endLine + 1 )]) === false
203
- || $ tokens [$ endLine ]['line ' ] !== $ tokens [($ endLine + 1 )]['line ' ]
204
- ) {
205
- break ;
206
- }
197
+ if ($ tokens [$ end ]['code ' ] !== T_WHITESPACE ) {
198
+ $ lastNonEmpty = $ end ;
207
199
}
200
+ }//end for
208
201
209
- if ($ tokens [$ endLine ]['code ' ] !== T_COMMENT ) {
210
- $ endLine = $ end ;
202
+ $ next = $ phpcsFile ->findNext (T_WHITESPACE , ($ closer + 1 ), ($ end + 1 ), true );
203
+
204
+ // Account for a comment on the end of the line.
205
+ for ($ endLine = $ end ; $ endLine < $ phpcsFile ->numTokens ; $ endLine ++) {
206
+ if (isset ($ tokens [($ endLine + 1 )]) === false
207
+ || $ tokens [$ endLine ]['line ' ] !== $ tokens [($ endLine + 1 )]['line ' ]
208
+ ) {
209
+ break ;
211
210
}
211
+ }
212
212
213
- if ($ next !== $ end ) {
214
- if ($ endLine !== $ end ) {
215
- $ phpcsFile ->fixer ->addContent ($ endLine , '} ' );
216
- } else {
217
- if ($ tokens [$ end ]['code ' ] !== T_SEMICOLON
218
- && $ tokens [$ end ]['code ' ] !== T_CLOSE_CURLY_BRACKET
219
- ) {
220
- $ phpcsFile ->fixer ->addContent ($ end , '; ' );
221
- }
213
+ if ($ tokens [$ endLine ]['code ' ] !== T_COMMENT ) {
214
+ $ endLine = $ end ;
215
+ }
222
216
223
- $ phpcsFile ->fixer ->addContent ($ end , ' } ' );
224
- }
217
+ if ($ next !== $ end ) {
218
+ if ($ endLine !== $ end ) {
219
+ $ phpcsFile ->fixer ->addContent ($ endLine , '} ' );
225
220
} else {
226
- if ($ endLine !== $ end ) {
227
- $ phpcsFile ->fixer ->replaceToken ($ end , '' );
228
- $ phpcsFile ->fixer ->addNewlineBefore ($ endLine );
229
- $ phpcsFile ->fixer ->addContent ($ endLine , '} ' );
230
- } else {
231
- $ phpcsFile ->fixer ->replaceToken ($ end , '} ' );
221
+ if ($ tokens [$ end ]['code ' ] !== T_SEMICOLON
222
+ && $ tokens [$ end ]['code ' ] !== T_CLOSE_CURLY_BRACKET
223
+ ) {
224
+ $ phpcsFile ->fixer ->addContent ($ end , '; ' );
232
225
}
233
- }//end if
234
226
235
- $ phpcsFile ->fixer ->endChangeset ();
227
+ $ phpcsFile ->fixer ->addContent ($ end , ' } ' );
228
+ }
229
+ } else {
230
+ if ($ endLine !== $ end ) {
231
+ $ phpcsFile ->fixer ->replaceToken ($ end , '' );
232
+ $ phpcsFile ->fixer ->addNewlineBefore ($ endLine );
233
+ $ phpcsFile ->fixer ->addContent ($ endLine , '} ' );
234
+ } else {
235
+ $ phpcsFile ->fixer ->replaceToken ($ end , '} ' );
236
+ }
236
237
}//end if
237
238
239
+ $ phpcsFile ->fixer ->endChangeset ();
240
+
238
241
}//end process()
239
242
240
243
0 commit comments