Skip to content

Commit 4baa479

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 whitespace before adding the one expected newline. Includes test.
1 parent a3d11a9 commit 4baa479

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ 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+
}
104110
$phpcsFile->fixer->addNewline($parenOpener);
111+
$phpcsFile->fixer->endChangeset();
105112
}
106113
}
107114

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public function getErrorList()
5050
96 => 1,
5151
97 => 1,
5252
98 => 2,
53+
106 => 1,
54+
121 => 1,
5355
];
5456

5557
}//end getErrorList()

0 commit comments

Comments
 (0)