Skip to content

Commit

Permalink
Merge pull request #581 from fredden/fixer-conflict/PSR12.ControlStru…
Browse files Browse the repository at this point in the history
…ctures.ControlStructureSpacing

Fix conflict within PSR12.ControlStructures.ControlStructureSpacing
  • Loading branch information
jrfnl authored Sep 2, 2024
2 parents 41a426c + d9e164d commit f905241
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,19 @@ public function process(File $phpcsFile, $stackPtr)
$error = 'The first expression of a multi-line control structure must be on the line after the opening parenthesis';
$fix = $phpcsFile->addFixableError($error, $next, 'FirstExpressionLine');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
if ($tokens[$next]['line'] > ($tokens[$parenOpener]['line'] + 1)) {
for ($i = ($parenOpener + 1); $i < $next; $i++) {
if ($tokens[$next]['line'] === $tokens[$i]['line']) {
break;
}

$phpcsFile->fixer->replaceToken($i, '');
}
}

$phpcsFile->fixer->addNewline($parenOpener);
$phpcsFile->fixer->endChangeset();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,36 @@ $expr2 &&
$expr3) {
// structure body
};

// Ensure the sniff handles too many newlines (not just too few).
for (


$i = 0;
$i < 10;
$i++


) {}

// Ensure the sniff does not remove indentation whitespace when comments are involved.
for (


// comment.
$i = 0;
$i < 10;
$i++
) {}

// The sniff treats a comment (ie non-whitespace) as content, but only at the
// start / end of the control structure. So the inner-whitespace here is
// intentionally ignored by this sniff. Additionally, the comment is not indented
// by this sniff when fixing.
for (// comment.


$i = 0;
$i < 10;
$i++
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,31 @@ match (
) {
// structure body
};

// Ensure the sniff handles too many newlines (not just too few).
for (
$i = 0;
$i < 10;
$i++
) {}

// Ensure the sniff does not remove indentation whitespace when comments are involved.
for (
// comment.
$i = 0;
$i < 10;
$i++
) {}

// The sniff treats a comment (ie non-whitespace) as content, but only at the
// start / end of the control structure. So the inner-whitespace here is
// intentionally ignored by this sniff. Additionally, the comment is not indented
// by this sniff when fixing.
for (
// comment.


$i = 0;
$i < 10;
$i++
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,29 @@ final class ControlStructureSpacingUnitTest extends AbstractSniffUnitTest
public function getErrorList()
{
return [
2 => 2,
16 => 1,
17 => 1,
18 => 1,
22 => 1,
23 => 1,
32 => 1,
33 => 1,
34 => 1,
37 => 1,
38 => 1,
39 => 1,
48 => 2,
58 => 1,
59 => 1,
92 => 1,
96 => 1,
97 => 1,
98 => 2,
2 => 2,
16 => 1,
17 => 1,
18 => 1,
22 => 1,
23 => 1,
32 => 1,
33 => 1,
34 => 1,
37 => 1,
38 => 1,
39 => 1,
48 => 2,
58 => 1,
59 => 1,
92 => 1,
96 => 1,
97 => 1,
98 => 2,
106 => 1,
111 => 1,
117 => 1,
127 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit f905241

Please sign in to comment.