Skip to content

Commit d2fd0fd

Browse files
committed
Introduce lineDistance method.
1 parent 0ca8e98 commit d2fd0fd

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

WPForms/Sniffs/Formatting/SwitchSniff.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private function processCase( File $phpcsFile, $stackPtr ) {
119119

120120
if (
121121
$tokens[ $previousStatement ]['code'] === T_SWITCH &&
122-
$tokens[ $previousStatement ]['line'] !== $tokens[ $stackPtr ]['line'] - 1
122+
$this->lineDistance( $phpcsFile, $stackPtr, $previousStatement ) !== 1
123123
) {
124124
$this->removeEmptyLineError( $phpcsFile, $stackPtr );
125125

@@ -128,21 +128,39 @@ private function processCase( File $phpcsFile, $stackPtr ) {
128128

129129
if (
130130
$tokens[ $previousStatement ]['code'] === T_BREAK &&
131-
$tokens[ $stackPtr ]['line'] - $tokens[ $previousStatement ]['line'] === 1
131+
$this->lineDistance( $phpcsFile, $stackPtr, $previousStatement ) === 1
132132
) {
133133
$this->addEmptyLineError( $phpcsFile, $stackPtr );
134134

135135
return;
136136
}
137137

138138
if (
139-
$tokens[ $stackPtr ]['line'] - $tokens[ $previousStatement ]['line'] !== 1 &&
139+
$this->lineDistance( $phpcsFile, $stackPtr, $previousStatement ) !== 1 &&
140140
in_array( $tokens[ $previousStatement ]['code'], [ T_CASE, T_DEFAULT ], true )
141141
) {
142142
$this->removeEmptyLineError( $phpcsFile, $stackPtr );
143143
}
144144
}
145145

146+
/**
147+
* Get distance in lines between tokens, ignoring comment lines.
148+
*
149+
* @since 1.0.4
150+
*
151+
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
152+
* @param int $endPtr End token.
153+
* @param int $startPtr Start token.
154+
*
155+
* @return mixed
156+
*/
157+
private function lineDistance( File $phpcsFile, $endPtr, $startPtr ) {
158+
159+
$tokens = $phpcsFile->getTokens();
160+
161+
return $tokens[ $endPtr ]['line'] - $tokens[ $startPtr ]['line'];
162+
}
163+
146164
/**
147165
* Process the break element.
148166
*

0 commit comments

Comments
 (0)