Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR2/ClassDeclaration: bug fix - prevent fixer conflict #431

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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