Skip to content

Commit 44c338e

Browse files
authored
Merge pull request #1358 from bigdevlarry/PSR2-WrongOpenercase-with-colon-and-bracket-is-unclear
PSR2 WrongOpenercase with colon and bracket is unclear
2 parents cb3ecb9 + 7c06a1d commit 44c338e

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,12 @@ public function process(File $phpcsFile, int $stackPtr)
197197
}
198198
}
199199
} else {
200-
// Probably a case/default statement with colon + curly braces.
201-
$phpcsFile->addError($error, $nextCase, 'WrongOpener' . $type);
200+
if ($tokens[$opener]['code'] === T_OPEN_CURLY_BRACKET) {
201+
$error = '%s statements must not use a braced block after the colon';
202+
$phpcsFile->addError($error, $nextCase, 'WrongOpener', [strtoupper($type)]);
203+
} else {
204+
$phpcsFile->addError($error, $nextCase, 'WrongOpener' . $type);
205+
}
202206
}
203207
}
204208

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,12 @@ switch ($foo):
638638
<div>Some html</div>
639639
<?php break;
640640
endswitch;
641+
642+
// Verify that the sniff gracefully handles braced block with trailing comment.
643+
switch ($foo) {
644+
case 'Foo': // Trailing comment.
645+
{
646+
echo 'foo';
647+
break;
648+
}
649+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,12 @@ switch ($foo):
641641
<?php
642642
break;
643643
endswitch;
644+
645+
// Verify that the sniff gracefully handles braced block with trailing comment.
646+
switch ($foo) {
647+
case 'Foo': // Trailing comment.
648+
{
649+
echo 'foo';
650+
break;
651+
}
652+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public function getErrorList()
7373
631 => 1,
7474
634 => 1,
7575
637 => 1,
76+
644 => 1,
7677
];
7778
}
7879

0 commit comments

Comments
 (0)