Skip to content

Commit e67fe56

Browse files
committed
Fix conflict within PSR12.ControlStructures.ControlStructureSpacing
For multi-line control structures, the first line of code must be on the next line after the control structure. This sniff correctly identified such cases. When the first line of code was on the same line as the control structure, the sniff correctly fixed this by adding a newline between these. However, when there were multiple blank lines between these, the fixer would continue adding new newlines. This change fixes this bug by first removing all white-space before adding the one expected newline. Includes test.
1 parent a3d11a9 commit e67fe56

File tree

4 files changed

+67
-19
lines changed

4 files changed

+67
-19
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,15 @@ 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+
$phpcsFile->fixer->replaceToken($i, '');
108+
}
109+
}
110+
104111
$phpcsFile->fixer->addNewline($parenOpener);
112+
$phpcsFile->fixer->endChangeset();
105113
}
106114
}
107115

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,24 @@ $expr2 &&
9898
$expr3) {
9999
// structure body
100100
};
101+
102+
// Ensure the sniff handles too many newlines (not just too few). This was copied from src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.1.inc
103+
for (
104+
105+
106+
$i = 0
107+
108+
109+
;
110+
111+
112+
$i < 10
113+
114+
115+
;
116+
117+
118+
$i++
119+
120+
121+
) {}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,20 @@ match (
101101
) {
102102
// structure body
103103
};
104+
105+
// Ensure the sniff handles too many newlines (not just too few). This was copied from src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.1.inc
106+
for (
107+
$i = 0
108+
109+
110+
;
111+
112+
113+
$i < 10
114+
115+
116+
;
117+
118+
119+
$i++
120+
) {}

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,27 @@ 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+
121 => 1,
5355
];
5456

5557
}//end getErrorList()

0 commit comments

Comments
 (0)