Skip to content

Commit f905241

Browse files
authored
Merge pull request #581 from fredden/fixer-conflict/PSR12.ControlStructures.ControlStructureSpacing
Fix conflict within PSR12.ControlStructures.ControlStructureSpacing
2 parents 41a426c + d9e164d commit f905241

File tree

4 files changed

+96
-19
lines changed

4 files changed

+96
-19
lines changed

src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

+12
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,19 @@ public function process(File $phpcsFile, $stackPtr)
101101
$error = 'The first expression of a multi-line control structure must be on the line after the opening parenthesis';
102102
$fix = $phpcsFile->addFixableError($error, $next, 'FirstExpressionLine');
103103
if ($fix === true) {
104+
$phpcsFile->fixer->beginChangeset();
105+
if ($tokens[$next]['line'] > ($tokens[$parenOpener]['line'] + 1)) {
106+
for ($i = ($parenOpener + 1); $i < $next; $i++) {
107+
if ($tokens[$next]['line'] === $tokens[$i]['line']) {
108+
break;
109+
}
110+
111+
$phpcsFile->fixer->replaceToken($i, '');
112+
}
113+
}
114+
104115
$phpcsFile->fixer->addNewline($parenOpener);
116+
$phpcsFile->fixer->endChangeset();
105117
}
106118
}
107119

src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc

+33
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,36 @@ $expr2 &&
9898
$expr3) {
9999
// structure body
100100
};
101+
102+
// Ensure the sniff handles too many newlines (not just too few).
103+
for (
104+
105+
106+
$i = 0;
107+
$i < 10;
108+
$i++
109+
110+
111+
) {}
112+
113+
// Ensure the sniff does not remove indentation whitespace when comments are involved.
114+
for (
115+
116+
117+
// comment.
118+
$i = 0;
119+
$i < 10;
120+
$i++
121+
) {}
122+
123+
// The sniff treats a comment (ie non-whitespace) as content, but only at the
124+
// start / end of the control structure. So the inner-whitespace here is
125+
// intentionally ignored by this sniff. Additionally, the comment is not indented
126+
// by this sniff when fixing.
127+
for (// comment.
128+
129+
130+
$i = 0;
131+
$i < 10;
132+
$i++
133+
) {}

src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed

+28
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,31 @@ match (
101101
) {
102102
// structure body
103103
};
104+
105+
// Ensure the sniff handles too many newlines (not just too few).
106+
for (
107+
$i = 0;
108+
$i < 10;
109+
$i++
110+
) {}
111+
112+
// Ensure the sniff does not remove indentation whitespace when comments are involved.
113+
for (
114+
// comment.
115+
$i = 0;
116+
$i < 10;
117+
$i++
118+
) {}
119+
120+
// The sniff treats a comment (ie non-whitespace) as content, but only at the
121+
// start / end of the control structure. So the inner-whitespace here is
122+
// intentionally ignored by this sniff. Additionally, the comment is not indented
123+
// by this sniff when fixing.
124+
for (
125+
// comment.
126+
127+
128+
$i = 0;
129+
$i < 10;
130+
$i++
131+
) {}

src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.php

+23-19
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,29 @@ final class ControlStructureSpacingUnitTest extends AbstractSniffUnitTest
3131
public function getErrorList()
3232
{
3333
return [
34-
2 => 2,
35-
16 => 1,
36-
17 => 1,
37-
18 => 1,
38-
22 => 1,
39-
23 => 1,
40-
32 => 1,
41-
33 => 1,
42-
34 => 1,
43-
37 => 1,
44-
38 => 1,
45-
39 => 1,
46-
48 => 2,
47-
58 => 1,
48-
59 => 1,
49-
92 => 1,
50-
96 => 1,
51-
97 => 1,
52-
98 => 2,
34+
2 => 2,
35+
16 => 1,
36+
17 => 1,
37+
18 => 1,
38+
22 => 1,
39+
23 => 1,
40+
32 => 1,
41+
33 => 1,
42+
34 => 1,
43+
37 => 1,
44+
38 => 1,
45+
39 => 1,
46+
48 => 2,
47+
58 => 1,
48+
59 => 1,
49+
92 => 1,
50+
96 => 1,
51+
97 => 1,
52+
98 => 2,
53+
106 => 1,
54+
111 => 1,
55+
117 => 1,
56+
127 => 1,
5357
];
5458

5559
}//end getErrorList()

0 commit comments

Comments
 (0)