Skip to content

Commit 23c79fc

Browse files
authored
Merge pull request #431 from PHPCSStandards/feature/psr2-classdeclarations-fix-fixer-conflict
PSR2/ClassDeclaration: bug fix - prevent fixer conflict
2 parents b131294 + 0b4f3e0 commit 23c79fc

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/Standards/PSR2/Sniffs/Classes/ClassDeclarationSniff.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,10 @@ public function processOpen(File $phpcsFile, $stackPtr)
349349
$phpcsFile->fixer->addNewline($prev);
350350
$phpcsFile->fixer->endChangeset();
351351
}
352-
} else if ($tokens[$prev]['line'] !== ($tokens[$className]['line'] - 1)) {
352+
} else if ((isset(Tokens::$commentTokens[$tokens[$prev]['code']]) === false
353+
&& $tokens[$prev]['line'] !== ($tokens[$className]['line'] - 1))
354+
|| $tokens[$prev]['line'] === $tokens[$className]['line']
355+
) {
353356
if ($keywordTokenType === T_EXTENDS) {
354357
$error = 'Only one interface may be specified per line in a multi-line extends declaration';
355358
$fix = $phpcsFile->addFixableError($error, $className, 'ExtendsInterfaceSameLine');

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc

+23
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,26 @@ class BarFoo implements
293293
namespace\BarFoo
294294
{
295295
}
296+
297+
// Safeguard that the sniff ignores comments between interface names in a multiline implements.
298+
class ClassWithMultiLineImplementsAndIgnoreAnnotation implements
299+
SomeInterface,
300+
// phpcs:disable Stnd.Cat.Sniff -- For reasons.
301+
302+
\AnotherInterface
303+
{
304+
}
305+
306+
class ClassWithMultiLineImplementsAndComment implements
307+
SomeInterface,
308+
// Comment.
309+
310+
AnotherInterface
311+
{
312+
}
313+
314+
class ClassWithMultiLineImplementsAndCommentOnSameLineAsInterfaceName implements
315+
SomeInterface,
316+
/* Comment. */ AnotherInterface
317+
{
318+
}

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.inc.fixed

+24
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,27 @@ class BarFoo implements
281281
namespace\BarFoo
282282
{
283283
}
284+
285+
// Safeguard that the sniff ignores comments between interface names in a multiline implements.
286+
class ClassWithMultiLineImplementsAndIgnoreAnnotation implements
287+
SomeInterface,
288+
// phpcs:disable Stnd.Cat.Sniff -- For reasons.
289+
290+
\AnotherInterface
291+
{
292+
}
293+
294+
class ClassWithMultiLineImplementsAndComment implements
295+
SomeInterface,
296+
// Comment.
297+
298+
AnotherInterface
299+
{
300+
}
301+
302+
class ClassWithMultiLineImplementsAndCommentOnSameLineAsInterfaceName implements
303+
SomeInterface,
304+
/* Comment. */
305+
AnotherInterface
306+
{
307+
}

src/Standards/PSR2/Tests/Classes/ClassDeclarationUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public function getErrorList()
7676
273 => 1,
7777
276 => 1,
7878
282 => 1,
79+
310 => 1,
80+
316 => 1,
7981
];
8082

8183
}//end getErrorList()

0 commit comments

Comments
 (0)