Skip to content

Commit bef1fc8

Browse files
committed
Fixed bug #778 : PHPCBF creates invalid PHP for inline FOREACH containing multiple control structures
1 parent c1b0593 commit bef1fc8

File tree

5 files changed

+92
-18
lines changed

5 files changed

+92
-18
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,38 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
161161
}
162162

163163
if (isset($tokens[$end]['scope_opener']) === true) {
164-
$end = $tokens[$end]['scope_closer'];
164+
$type = $tokens[$end]['code'];
165+
$end = $tokens[$end]['scope_closer'];
166+
if ($type === T_DO || $type === T_IF || $type === T_ELSEIF) {
167+
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($end + 1), null, true);
168+
if ($next === false) {
169+
break;
170+
}
171+
172+
$nextType = $tokens[$next]['code'];
173+
174+
// Let additional conditions loop and find their ending.
175+
if (($type === T_IF
176+
|| $type === T_ELSEIF)
177+
&& ($nextType === T_ELSEIF
178+
|| $nextType === T_ELSE)
179+
) {
180+
continue;
181+
}
182+
183+
// Account for DO... WHILE conditions.
184+
if ($type === T_DO && $nextType === T_WHILE) {
185+
$end = $phpcsFile->findNext(T_SEMICOLON, ($next + 1));
186+
}
187+
}//end if
188+
165189
break;
166-
}
190+
}//end if
167191

168192
if ($tokens[$end]['code'] !== T_WHITESPACE) {
169193
$lastNonEmpty = $end;
170194
}
171-
}
195+
}//end for
172196

173197
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
174198

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,26 @@ switch($response = \Bar::baz('bat', function ($foo) {
115115
case 2:
116116
return 'other';
117117
}
118+
119+
$stuff = [1,2,3];
120+
foreach($stuff as $num)
121+
if ($num %2 ) {
122+
echo "even";
123+
} else {
124+
echo "odd";
125+
}
126+
127+
$i = 0;
128+
foreach($stuff as $num)
129+
do {
130+
echo $i;
131+
$i++;
132+
} while ($i < 5);
133+
134+
foreach($stuff as $num)
135+
if (true) {
136+
echo "true1\n";
137+
}
138+
if (true) {
139+
echo "true2\n";
140+
}

CodeSniffer/Standards/Generic/Tests/ControlStructures/InlineControlStructureUnitTest.inc.fixed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,26 @@ switch($response = \Bar::baz('bat', function ($foo) {
116116
case 2:
117117
return 'other';
118118
}
119+
120+
$stuff = [1,2,3];
121+
foreach($stuff as $num) {
122+
if ($num %2 ) {
123+
echo "even";
124+
} else {
125+
echo "odd";
126+
} }
127+
128+
$i = 0;
129+
foreach($stuff as $num) {
130+
do {
131+
echo $i;
132+
$i++;
133+
} while ($i < 5); }
134+
135+
foreach($stuff as $num) {
136+
if (true) {
137+
echo "true1\n";
138+
} }
139+
if (true) {
140+
echo "true2\n";
141+
}

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,24 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.inc')
4747
switch ($testFile) {
4848
case 'InlineControlStructureUnitTest.inc':
4949
return array(
50-
3 => 1,
51-
7 => 1,
52-
11 => 1,
53-
13 => 1,
54-
15 => 1,
55-
17 => 1,
56-
23 => 1,
57-
42 => 1,
58-
43 => 1,
59-
45 => 1,
60-
46 => 1,
61-
49 => 1,
62-
62 => 1,
63-
66 => 1,
64-
78 => 1,
50+
3 => 1,
51+
7 => 1,
52+
11 => 1,
53+
13 => 1,
54+
15 => 1,
55+
17 => 1,
56+
23 => 1,
57+
42 => 1,
58+
43 => 1,
59+
45 => 1,
60+
46 => 1,
61+
49 => 1,
62+
62 => 1,
63+
66 => 1,
64+
78 => 1,
65+
120 => 1,
66+
128 => 1,
67+
134 => 1,
6568
);
6669
break;
6770
case 'InlineControlStructureUnitTest.js':

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
7373
- Fixed bug #769 : Incorrect detection of variable reference operator when used with short array syntax
7474
-- Thanks to Klaus Purer for the patch
7575
- Fixed bug #772 : Syntax error when using PHPCBF on alternative style foreach loops
76+
- Fixed bug #778 : PHPCBF creates invalid PHP for inline FOREACH containing multiple control structures
7677
- Fixed bug #781 : Incorrect checking for PHP7 return types on multi-line function declartions
7778
- Fixed bug #782 : Conditional function declarations cause fixing conflicts in Squiz standard
7879
-- Squiz.ControlStructures.ControlSignature no longer enforces a single newline after open brace

0 commit comments

Comments
 (0)