Skip to content

Commit 21c0e80

Browse files
committed
PSR2/SwitchDeclaration: make implied semicolon via close tag auto-fixable
The `PSR2.ControlStructures.SwitchDeclaration` sniff includes a rule/error flagging when the scope opener is not a `:` colon. This error was previously already made auto-fixable when the `scope_opener` is a semi-colon (via PR 1161). This commit now also makes that error auto-fixable if the `scope_opener` is a PHP close tag. Includes tests. These tests also document how the sniff otherwise behaves when the code within a `switch` moves in and out of PHP a lot.
1 parent 6327f77 commit 21c0e80

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,15 @@ public function process(File $phpcsFile, int $stackPtr)
186186
}
187187
} else {
188188
$error = strtoupper($type) . ' statements must be defined using a colon';
189-
if ($tokens[$opener]['code'] === T_SEMICOLON) {
189+
if ($tokens[$opener]['code'] === T_SEMICOLON || $tokens[$opener]['code'] === T_CLOSE_TAG) {
190190
$fix = $phpcsFile->addFixableError($error, $nextCase, 'WrongOpener' . $type);
191191
if ($fix === true) {
192-
$phpcsFile->fixer->replaceToken($opener, ':');
192+
if ($tokens[$opener]['code'] === T_SEMICOLON) {
193+
$phpcsFile->fixer->replaceToken($opener, ':');
194+
} else {
195+
$prevNonEmpty = $phpcsFile->findPrevious(T_WHITESPACE, ($opener - 1), null, true);
196+
$phpcsFile->fixer->addContent($prevNonEmpty, ':');
197+
}
193198
}
194199
} else {
195200
// Probably a case/default statement with colon + curly braces.

src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,19 @@ switch ($value) {
622622
default ;
623623
echo 'Other';
624624
}
625+
626+
// Verify that case/default statements with a PHP close tag as scope opener are handled correctly by the sniff.
627+
switch ($foo):
628+
case "foo"?>
629+
<div>Some html</div>
630+
<?php break;
631+
case "bar" ; ?>
632+
<div>Some html</div>
633+
<?php break;
634+
default ?>
635+
<div>Some html</div>
636+
<?php break;
637+
default ; ?>
638+
<div>Some html</div>
639+
<?php break;
640+
endswitch;

src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,27 @@ switch ($value) {
617617
default:
618618
echo 'Other';
619619
}
620+
621+
// Verify that case/default statements with a PHP close tag as scope opener are handled correctly by the sniff.
622+
switch ($foo):
623+
case "foo":
624+
?>
625+
<div>Some html</div>
626+
<?php
627+
break;
628+
case "bar":
629+
?>
630+
<div>Some html</div>
631+
<?php
632+
break;
633+
default:
634+
?>
635+
<div>Some html</div>
636+
<?php
637+
break;
638+
default:
639+
?>
640+
<div>Some html</div>
641+
<?php
642+
break;
643+
endswitch;

src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function getErrorList()
6969
617 => 1,
7070
620 => 1,
7171
622 => 1,
72+
628 => 1,
73+
631 => 1,
74+
634 => 1,
75+
637 => 1,
7276
];
7377
}
7478

0 commit comments

Comments
 (0)