Skip to content

Commit c6359c4

Browse files
committed
PSR2/ClassDeclaration: prevent fixer conflict with itself [1]
While the `PSR2.Classes.ClassDeclaration` sniff did take partially/fully qualified names into account for interfaces being extended, it did not take _namespace relative_ interface name into account. This led to a fixer conflict within the sniff, where the sniff would first add a space between the `namespace` keyword and the namespace separator (`SpaceBeforeName` fixer) and in a subsequent loop would remove that same space again as it would think it was a space before a comma (`SpaceBeforeComma` fixer). Fixed now by adding support for namespace relative interface names in the `extends` checks. Includes unit test. Related to 152 Note: at a glance, this sniff could probably do with a more thorough review and more defensive token walking/more precise checking, but that's outside the scope of this PR.
1 parent 872add0 commit c6359c4

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,10 @@ public function processOpen(File $phpcsFile, $stackPtr)
397397
}
398398
}//end if
399399
} else if ($tokens[($className - 1)]['code'] !== T_NS_SEPARATOR
400-
|| $tokens[($className - 2)]['code'] !== T_STRING
400+
|| ($tokens[($className - 2)]['code'] !== T_STRING
401+
&& $tokens[($className - 2)]['code'] !== T_NAMESPACE)
401402
) {
402-
// Not part of a longer fully qualified class name.
403+
// Not part of a longer fully qualified or namespace relative class name.
403404
if ($tokens[($className - 1)]['code'] === T_COMMA
404405
|| ($tokens[($className - 1)]['code'] === T_NS_SEPARATOR
405406
&& $tokens[($className - 2)]['code'] === T_COMMA)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,8 @@ readonly
282282
class ReadonlyClassWithComment
283283
{
284284
}
285+
286+
// Safeguard against fixer conflict when there are namespace relative interface names in extends.
287+
interface FooBar extends namespace\BarFoo
288+
{
289+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,8 @@ readonly
270270
class ReadonlyClassWithComment
271271
{
272272
}
273+
274+
// Safeguard against fixer conflict when there are namespace relative interface names in extends.
275+
interface FooBar extends namespace\BarFoo
276+
{
277+
}

0 commit comments

Comments
 (0)