diff --git a/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php b/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php index c846e43e8a..0c8355d2e2 100644 --- a/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php @@ -172,7 +172,15 @@ public function process(File $phpcsFile, $stackPtr) }//end if } else { $error = strtoupper($type).' statements must be defined using a colon'; - $phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type); + if ($tokens[$opener]['code'] === T_SEMICOLON) { + $fix = $phpcsFile->addFixableError($error, $nextCase, 'WrongOpener'.$type); + if ($fix === true) { + $phpcsFile->fixer->replaceToken($opener, ':'); + } + } else { + // Probably a case/default statement with colon + curly braces. + $phpcsFile->addError($error, $nextCase, 'WrongOpener'.$type); + } }//end if // We only want cases from here on in. diff --git a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc index 440cf8a38a..7db00c23f9 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc +++ b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc @@ -609,3 +609,16 @@ switch ( $a ) { jumpOut: doSomething(); + +// Fixable semicolon as case/default scope opener. +switch ($value) { + case 'foo'; + case 'bar' /*comment*/ ; + case 'baz' ; + echo 'foo, bar, or baz'; + break; + default; + echo 'Other'; + default ; + echo 'Other'; +} diff --git a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed index ca39c76d02..c84f389ddc 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed @@ -604,3 +604,16 @@ switch ( $a ) { jumpOut: doSomething(); + +// Fixable semicolon as case/default scope opener. +switch ($value) { + case 'foo': + case 'bar' /*comment*/: + case 'baz': + echo 'foo, bar, or baz'; + break; + default: + echo 'Other'; + default: + echo 'Other'; +} diff --git a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.php b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.php index a292503dcc..187d9f48ce 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.php @@ -63,6 +63,11 @@ public function getErrorList() 541 => 1, 558 => 1, 575 => 1, + 615 => 1, + 616 => 1, + 617 => 1, + 620 => 1, + 622 => 1, ]; }//end getErrorList()