Skip to content

Commit 493a70a

Browse files
committed
Merge branch 'master' into 3.0
2 parents 30a5eb0 + a5d3f14 commit 493a70a

File tree

6 files changed

+153
-34
lines changed

6 files changed

+153
-34
lines changed

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
8383
-- Thanks to Walt Sorensen for the patch
8484
- PHPCBF is now able to fix Generic.PHP.DisallowShortOpenTag
8585
-- Thanks to Juliette Reinders Folmer for the patch
86+
- Improved the formatting of the end brace when auto fixing InlineControlStructure errors (request #1121)
8687
- Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine fix no longer leaves blank line after brace (request #1085)
8788
- Generic UpperCaseConstantNameSniff now allows lowercase namespaces in constant definitions
8889
-- Thanks to Daniel Schniepp for the patch
@@ -106,6 +107,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
106107
- Fixed bug #1102 : Squiz.Formatting.OperatorBracket.MissingBrackets faulty bracketing fix
107108
- Fixed bug #1109 : Wrong scope indent reported in anonymous class
108109
- Fixed bug #1112 : File docblock not recognized when require_once follows it
110+
- Fixed bug #1120 : InlineControlStructureSniff does not handle auto-fixing for control structures that make function calls
109111
</notes>
110112
<contents>
111113
<dir name="/">

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,28 @@ public function process(File $phpcsFile, $stackPtr)
188188
}
189189
}//end if
190190

191-
break;
191+
if ($tokens[$end]['code'] !== T_END_HEREDOC
192+
&& $tokens[$end]['code'] !== T_END_NOWDOC
193+
) {
194+
break;
195+
}
192196
}//end if
193197

198+
if (isset($tokens[$end]['parenthesis_closer']) === true) {
199+
$end = $tokens[$end]['parenthesis_closer'];
200+
$lastNonEmpty = $end;
201+
continue;
202+
}
203+
194204
if ($tokens[$end]['code'] !== T_WHITESPACE) {
195205
$lastNonEmpty = $end;
196206
}
197207
}//end for
198208

209+
if ($end === $phpcsFile->numTokens) {
210+
$end = $lastNonEmpty;
211+
}
212+
199213
$next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true);
200214

201215
// Account for a comment on the end of the line.
@@ -213,16 +227,50 @@ public function process(File $phpcsFile, $stackPtr)
213227

214228
if ($next !== $end) {
215229
if ($endLine !== $end) {
216-
$phpcsFile->fixer->addContent($endLine, '}');
230+
$endToken = $endLine;
231+
$addedContent = '';
217232
} else {
233+
$endToken = $end;
234+
$addedContent = $phpcsFile->eolChar;
235+
218236
if ($tokens[$end]['code'] !== T_SEMICOLON
219237
&& $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET
220238
) {
221-
$phpcsFile->fixer->addContent($end, ';');
239+
$phpcsFile->fixer->addContent($end, '; ');
222240
}
223-
224-
$phpcsFile->fixer->addContent($end, ' }');
225241
}
242+
243+
$next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true);
244+
if ($next !== false
245+
&& ($tokens[$next]['code'] === T_ELSE
246+
|| $tokens[$next]['code'] === T_ELSEIF)
247+
) {
248+
$phpcsFile->fixer->addContentBefore($next, '} ');
249+
} else {
250+
$indent = '';
251+
for ($first = $stackPtr; $first > 0; $first--) {
252+
if ($first === 1
253+
|| $tokens[($first - 1)]['line'] !== $tokens[$first]['line']
254+
) {
255+
break;
256+
}
257+
}
258+
259+
if ($tokens[$first]['code'] === T_WHITESPACE) {
260+
$indent = $tokens[$first]['content'];
261+
} else if ($tokens[$first]['code'] === T_INLINE_HTML
262+
|| $tokens[$first]['code'] === T_OPEN_TAG
263+
) {
264+
$addedContent = '';
265+
}
266+
267+
$addedContent .= $indent.'}';
268+
if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) {
269+
$addedContent .= $phpcsFile->eolChar;
270+
}
271+
272+
$phpcsFile->fixer->addContent($endToken, $addedContent);
273+
}//end if
226274
} else {
227275
if ($endLine !== $end) {
228276
$phpcsFile->fixer->replaceToken($end, '');

src/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;

src/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+
}

src/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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function getErrorList($testFile='InlineControlStructureUnitTest.inc')
5757
162 => 1,
5858
163 => 1,
5959
164 => 1,
60+
167 => 1,
61+
168 => 1,
62+
170 => 1,
63+
178 => 1,
6064
);
6165
break;
6266
case 'InlineControlStructureUnitTest.js':

0 commit comments

Comments
 (0)