From 99b0339bce7eaae8b5285398d5f1492da734d1ac Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Wed, 4 Nov 2020 09:22:47 +1100 Subject: [PATCH] Fixed bug #3157 : PSR2.ControlStructures.SwitchDeclaration.BreakIndent false positive when case keyword is not indented --- package.xml | 1 + .../ControlStructures/SwitchDeclarationSniff.php | 2 +- .../ControlStructures/SwitchDeclarationUnitTest.inc | 10 ++++++++++ .../SwitchDeclarationUnitTest.inc.fixed | 10 ++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/package.xml b/package.xml index 7d0cf64e29..8efb23c5b6 100644 --- a/package.xml +++ b/package.xml @@ -61,6 +61,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Fixed an issue that could occurr when checking files on network drives, such as with WSL2 on Windows 10 -- This works around a long-standing PHP bug with is_readable() -- Thanks to Michael S for the patch + - Fixed bug #3157 : PSR2.ControlStructures.SwitchDeclaration.BreakIndent false positive when case keyword is not indented diff --git a/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php b/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php index c38a10d2d3..0db0cd98b8 100644 --- a/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/ControlStructures/SwitchDeclarationSniff.php @@ -152,7 +152,7 @@ public function process(File $phpcsFile, $stackPtr) $phpcsFile->fixer->replaceToken($nextCloser, trim($tokens[$nextCloser]['content'])); } } else { - $diff = ($caseAlignment + $this->indent - $tokens[$nextCloser]['column']); + $diff = ($tokens[$nextCase]['column'] + $this->indent - $tokens[$nextCloser]['column']); if ($diff !== 0) { $error = 'Terminating statement must be indented to the same level as the CASE body'; $fix = $phpcsFile->addFixableError($error, $nextCloser, 'BreakIndent'); diff --git a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc index af605cd71d..572e6ada05 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc +++ b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc @@ -271,3 +271,13 @@ $foo = $foo ? } } : null; + +switch ($foo) { +case Foo::INTERFACE: + echo '1'; + return self::INTERFACE; +case Foo::TRAIT: +case Foo::ARRAY: + echo '1'; + return self::VALUE; +} diff --git a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed index d5671feea3..e454cd3318 100644 --- a/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/ControlStructures/SwitchDeclarationUnitTest.inc.fixed @@ -274,3 +274,13 @@ $foo = $foo ? } } : null; + +switch ($foo) { +case Foo::INTERFACE: + echo '1'; + return self::INTERFACE; +case Foo::TRAIT: +case Foo::ARRAY: + echo '1'; + return self::VALUE; +}