diff --git a/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php index d356e76ef2..3325459ae2 100644 --- a/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php @@ -349,7 +349,10 @@ public function processOpen(File $phpcsFile, $stackPtr) $phpcsFile->fixer->addNewline($prev); $phpcsFile->fixer->endChangeset(); } - } else if ($tokens[$prev]['line'] !== ($tokens[$className]['line'] - 1)) { + } else if ((isset(Tokens::$commentTokens[$tokens[$prev]['code']]) === false + && $tokens[$prev]['line'] !== ($tokens[$className]['line'] - 1)) + || $tokens[$prev]['line'] === $tokens[$className]['line'] + ) { if ($keywordTokenType === T_EXTENDS) { $error = 'Only one interface may be specified per line in a multi-line extends declaration'; $fix = $phpcsFile->addFixableError($error, $className, 'ExtendsInterfaceSameLine'); diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc index 962f182dfd..144eef8fe3 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc @@ -293,3 +293,26 @@ class BarFoo implements namespace\BarFoo { } + +// Safeguard that the sniff ignores comments between interface names in a multiline implements. +class ClassWithMultiLineImplementsAndIgnoreAnnotation implements + SomeInterface, + // phpcs:disable Stnd.Cat.Sniff -- For reasons. + + \AnotherInterface +{ +} + +class ClassWithMultiLineImplementsAndComment implements + SomeInterface, + // Comment. + +AnotherInterface +{ +} + +class ClassWithMultiLineImplementsAndCommentOnSameLineAsInterfaceName implements + SomeInterface, + /* Comment. */ AnotherInterface +{ +} diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed index 8725c4fac7..84123ca500 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed @@ -281,3 +281,27 @@ class BarFoo implements namespace\BarFoo { } + +// Safeguard that the sniff ignores comments between interface names in a multiline implements. +class ClassWithMultiLineImplementsAndIgnoreAnnotation implements + SomeInterface, + // phpcs:disable Stnd.Cat.Sniff -- For reasons. + + \AnotherInterface +{ +} + +class ClassWithMultiLineImplementsAndComment implements + SomeInterface, + // Comment. + + AnotherInterface +{ +} + +class ClassWithMultiLineImplementsAndCommentOnSameLineAsInterfaceName implements + SomeInterface, + /* Comment. */ + AnotherInterface +{ +} diff --git a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php index b74d811c17..ef97be4601 100644 --- a/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php @@ -76,6 +76,8 @@ public function getErrorList() 273 => 1, 276 => 1, 282 => 1, + 310 => 1, + 316 => 1, ]; }//end getErrorList()