From 4377de2e1405fceeb7954195d2c19e11ea595dc2 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Thu, 24 Sep 2020 14:28:23 +1000 Subject: [PATCH] Fixed bug #3053 : PSR2 incorrect fix when multiple use statements on same line do not have whitespace between them --- .../PSR2/Sniffs/Namespaces/UseDeclarationSniff.php | 4 ++++ .../PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc | 4 ++++ .../Tests/Namespaces/UseDeclarationUnitTest.2.inc.fixed | 8 ++++++++ .../PSR2/Tests/Namespaces/UseDeclarationUnitTest.php | 5 ++++- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php b/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php index ea26657b03..21a9dbe9a3 100644 --- a/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php +++ b/src/Standards/PSR2/Sniffs/Namespaces/UseDeclarationSniff.php @@ -77,6 +77,10 @@ public function process(File $phpcsFile, $stackPtr) $baseUse = 'use'; } + if ($tokens[($next + 1)]['code'] !== T_WHITESPACE) { + $baseUse .= ' '; + } + $phpcsFile->fixer->replaceToken($next, ';'.$phpcsFile->eolChar.$baseUse); } } else { diff --git a/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc b/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc index 068b5233f1..bc0d7a961e 100644 --- a/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc +++ b/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc @@ -6,6 +6,10 @@ use My\Full\Classname as Another, My\Full\NSname; use function My\Full\functionname as somefunction, My\Full\otherfunction; use const My\Full\constantname as someconstant, My\Full\otherconstant; +use BarClass as Bar,FooClass,BazClass as Baz; +use function My\Full\functionname as somefunction,My\Full\otherfunction; +use const My\Full\constantname as someconstant,My\Full\otherconstant; + namespace AnotherProject; diff --git a/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc.fixed b/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc.fixed index 21e574ddb2..6579613b12 100644 --- a/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc.fixed +++ b/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.2.inc.fixed @@ -9,6 +9,14 @@ use function My\Full\otherfunction; use const My\Full\constantname as someconstant; use const My\Full\otherconstant; +use BarClass as Bar; +use FooClass; +use BazClass as Baz; +use function My\Full\functionname as somefunction; +use function My\Full\otherfunction; +use const My\Full\constantname as someconstant; +use const My\Full\otherconstant; + namespace AnotherProject; diff --git a/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.php b/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.php index b0b47e3530..b424a2c113 100644 --- a/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.php +++ b/src/Standards/PSR2/Tests/Namespaces/UseDeclarationUnitTest.php @@ -34,7 +34,10 @@ public function getErrorList($testFile='') 5 => 1, 6 => 1, 7 => 1, - 12 => 1, + 9 => 1, + 10 => 1, + 11 => 1, + 16 => 1, ]; case 'UseDeclarationUnitTest.3.inc': return [