Skip to content

OrderedImportsFixer - fix for trailing comma in group #4958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2020
Merged

OrderedImportsFixer - fix for trailing comma in group #4958

merged 1 commit into from
May 23, 2020

Conversation

kubawerlos
Copy link
Contributor

@kubawerlos kubawerlos commented May 16, 2020

Fixes #4955

Originally added test fails, as the result code is:

<?php use Foo\{
    ,
    Aaa,
    Bbb
};

@kubawerlos kubawerlos changed the base branch from 2.16 to 2.15 May 16, 2020 10:11
@SpacePossum
Copy link
Contributor

looking good!

I propose these tweaks;

diff --git a/src/Fixer/Import/OrderedImportsFixer.php b/src/Fixer/Import/OrderedImportsFixer.php
index a6c5ed36b..98a580477 100644
--- a/src/Fixer/Import/OrderedImportsFixer.php
+++ b/src/Fixer/Import/OrderedImportsFixer.php
@@ -390,8 +390,8 @@ private function getNewOrder(array $uses, Tokens $tokens)
                         $firstIndent = '';
                         $separator = ', ';
                         $lastIndent = '';
-
                         $hasGroupTrailingComma = false;
+
                         for ($k1 = $k + 1; $k1 < $namespaceTokensCount; ++$k1) {
                             $comment = '';
                             $namespacePart = '';
@@ -421,12 +421,13 @@ private function getNewOrder(array $uses, Tokens $tokens)
                             }
 
                             $namespacePart = trim($namespacePart);
-                            $comment = trim($comment);
                             if ('' === $namespacePart) {
                                 $hasGroupTrailingComma = true;
 
                                 continue;
                             }
+
+                            $comment = trim($comment);
                             if ('' !== $comment) {
                                 $namespacePart .= ' '.$comment;
                             }
diff --git a/tests/Fixer/Import/OrderedImportsFixerTest.php b/tests/Fixer/Import/OrderedImportsFixerTest.php
index e5de15806..117220275 100644
--- a/tests/Fixer/Import/OrderedImportsFixerTest.php
+++ b/tests/Fixer/Import/OrderedImportsFixerTest.php
@@ -2084,6 +2084,18 @@ public function provideFix72Cases()
 use Foo\{
     Bbb,
     Aaa,
+};',
+            ],
+            [
+                '<?php
+use Foo\{
+    Aaa /* 3 *//* 4 *//* 5 */,
+    Bbb /* 1 *//* 2 */,
+};',
+                '<?php
+use Foo\{
+    /* 1 */Bbb/* 2 */,/* 3 */
+    /* 4 */Aaa/* 5 */,/* 6 */
 };',
             ],
         ];

@SpacePossum SpacePossum added the RTM Ready To Merge label May 17, 2020
@SpacePossum SpacePossum added this to the 2.15.8 milestone May 17, 2020
@SpacePossum
Copy link
Contributor

Thank you @kubawerlos.

@SpacePossum SpacePossum merged commit c2fff43 into PHP-CS-Fixer:2.15 May 23, 2020
@SpacePossum SpacePossum removed the RTM Ready To Merge label May 23, 2020
@kubawerlos kubawerlos deleted the fix_ordered_imports_trailing_comma branch May 23, 2020 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OrderedImports - Error with trailing last comma on use statament
2 participants