Skip to content

Commit bfc7766

Browse files
committed
Use lineDistance method to exclude comment lines from consideration.
1 parent d2fd0fd commit bfc7766

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

WPForms/Sniffs/Formatting/SwitchSniff.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ private function processSwitch( File $phpcsFile, $stackPtr ) {
7474
return;
7575
}
7676

77-
if ( $tokens[ $stackPtr ]['line'] - $tokens[ $previous ]['line'] === 1 ) {
77+
if ( $this->lineDistance( $phpcsFile, $stackPtr, $previous ) === 1 ) {
7878
$this->addEmptyLineError( $phpcsFile, $stackPtr );
7979
}
8080

8181
$beforeClose = $phpcsFile->findPrevious( T_WHITESPACE, $tokens[ $stackPtr ]['scope_closer'] - 1, null, true );
8282

83-
if ( $tokens[ $tokens[ $stackPtr ]['scope_closer'] ]['line'] - $tokens[ $beforeClose ]['line'] !== 1 ) {
83+
if ( $this->lineDistance( $phpcsFile, $tokens[ $stackPtr ]['scope_closer'], $beforeClose ) !== 1 ) {
8484
$this->removeEmptyLineError( $phpcsFile, $tokens[ $stackPtr ]['scope_closer'] );
8585
}
8686

@@ -90,7 +90,7 @@ private function processSwitch( File $phpcsFile, $stackPtr ) {
9090
return;
9191
}
9292

93-
if ( $tokens[ $next ]['line'] - $tokens[ $tokens[ $stackPtr ]['scope_closer'] ]['line'] > 1 ) {
93+
if ( $this->lineDistance( $phpcsFile, $next, $tokens[ $stackPtr ]['scope_closer'] ) > 1 ) {
9494
return;
9595
}
9696

@@ -158,7 +158,25 @@ private function lineDistance( File $phpcsFile, $endPtr, $startPtr ) {
158158

159159
$tokens = $phpcsFile->getTokens();
160160

161-
return $tokens[ $endPtr ]['line'] - $tokens[ $startPtr ]['line'];
161+
$ptr = $startPtr + 1;
162+
163+
$commentLines = [];
164+
165+
while ( $ptr < $endPtr ) {
166+
if (
167+
$tokens[ $ptr ]['code'] === T_COMMENT &&
168+
$phpcsFile->findFirstOnLine( [ T_WHITESPACE, T_COMMENT ], $ptr, true ) === false
169+
) {
170+
$commentLines[] = $tokens[ $ptr ]['line'];
171+
}
172+
173+
$ptr ++;
174+
}
175+
176+
return max(
177+
0,
178+
$tokens[ $endPtr ]['line'] - $tokens[ $startPtr ]['line'] - count( array_unique( $commentLines ) )
179+
);
162180
}
163181

164182
/**

WPForms/Tests/TestFiles/Formatting/Switch.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public function example( $args ) {
88

99
// Switch comment.
1010
switch ( $args ) {
11+
// Case comment.
1112
case 'a':
1213
example( $args );
1314
break;

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', [ 63, 69, 89, 94, 97 ] );
25-
$this->fileHasErrors( $phpcsFile, 'RemoveEmptyLineBefore', [ 65, 68, 71, 74, 79, 88, 93 ] );
24+
$this->fileHasErrors( $phpcsFile, 'AddEmptyLineBefore', [ 64, 70, 90, 95, 98 ] );
25+
$this->fileHasErrors( $phpcsFile, 'RemoveEmptyLineBefore', [ 66, 69, 72, 75, 80, 89, 94 ] );
2626
}
2727
}

0 commit comments

Comments
 (0)