Skip to content

Commit a5d3f14

Browse files
committed
Fixed bug PHPCSStandards#1120 : InlineControlStructureSniff does not handle auto-fixing for control structures that make function calls
Also improved the formatting of the end brace when auto fixing InlineControlStructure errors (request PHPCSStandards#1121) as it made the original bug fix a little easier.
1 parent 71945d9 commit a5d3f14

File tree

6 files changed

+153
-34
lines changed

6 files changed

+153
-34
lines changed

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,28 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
202202
}
203203
}//end if
204204

205-
break;
205+
if ($tokens[$end]['code'] !== T_END_HEREDOC
206+
&& $tokens[$end]['code'] !== T_END_NOWDOC
207+
) {
208+
break;
209+
}
206210
}//end if
207211

212+
if (isset($tokens[$end]['parenthesis_closer']) === true) {
213+
$end = $tokens[$end]['parenthesis_closer'];
214+
$lastNonEmpty = $end;
215+
continue;
216+
}
217+
208218
if ($tokens[$end]['code'] !== T_WHITESPACE) {
209219
$lastNonEmpty = $end;
210220
}
211221
}//end for
212222

223+
if ($end === $phpcsFile->numTokens) {
224+
$end = $lastNonEmpty;
225+
}
226+
213227
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
214228

215229
// Account for a comment on the end of the line.
@@ -227,16 +241,50 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
227241

228242
if ($next !== $end) {
229243
if ($endLine !== $end) {
230-
$phpcsFile->fixer->addContent($endLine, '}');
244+
$endToken = $endLine;
245+
$addedContent = '';
231246
} else {
247+
$endToken = $end;
248+
$addedContent = $phpcsFile->eolChar;
249+
232250
if ($tokens[$end]['code'] !== T_SEMICOLON
233251
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
234252
) {
235-
$phpcsFile->fixer->addContent($end, ';');
253+
$phpcsFile->fixer->addContent($end, '; ');
236254
}
237-
238-
$phpcsFile->fixer->addContent($end, ' }');
239255
}
256+
257+
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
258+
if ($next !== false
259+
&& ($tokens[$next]['code'] === T_ELSE
260+
|| $tokens[$next]['code'] === T_ELSEIF)
261+
) {
262+
$phpcsFile->fixer->addContentBefore($next, '} ');
263+
} else {
264+
$indent = '';
265+
for ($first = $stackPtr; $first > 0; $first--) {
266+
if ($first === 1
267+
|| $tokens[($first - 1)]['line'] !== $tokens[$first]['line']
268+
) {
269+
break;
270+
}
271+
}
272+
273+
if ($tokens[$first]['code'] === T_WHITESPACE) {
274+
$indent = $tokens[$first]['content'];
275+
} else if ($tokens[$first]['code'] === T_INLINE_HTML
276+
|| $tokens[$first]['code'] === T_OPEN_TAG
277+
) {
278+
$addedContent = '';
279+
}
280+
281+
$addedContent .= $indent.'}';
282+
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
283+
$addedContent .= $phpcsFile->eolChar;
284+
}
285+
286+
$phpcsFile->fixer->addContent($endToken, $addedContent);
287+
}//end if
240288
} else {
241289
if ($endLine !== $end) {
242290
$phpcsFile->fixer->replaceToken($end, '');

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,21 @@ foreach ($sql as $s)
163163
if ($this->neverAbort) $ret = false;
164164
else return false;
165165
}
166+
167+
if ($bar)
168+
if ($foo) echo 'hi'; // lol
169+
170+
if ($level == 'district')
171+
\DB::update(<<<EOD
172+
some
173+
text
174+
here
175+
EOD
176+
);
177+
178+
if ($level == 'district')
179+
$var = <<<EOD
180+
some
181+
text
182+
here
183+
EOD;

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

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
<?php
22

3-
if ($something) { echo 'hello'; }
3+
if ($something) { echo 'hello';
4+
}
45

56
if ($something) {
67
echo 'hello';
7-
} else { echo 'hi'; }
8+
} else { echo 'hi';
9+
}
810

911
if ($something) {
1012
echo 'hello';
11-
} else if ($else) { echo 'hi'; }
13+
} else if ($else) { echo 'hi';
14+
}
1215

13-
foreach ($something as $thing) { echo 'hello'; }
16+
foreach ($something as $thing) { echo 'hello';
17+
}
1418

15-
for ($i; $i > 0; $i--) { echo 'hello'; }
19+
for ($i; $i > 0; $i--) { echo 'hello';
20+
}
1621

17-
while ($something) { echo 'hello'; }
22+
while ($something) { echo 'hello';
23+
}
1824

1925
do {
2026
$i--;
2127
} while ($something);
2228

2329
if(true) {
24-
$someObject->{$name}; }
30+
$someObject->{$name};
31+
}
2532

2633
if (true) :
2734
$foo = true;
@@ -45,11 +52,14 @@ while (!$this->readLine($tokens, $tag)) {
4552
}
4653
foreach ($cookies as $cookie) {
4754
if ($cookie->match($uri, $matchSessionCookies, $now)) {
48-
$ret[] = $cookie; } }
55+
$ret[] = $cookie;
56+
}
57+
}
4958

