Skip to content

Commit 778d24a

Browse files
authored
Merge pull request #21 from awesomemotive/issue/2-no-empty-line-before-break
Remove requirement for a new line before break statement inside the switch.
2 parents 13eb7f0 + a14b146 commit 778d24a

File tree

6 files changed

+73
-11
lines changed

6 files changed

+73
-11
lines changed

WPForms/Sniffs/BaseSniff.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,30 @@ protected function getFirstArgument( $phpcsFile, $stackPtr ) {
151151
);
152152
}
153153

154+
/**
155+
* Check whether the token is break in switch statement.
156+
*
157+
* @since 1.0.4
158+
*
159+
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
160+
* @param array $token First token on the next line.
161+
*
162+
* @return bool
163+
*/
164+
protected function isBreakInSwitch( $phpcsFile, $token ) {
165+
166+
$tokens = $phpcsFile->getTokens();
167+
168+
if ( $token['code'] !== T_BREAK ) {
169+
return false;
170+
}
171+
172+
return (
173+
isset( $token['scope_condition'] ) &&
174+
in_array( $tokens[ $token['scope_condition'] ]['code'], [ T_CASE, T_DEFAULT ], true )
175+
);
176+
}
177+
154178
/**
155179
* Get fully qualified class name.
156180
*

WPForms/Sniffs/Formatting/EmptyLineAfterAssigmentVariablesSniff.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public function process( File $phpcsFile, $stackPtr ) {
6262
return;
6363
}
6464

65-
if ( in_array( $nextLineTokens[0]['code'], $this->getAllowedTokensAfterAssigment(), true ) ) {
65+
if (
66+
$this->isBreakInSwitch( $phpcsFile, $nextLineTokens[0] ) ||
67+
in_array( $nextLineTokens[0]['code'], $this->getAllowedTokensAfterAssigment(), true )
68+
) {
6669
return;
6770
}
6871

WPForms/Sniffs/Formatting/SwitchSniff.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function process( File $phpcsFile, $stackPtr ) {
5252
$this->processCase( $phpcsFile, $stackPtr );
5353
}
5454

55-
if ( $tokens[ $stackPtr ]['code'] === T_BREAK ) {
55+
if ( $this->isBreakInSwitch( $phpcsFile, $tokens[ $stackPtr ] ) ) {
5656
$this->processBreak( $phpcsFile, $stackPtr );
5757
}
5858
}
@@ -70,6 +70,10 @@ private function processSwitch( File $phpcsFile, $stackPtr ) {
7070
$tokens = $phpcsFile->getTokens();
7171
$previous = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true );
7272

73+
if ( $previous === false ) {
74+
return;
75+
}
76+
7377
if ( $tokens[ $stackPtr ]['line'] - $tokens[ $previous ]['line'] === 1 ) {
7478
$this->addEmptyLineError( $phpcsFile, $stackPtr );
7579
}
@@ -82,7 +86,7 @@ private function processSwitch( File $phpcsFile, $stackPtr ) {
8286

8387
$next = $phpcsFile->findNext( T_WHITESPACE, $tokens[ $stackPtr ]['scope_closer'] + 1, null, true );
8488

85-
if ( $tokens[ $next ]['code'] === T_CLOSE_CURLY_BRACKET ) {
89+
if ( $next === false || ( $tokens[ $next ]['code'] === T_CLOSE_CURLY_BRACKET ) ) {
8690
return;
8791
}
8892

@@ -156,8 +160,8 @@ private function processBreak( File $phpcsFile, $stackPtr ) {
156160

157161
$previousStatement = $phpcsFile->findFirstOnLine( [ T_CASE, T_DEFAULT ], $previous );
158162

159-
if ( empty( $previousStatement ) && $tokens[ $stackPtr ]['line'] - $tokens[ $previous ]['line'] !== 2 ) {
160-
$this->addEmptyLineError( $phpcsFile, $stackPtr );
163+
if ( empty( $previousStatement ) && $tokens[ $stackPtr ]['line'] - $tokens[ $previous ]['line'] !== 1 ) {
164+
$this->removeEmptyLineError( $phpcsFile, $stackPtr );
161165
}
162166
}
163167

WPForms/Tests/TestFiles/Formatting/EmptyLineAfterAssigmentVariables.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,34 @@ function invalid2() {
137137
unset( $types[ $key ] );
138138
}
139139
}
140+
141+
// Good example.
142+
switch ( $foo ) {
143+
case 'foo':
144+
$bar = 1;
145+
break;
146+
147+
case 'bar':
148+
echo 1;
149+
break;
150+
151+
case 'baz':
152+
$bar = 3;
153+
break;
154+
155+
case 'krya':
156+
default:
157+
$bar = 4;
158+
break;
159+
}
160+
161+
// Good example.
162+
foreach ( $rays as $ray ) {
163+
if ( $ray === 1 ) {
164+
$a = 2;
165+
166+
break;
167+
}
168+
169+
$c = 3;
170+
}

WPForms/Tests/TestFiles/Formatting/Switch.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ public function example( $args ) {
99
switch ( $args ) {
1010
case 'a':
1111
example( $args );
12-
1312
break;
1413

1514
case 'b':
1615
case 'c':
1716
$value = 2;
18-
1917
break;
2018

2119
case 'd':
@@ -31,13 +29,11 @@ public function example_2( $args ) {
3129
switch ( $args ) {
3230
case 'a':
3331
example( $args );
34-
3532
break;
3633

3734
case 'b':
3835
case 'c':
3936
$value = 2;
40-
4137
break;
4238

4339
default:
@@ -65,11 +61,13 @@ public function example( $args ) {
6561

6662
case 'a':
6763
example( $args );
64+
6865
break;
6966
case 'b':
7067

7168
case 'c':
7269
$value = 2;
70+
7371
break;
7472

7573
default:
@@ -83,10 +81,12 @@ public function example_2( $args ) {
8381
switch ( $args ) {
8482
case 'a':
8583
example( $args );
84+
8685
break;
8786
case 'b':
8887
case 'c':
8988
$value = 2;
89+
9090
break;
9191
default:
9292
$value = 3;

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', [ 64, 68, 69, 73, 86, 87, 90, 91, 94 ] );
25-
$this->fileHasErrors( $phpcsFile, 'RemoveEmptyLineBefore', [ 66, 71, 78 ] );
24+
$this->fileHasErrors( $phpcsFile, 'AddEmptyLineBefore', [ 60, 66, 86, 91, 94 ] );
25+
$this->fileHasErrors( $phpcsFile, 'RemoveEmptyLineBefore', [ 62, 65, 68, 71, 76, 85, 90 ] );
2626
}
2727
}

0 commit comments

Comments
 (0)