Skip to content

Commit 99b0339

Browse files
committed
Fixed bug #3157 : PSR2.ControlStructures.SwitchDeclaration.BreakIndent false positive when case keyword is not indented
1 parent 75a0512 commit 99b0339

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
6161
- Fixed an issue that could occurr when checking files on network drives, such as with WSL2 on Windows 10
6262
-- This works around a long-standing PHP bug with is_readable()
6363
-- Thanks to Michael S for the patch
64+
- Fixed bug #3157 : PSR2.ControlStructures.SwitchDeclaration.BreakIndent false positive when case keyword is not indented
6465
</notes>
6566
<contents>
6667
<dir name="/">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function process(File $phpcsFile, $stackPtr)
152152
$phpcsFile->fixer->replaceToken($nextCloser, trim($tokens[$nextCloser]['content']));
153153
}
154154
} else {
155-
$diff = ($caseAlignment + $this->indent - $tokens[$nextCloser]['column']);
155+
$diff = ($tokens[$nextCase]['column'] + $this->indent - $tokens[$nextCloser]['column']);
156156
if ($diff !== 0) {
157157
$error = 'Terminating statement must be indented to the same level as the CASE body';
158158
$fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent');

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,13 @@ $foo = $foo ?
271271
}
272272
} :
273273
null;
274+
275+
switch ($foo) {
276+
case Foo::INTERFACE:
277+
echo '1';
278+
return self::INTERFACE;
279+
case Foo::TRAIT:
280+
case Foo::ARRAY:
281+
echo '1';
282+
return self::VALUE;
283+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,13 @@ $foo = $foo ?
274274
}
275275
} :
276276
null;
277+
278+
switch ($foo) {
279+
case Foo::INTERFACE:
280+
echo '1';
281+
return self::INTERFACE;
282+
case Foo::TRAIT:
283+
case Foo::ARRAY:
284+
echo '1';
285+
return self::VALUE;
286+
}

0 commit comments

Comments
 (0)