@@ -172,80 +172,69 @@ public function process(File $phpcsFile, $stackPtr)
172
172
}//end if
173
173
}//end if
174
174
175
- $ firstSemicolon = $ phpcsFile ->findNext (T_SEMICOLON , $ openingBracket , $ closingBracket );
175
+ /*
176
+ * Check whitespace around each of the semicolon tokens.
177
+ */
176
178
177
- // Check whitespace around each of the tokens.
178
- if ($ firstSemicolon !== false ) {
179
- if ($ tokens [($ firstSemicolon - 1 )]['code ' ] === T_WHITESPACE ) {
180
- $ error = 'Space found before first semicolon of FOR loop ' ;
181
- $ fix = $ phpcsFile ->addFixableError ($ error , $ firstSemicolon , 'SpacingBeforeFirst ' );
179
+ $ semicolonCount = 0 ;
180
+ $ semicolon = $ openingBracket ;
181
+
182
+ do {
183
+ $ semicolon = $ phpcsFile ->findNext (T_SEMICOLON , ($ semicolon + 1 ), $ closingBracket );
184
+ if ($ semicolon === false ) {
185
+ break ;
186
+ }
187
+
188
+ ++$ semicolonCount ;
189
+
190
+ $ humanReadableCount = 'first ' ;
191
+ if ($ semicolonCount !== 1 ) {
192
+ $ humanReadableCount = 'second ' ;
193
+ }
194
+
195
+ $ humanReadableCode = ucfirst ($ humanReadableCount );
196
+ $ data = [$ humanReadableCount ];
197
+
198
+ if ($ tokens [($ semicolon - 1 )]['code ' ] === T_WHITESPACE ) {
199
+ $ error = 'Space found before %s semicolon of FOR loop ' ;
200
+ $ errorCode = 'SpacingBefore ' .$ humanReadableCode ;
201
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ semicolon , $ errorCode , $ data );
182
202
if ($ fix === true ) {
183
- $ phpcsFile ->fixer ->replaceToken (($ firstSemicolon - 1 ), '' );
203
+ $ phpcsFile ->fixer ->replaceToken (($ semicolon - 1 ), '' );
184
204
}
185
205
}
186
206
187
- if ($ tokens [($ firstSemicolon + 1 )]['code ' ] !== T_WHITESPACE
188
- && $ tokens [($ firstSemicolon + 1 )]['code ' ] !== T_SEMICOLON
207
+ if ($ tokens [($ semicolon + 1 )]['code ' ] !== T_WHITESPACE
208
+ && $ tokens [($ semicolon + 1 )]['code ' ] !== T_SEMICOLON
209
+ && ($ semicolon + 1 ) !== $ closingBracket
189
210
) {
190
- $ error = 'Expected 1 space after first semicolon of FOR loop; 0 found ' ;
191
- $ fix = $ phpcsFile ->addFixableError ($ error , $ firstSemicolon , 'NoSpaceAfterFirst ' );
211
+ $ error = 'Expected 1 space after %s semicolon of FOR loop; 0 found ' ;
212
+ $ errorCode = 'NoSpaceAfter ' .$ humanReadableCode ;
213
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ semicolon , $ errorCode , $ data );
192
214
if ($ fix === true ) {
193
- $ phpcsFile ->fixer ->addContent ($ firstSemicolon , ' ' );
215
+ $ phpcsFile ->fixer ->addContent ($ semicolon , ' ' );
194
216
}
195
217
} else {
196
- if (strlen ($ tokens [($ firstSemicolon + 1 )]['content ' ]) !== 1 ) {
197
- $ spaces = strlen ($ tokens [($ firstSemicolon + 1 )]['content ' ]);
198
- $ error = 'Expected 1 space after first semicolon of FOR loop; %s found ' ;
199
- $ data = [$ spaces ];
200
- $ fix = $ phpcsFile ->addFixableError ($ error , $ firstSemicolon , 'SpacingAfterFirst ' , $ data );
201
- if ($ fix === true ) {
202
- $ phpcsFile ->fixer ->replaceToken (($ firstSemicolon + 1 ), ' ' );
203
- }
204
- }
205
- }
206
-
207
- $ secondSemicolon = $ phpcsFile ->findNext (T_SEMICOLON , ($ firstSemicolon + 1 ));
208
-
209
- if ($ secondSemicolon !== false ) {
210
- if ($ tokens [($ secondSemicolon - 1 )]['code ' ] === T_WHITESPACE
211
- && $ tokens [($ firstSemicolon + 1 )]['code ' ] !== T_SEMICOLON
212
- ) {
213
- $ error = 'Space found before second semicolon of FOR loop ' ;
214
- $ fix = $ phpcsFile ->addFixableError ($ error , $ secondSemicolon , 'SpacingBeforeSecond ' );
215
- if ($ fix === true ) {
216
- $ phpcsFile ->fixer ->replaceToken (($ secondSemicolon - 1 ), '' );
217
- }
218
- }
219
-
220
- if (($ secondSemicolon + 1 ) !== $ closingBracket
221
- && $ tokens [($ secondSemicolon + 1 )]['code ' ] !== T_WHITESPACE
222
- ) {
223
- $ error = 'Expected 1 space after second semicolon of FOR loop; 0 found ' ;
224
- $ fix = $ phpcsFile ->addFixableError ($ error , $ secondSemicolon , 'NoSpaceAfterSecond ' );
225
- if ($ fix === true ) {
226
- $ phpcsFile ->fixer ->addContent ($ secondSemicolon , ' ' );
227
- }
228
- } else {
229
- if (strlen ($ tokens [($ secondSemicolon + 1 )]['content ' ]) !== 1 ) {
230
- $ spaces = strlen ($ tokens [($ secondSemicolon + 1 )]['content ' ]);
231
- $ data = [$ spaces ];
232
- if (($ secondSemicolon + 2 ) === $ closingBracket ) {
233
- $ error = 'Expected no space after second semicolon of FOR loop; %s found ' ;
234
- $ fix = $ phpcsFile ->addFixableError ($ error , $ secondSemicolon , 'SpacingAfterSecondNoThird ' , $ data );
235
- if ($ fix === true ) {
236
- $ phpcsFile ->fixer ->replaceToken (($ secondSemicolon + 1 ), '' );
237
- }
238
- } else {
239
- $ error = 'Expected 1 space after second semicolon of FOR loop; %s found ' ;
240
- $ fix = $ phpcsFile ->addFixableError ($ error , $ secondSemicolon , 'SpacingAfterSecond ' , $ data );
241
- if ($ fix === true ) {
242
- $ phpcsFile ->fixer ->replaceToken (($ secondSemicolon + 1 ), ' ' );
243
- }
218
+ if (strlen ($ tokens [($ semicolon + 1 )]['content ' ]) !== 1 ) {
219
+ $ spaces = strlen ($ tokens [($ semicolon + 1 )]['content ' ]);
220
+ $ data [] = $ spaces ;
221
+ if ($ semicolonCount === 2 && ($ semicolon + 2 ) === $ closingBracket ) {
222
+ $ error = 'Expected no space after second semicolon of FOR loop; %s found ' ;
223
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ semicolon , 'SpacingAfterSecondNoThird ' , $ data );
224
+ if ($ fix === true ) {
225
+ $ phpcsFile ->fixer ->replaceToken (($ semicolon + 1 ), '' );
226
+ }
227
+ } else {
228
+ $ error = 'Expected 1 space after %s semicolon of FOR loop; %s found ' ;
229
+ $ errorCode = 'SpacingAfter ' .$ humanReadableCode ;
230
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ semicolon , $ errorCode , $ data );
231
+ if ($ fix === true ) {
232
+ $ phpcsFile ->fixer ->replaceToken (($ semicolon + 1 ), ' ' );
244
233
}
245
234
}
246
235
}//end if
247
236
}//end if
248
- }//end if
237
+ } while ( $ semicolonCount < 2 );
249
238
250
239
}//end process()
251
240
0 commit comments