Skip to content

Commit 1141cb8

Browse files
committed
Merge branch 'feature/2822-generic-inlinecontrolstructures-bug-fix' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 952426c + 13c803b commit 1141cb8

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,26 @@ public function process(File $phpcsFile, $stackPtr)
8181
}
8282
}
8383

84-
if ($tokens[$stackPtr]['code'] === T_WHILE) {
85-
// This could be from a DO WHILE, which doesn't have an opening brace.
86-
$lastContent = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
87-
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) {
88-
$brace = $tokens[$lastContent];
89-
if (isset($brace['scope_condition']) === true) {
90-
$condition = $tokens[$brace['scope_condition']];
91-
if ($condition['code'] === T_DO) {
92-
return;
93-
}
84+
if ($tokens[$stackPtr]['code'] === T_WHILE || $tokens[$stackPtr]['code'] === T_FOR) {
85+
// This could be from a DO WHILE, which doesn't have an opening brace or a while/for without body.
86+
if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) {
87+
$afterParensCloser = $phpcsFile->findNext(Tokens::$emptyTokens, ($tokens[$stackPtr]['parenthesis_closer'] + 1), null, true);
88+
if ($afterParensCloser === false) {
89+
// Live coding.
90+
return;
91+
}
92+
93+
if ($tokens[$afterParensCloser]['code'] === T_SEMICOLON) {
94+
$phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no');
95+
return;
9496
}
9597
}
9698

9799
// In Javascript DO WHILE loops without curly braces are legal. This
98100
// is only valid if a single statement is present between the DO and
99101
// the WHILE. We can detect this by checking only a single semicolon
100102
// is present between them.
101-
if ($phpcsFile->tokenizerType === 'JS') {
103+
if ($tokens[$stackPtr]['code'] === T_WHILE && $phpcsFile->tokenizerType === 'JS') {
102104
$lastDo = $phpcsFile->findPrevious(T_DO, ($stackPtr - 1));
103105
$lastSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, ($stackPtr - 1));
104106
if ($lastDo !== false && $lastSemicolon !== false && $lastDo < $lastSemicolon) {

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc

+6
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,9 @@ echo 'bar';
244244
{
245245
echo 'baz';
246246
}
247+
248+
// Issue 2822.
249+
$i = 10;
250+
while ($i > 0 && --$i);
251+
252+
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.1.inc.fixed

+13-10
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ foreach ($array as $element) :
4646
echo 'hello';
4747
endforeach;
4848

49-
while (!$this->readLine($tokens, $tag)) {}
50-
while (!$this->readLine($tokens, $tag)) {
51-
//skip to end of file
52-
}
49+
while (!$this->readLine($tokens, $tag));
50+
while (!$this->readLine($tokens, $tag)); //skip to end of file
51+
5352
foreach ($cookies as $cookie) {
5453
if ($cookie->match($uri, $matchSessionCookies, $now)) {
5554
$ret[] = $cookie;
@@ -254,14 +253,12 @@ if ($this) {
254253
}
255254
}
256255

257-
while (!$this->readLine($tokens, $tag)) {} //phpcs:ignore Standard.Category.Sniff
256+
while (!$this->readLine($tokens, $tag)); //phpcs:ignore Standard.Category.Sniff
258257

259-
while (!$this->readLine($tokens, $tag)) {
260-
// comment
261-
}
262-
while (!$this->readLine($tokens, $tag)) { /* comment */
258+
while (!$this->readLine($tokens, $tag)); // comment
259+
260+
while (!$this->readLine($tokens, $tag)); /* comment */
263261

264-
}
265262
foreach ($stringParade as $hit) {
266263
$hitParade[] = $hit + 0; // phpcs:ignore Standard.Category.Sniff
267264
}
@@ -280,3 +277,9 @@ echo 'bar';
280277
{
281278
echo 'baz';
282279
}
280+
281+
// Issue 2822.
282+
$i = 10;
283+
while ($i > 0 && --$i);
284+
285+
for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);

src/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.1.inc')
3737
15 => 1,
3838
17 => 1,
3939
23 => 1,
40-
42 => 1,
41-
43 => 1,
4240
45 => 1,
4341
46 => 1,
4442
49 => 1,
@@ -68,9 +66,6 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.1.inc')
6866
198 => 1,
6967
206 => 1,
7068
222 => 1,
71-
226 => 1,
72-
228 => 1,
73-
230 => 1,
7469
232 => 1,
7570
235 => 1,
7671
236 => 1,

0 commit comments

Comments
 (0)