Skip to content

Commit b07c153

Browse files
Majkl578kukulich
authored andcommitted
UseStatementHelper: Fix isTraitUse() to work with anonymous class
1 parent 64f1222 commit b07c153

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

SlevomatCodingStandard/Helpers/UseStatementHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function isAnonymousFunctionUse(\PHP_CodeSniffer_File $phpcsFile,
1616

1717
public static function isTraitUse(\PHP_CodeSniffer_File $phpcsFile, int $usePointer): bool
1818
{
19-
$typePointer = $phpcsFile->findPrevious(TokenHelper::$typeKeywordTokenCodes, $usePointer);
19+
$typePointer = $phpcsFile->findPrevious(array_merge(TokenHelper::$typeKeywordTokenCodes, [T_ANON_CLASS]), $usePointer);
2020
if ($typePointer !== false) {
2121
$tokens = $phpcsFile->getTokens();
2222
$typeToken = $tokens[$typePointer];

tests/Helpers/UseStatementHelperTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public function testIsTraitUse()
3232
$this->assertTrue(UseStatementHelper::isTraitUse($codeSnifferFile, $usePointer));
3333
}
3434

35+
public function testIsTraitUseInAnonymousClass()
36+
{
37+
$codeSnifferFile = $this->getCodeSnifferFile(
38+
__DIR__ . '/data/anonymousClassWithTrait.php'
39+
);
40+
$usePointer = $codeSnifferFile->findNext(T_USE, 0);
41+
$this->assertTrue(UseStatementHelper::isTraitUse($codeSnifferFile, $usePointer));
42+
}
43+
3544
public function testIsNotTraitUse()
3645
{
3746
$codeSnifferFile = $this->getCodeSnifferFile(
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
new class {
4+
use BazTrait;
5+
};

0 commit comments

Comments
 (0)