Skip to content

Commit 0ca8e98

Browse files
committed
Allow comment before switch and case statements.
1 parent 778d24a commit 0ca8e98

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

WPForms/Sniffs/Formatting/SwitchSniff.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function process( File $phpcsFile, $stackPtr ) {
6868
private function processSwitch( File $phpcsFile, $stackPtr ) {
6969

7070
$tokens = $phpcsFile->getTokens();
71-
$previous = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true );
71+
$previous = $phpcsFile->findPrevious( [ T_WHITESPACE, T_COMMENT ], $stackPtr - 1, null, true );
7272

73-
if ( $previous === false ) {
73+
if ( $previous === false || $tokens[ $previous ]['code'] === T_COMMENT ) {
7474
return;
7575
}
7676

@@ -108,11 +108,16 @@ private function processSwitch( File $phpcsFile, $stackPtr ) {
108108
private function processCase( File $phpcsFile, $stackPtr ) {
109109

110110
$tokens = $phpcsFile->getTokens();
111-
$previous = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true );
111+
$previous = $phpcsFile->findPrevious( [ T_WHITESPACE, T_COMMENT ], $stackPtr - 1, null, true );
112112
$previousStatement = $phpcsFile->findFirstOnLine( [ T_SWITCH, T_CASE, T_DEFAULT, T_BREAK ], $previous );
113113

114+
if ( $previousStatement === false ) {
115+
$this->addEmptyLineError( $phpcsFile, $stackPtr );
116+
117+
return;
118+
}
119+
114120
if (
115-
! empty( $previousStatement ) &&
116121
$tokens[ $previousStatement ]['code'] === T_SWITCH &&
117122
$tokens[ $previousStatement ]['line'] !== $tokens[ $stackPtr ]['line'] - 1
118123
) {
@@ -122,11 +127,8 @@ private function processCase( File $phpcsFile, $stackPtr ) {
122127
}
123128

124129
if (
125-
empty( $previousStatement ) ||
126-
(
127-
$tokens[ $previousStatement ]['code'] === T_BREAK &&
128-
$tokens[ $stackPtr ]['line'] - $tokens[ $previousStatement ]['line'] === 1
129-
)
130+
$tokens[ $previousStatement ]['code'] === T_BREAK &&
131+
$tokens[ $stackPtr ]['line'] - $tokens[ $previousStatement ]['line'] === 1
130132
) {
131133
$this->addEmptyLineError( $phpcsFile, $stackPtr );
132134

WPForms/Tests/TestFiles/Formatting/Switch.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ public function example( $args ) {
66

77
$this->get_value();
88

9+
// Switch comment.
910
switch ( $args ) {
1011
case 'a':
1112
example( $args );
1213
break;
1314

15+
// Case comment.
1416
case 'b':
1517
case 'c':
1618
$value = 2;
@@ -19,6 +21,7 @@ public function example( $args ) {
1921
case 'd':
2022
break;
2123

24+
// Default comment.
2225
default:
2326
$value = 3;
2427
}

WPForms/Tests/Tests/Formatting/SwitchTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testProcess() {
2121

2222
$phpcsFile = $this->process( new SwitchSniff() );
2323

24-
$this->fileHasErrors( $phpcsFile, 'AddEmptyLineBefore', [ 60, 66, 86, 91, 94 ] );
25-
$this->fileHasErrors( $phpcsFile, 'RemoveEmptyLineBefore', [ 62, 65, 68, 71, 76, 85, 90 ] );
24+
$this->fileHasErrors( $phpcsFile, 'AddEmptyLineBefore', [ 63, 69, 89, 94, 97 ] );
25+
$this->fileHasErrors( $phpcsFile, 'RemoveEmptyLineBefore', [ 65, 68, 71, 74, 79, 88, 93 ] );
2626
}
2727
}

0 commit comments

Comments
 (0)