5059
foreach ($stringParade as $hit) {
5160
$hitParade[] = $hit + 0; //cast to integer
5261
}
62+
5363
if ($foo) :
5464
echo 'true';
5565
elseif ($something) :
@@ -61,7 +71,8 @@ endif;
6171
function test()
6272
{
6373
if ($a) {
64-
$a.=' '.($b ? 'b' : ($c ? ($d ? 'd' : 'c') : '')); }
74+
$a.=' '.($b ? 'b' : ($c ? ($d ? 'd' : 'c') : ''));
75+
}
6576
}
6677

6778
if ($a) {
@@ -72,7 +83,8 @@ if ($a) {
7283
} elseif ($i==0) {
7384
$j=$k;
7485
}
75-
} }
86+
}
87+
}
7688

7789
?>
7890
<div style="text-align: right;">
@@ -123,44 +135,73 @@ foreach($stuff as $num) {
123135
echo "even";
124136
} else {
125137
echo "odd";
126-
} }
138+
}
139+
}
127140

128141
$i = 0;
129142
foreach($stuff as $num) {
130143
do {
131144
echo $i;
132145
$i++;
133-
} while ($i < 5); }
146+
} while ($i < 5);
147+
}
134148

135149
foreach($stuff as $num) {
136150
if (true) {
137151
echo "true1\n";
138-
} }
152+
}
153+
}
139154
if (true) {
140155
echo "true2\n";
141156
}
142157

143-
if ($foo) { echo 'foo'; }
144-
elseif ($bar) { echo 'bar'; }
145-
else { echo 'baz'; }
158+
if ($foo) { echo 'foo';
159+
} elseif ($bar) { echo 'bar';
160+
} else { echo 'baz';
161+
}
146162

147163
switch ($type) {
148164
case 1:
149165
if ($foo) {
150166
return true;
151167
} elseif ($baz) {
152-
return true; }
153-
else {
168+
return true;
169+
} else {
154170
echo 'else';
155171
}
156172
break;
157173
}
158174

159175
foreach ($sql as $s) {
160-
if (!$this->execute) { echo "<pre>",$s.";\n</pre>"; }
161-
else {
176+
if (!$this->execute) { echo "<pre>",$s.";\n</pre>";
177+
} else {
162178
$ok = $this->connDest->Execute($s);
163179
if (!$ok) {
164-
if ($this->neverAbort) { $ret = false; }
165-
else { return false; } }
166-
} }
180+
if ($this->neverAbort) { $ret = false;
181+
} else { return false;
182+
}
183+
}
184+
}
185+
}
186+
187+
if ($bar) {
188+
if ($foo) { echo 'hi'; // lol
189+
}
190+
}
191+
192+
if ($level == 'district') {
193+
\DB::update(<<<EOD
194+
some
195+
text
196+
here
197+
EOD
198+
);
199+
}
200+
201+
if ($level == 'district') {
202+
$var = <<<EOD
203+
some
204+
text
205+
here
206+
EOD;
207+
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11

22

3-
if (something) { print 'hello'; }
3+
if (something) { print 'hello';
4+
}
45

56
if (something) {
67
print 'hello';
7-
} else { print 'hi'; }
8+
} else { print 'hi';
9+
}
810

911
if (something) {
1012
print 'hello';
11-
} else if (something) { print 'hi'; }
13+
} else if (something) { print 'hi';
14+
}
1215

13-
for (i; i > 0; i--) { print 'hello'; }
16+
for (i; i > 0; i--) { print 'hello';
17+
}
1418

15-
while (something) { print 'hello'; }
19+
while (something) { print 'hello';
20+
}
1621

1722
do {
1823
i--;
1924
} while (something);
2025

21-
do { i++; } while (i < 5);
26+
do { i++;
27+
} while (i < 5);
2228

2329
SomeClass.prototype.switch = function() {
2430
// do something

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.inc')
7474
162 => 1,
7575
163 => 1,
7676
164 => 1,
77+
167 => 1,
78+
168 => 1,
79+
170 => 1,
80+
178 => 1,
7781
);
7882
break;
7983
case 'InlineControlStructureUnitTest.js':

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6969
-- Thanks to Walt Sorensen for the patch
7070
- PHPCBF is now able to fix Generic.PHP.DisallowShortOpenTag
7171
-- Thanks to Juliette Reinders Folmer for the patch
72+
- Improved the formatting of the end brace when auto fixing InlineControlStructure errors (request #1121)
7273
- Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine fix no longer leaves blank line after brace (request #1085)
7374
- Generic UpperCaseConstantNameSniff now allows lowercase namespaces in constant definitions
7475
-- Thanks to Daniel Schniepp for the patch
@@ -92,6 +93,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
9293
- Fixed bug #1102 : Squiz.Formatting.OperatorBracket.MissingBrackets faulty bracketing fix
9394
- Fixed bug #1109 : Wrong scope indent reported in anonymous class
9495
- Fixed bug #1112 : File docblock not recognized when require_once follows it
96+
- Fixed bug #1120 : InlineControlStructureSniff does not handle auto-fixing for control structures that make function calls
9597
</notes>
9698
<contents>
9799
<dir name="/">

0 commit comments

Comments
 (0)