Skip to content

Commit 8c59d2b

Browse files
committed
Squiz/ForLoopDeclaration: refactor semi-colon related code
The first and second semi-colon examinations were basically the same, i.e. duplicate code. This refactors the semicolon examination to use a `do-while` loop on the semicolons within a `for`, removing the code duplication (and opening the way for further improvements in the next few commits). This commit does not change the behaviour of the sniff.
1 parent 7c77ab0 commit 8c59d2b

File tree

1 file changed

+50
-61
lines changed

1 file changed

+50
-61
lines changed

src/Standards/Squiz/Sniffs/ControlStructures/ForLoopDeclarationSniff.php

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -172,80 +172,69 @@ public function process(File $phpcsFile, $stackPtr)
172172
}//end if
173173
}//end if
174174

175-
$firstSemicolon = $phpcsFile->findNext(T_SEMICOLON, $openingBracket, $closingBracket);
175+
/*
176+
* Check whitespace around each of the semicolon tokens.
177+
*/
176178

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);
182202
if ($fix === true) {
183-
$phpcsFile->fixer->replaceToken(($firstSemicolon - 1), '');
203+
$phpcsFile->fixer->replaceToken(($semicolon - 1), '');
184204
}
185205
}
186206

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
189210
) {
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);
192214
if ($fix === true) {
193-
$phpcsFile->fixer->addContent($firstSemicolon, ' ');
215+
$phpcsFile->fixer->addContent($semicolon, ' ');
194216
}
195217
} 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), ' ');
244233
}
245234
}
246235
}//end if
247236
}//end if
248-
}//end if
237+
} while ($semicolonCount < 2);
249238

250239
}//end process()
251240

0 commit comments

Comments
 (0)