Skip to content

Commit bb933e9

Browse files
committed
PSR12/ClassInstantiation: fix sniff to work with the PHP 8 identifier tokens
Includes some additional tests to cover the change.
1 parent 0f1c898 commit bb933e9

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

src/Standards/PSR12/Sniffs/Classes/ClassInstantiationSniff.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ public function process(File $phpcsFile, $stackPtr)
4343
$tokens = $phpcsFile->getTokens();
4444

4545
// Find the class name.
46-
$allowed = [
47-
T_STRING => T_STRING,
48-
T_NS_SEPARATOR => T_NS_SEPARATOR,
46+
$allowed = Tokens::$nameTokens;
47+
$allowed += [
4948
T_SELF => T_SELF,
5049
T_STATIC => T_STATIC,
5150
T_PARENT => T_PARENT,

src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ $foo = new parent;
4949
// PHP 8.3: safeguard that the sniff ignores anonymous classes, even when declared as readonly.
5050
$anon = new readonly class {};
5151
$anon = new #[MyAttribute] readonly class {};
52+
53+
$foo = new \FQN\Bar();
54+
$foo = new namespace\Bar();
55+
$foo = new \FQN\Bar;
56+
$foo = new namespace\Bar;

src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ $foo = new parent();
4949
// PHP 8.3: safeguard that the sniff ignores anonymous classes, even when declared as readonly.
5050
$anon = new readonly class {};
5151
$anon = new #[MyAttribute] readonly class {};
52+
53+
$foo = new \FQN\Bar();
54+
$foo = new namespace\Bar();
55+
$foo = new \FQN\Bar();
56+
$foo = new namespace\Bar();

src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function getErrorList()
4949
37 => 1,
5050
38 => 1,
5151
47 => 1,
52+
55 => 1,
53+
56 => 1,
5254
];
5355

5456
}//end getErrorList()

0 commit comments

Comments
 (0)