Skip to content

Commit 2908150

Browse files
Stop function and const being lost in UseDeclarationSniff
1 parent 89150c8 commit 2908150

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,18 @@ public function process(File $phpcsFile, $stackPtr)
6666
if ($tokens[$next]['code'] === T_COMMA) {
6767
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultipleDeclarations');
6868
if ($fix === true) {
69-
$phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.'use ');
69+
switch ($tokens[($stackPtr + 2)]['content']) {
70+
case 'const':
71+
$baseUse = 'use const';
72+
break;
73+
case 'function':
74+
$baseUse = 'use function';
75+
break;
76+
default:
77+
$baseUse = 'use';
78+
}
79+
80+
$phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.$baseUse);
7081
}
7182
} else {
7283
$closingCurly = $phpcsFile->findNext(T_CLOSE_USE_GROUP, ($next + 1));

src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ namespace MyProject;
33

44
use BarClass as Bar;
55
use My\Full\Classname as Another, My\Full\NSname;
6+
use function My\Full\functionname as somefunction, My\Full\otherfunction;
7+
use const My\Full\constantname as someconstant, My\Full\otherconstant;
68

79

810
namespace AnotherProject;

src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ namespace MyProject;
44
use BarClass as Bar;
55
use My\Full\Classname as Another;
66
use My\Full\NSname;
7+
use function My\Full\functionname as somefunction;
8+
use function My\Full\otherfunction;
9+
use const My\Full\constantname as someconstant;
10+
use const My\Full\otherconstant;
711

812

913
namespace AnotherProject;

src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public function getErrorList($testFile='')
3232
return [
3333
4 => 1,
3434
5 => 1,
35-
10 => 2,
35+
6 => 1,
36+
7 => 1,
37+
12 => 2,
3638
];
3739
case 'UseDeclarationUnitTest.3.inc':
3840
return [

0 commit comments

Comments
 (0)