Skip to content

Commit

Permalink
Merge pull request #431 from PHPCSStandards/feature/psr2-classdeclara…
Browse files Browse the repository at this point in the history
…tions-fix-fixer-conflict

PSR2/ClassDeclaration: bug fix - prevent fixer conflict
  • Loading branch information
jrfnl authored Apr 6, 2024
2 parents b131294 + 0b4f3e0 commit 23c79fc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
23 changes: 23 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
2 changes: 2 additions & 0 deletions src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public function getErrorList()
273 => 1,
276 => 1,
282 => 1,
310 => 1,
316 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 23c79fc

Please sign in to comment.