Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

welfordian's avatar

PHPStorm - Order Use Statements by Length

Hi there,

I wasn't sure where to put this as their doesn't seem to be a category for the series on editors like PHPStorm. What I want to be able to do is format my use statements using either Reformat Code (CTRL+SHIFT+L) or Optimize Imports (CTRL+SHIFT+O) by order of length.

So use App\Repositories\User would come before use App\Repositories\SomeOther. I've searched around for ways to change this as it looks like it currently orders by name.

Any ideas?

0 likes
17 replies
RoboRobok's avatar

In Sublime Text, I used phpfmt plugin that has this feature. I see it's also available for PhpStorm. Last update changed default format engine though and in this new one, ordering by length disappeared.

1 like
stevepop's avatar

@welfordian, did you by any chance find a solution to this? I tried to do same too and it is annoying having to do this manually every time. The only thing I have been able to do is sort Alphabetically.

ellisio's avatar

@bugsysha Maybe link to the solution you have? Some of us are willing to pay for a solution.

bugsysha's avatar

@ellisio my colleague and I made it for the company that we worked for. After that we decided to sell it to try to gather some funds for other free projects that we work on. If this is against the rules of the forum then I'll delete the post. I hope no one will think of this as self promotion.

https://sorter.kraftvel.com

I'm looking forward to some comments.

1 like
christophrumpel's avatar

@opilo tried this plugin, you mean "edit->string manipulations ->sort lines... by length" right? is there a way to include in into the reformat options?

1 like
Betterde's avatar

This feature is already supported in version 2019.1.

If you have multiple use statements in a file, you can now automatically sort them either alphabetically or by length. Go to the Code Conversion tab under Preferences | Editor | Code Style | PHP and choose the sorting type.

Having done that, use Code | Optimize Imports to sort your statements.

1 like
bugsysha's avatar

@Betterde Looking at import statements while using PhpStorm doesn't make any sense now. Just make it a part of "Actions on save" and you can go to the settings and fold import statements so they don't take up any space.

martinbean's avatar

I never understood why any one would want to order imports by length.

When I’m scanning lines of text, it’s easier to find what I’m looking for if they’re sorted alphabetically; not by how many characters it is. I don’t scan a list thinking, “Looking for something about 23 characters in length… ah, there it is!”

2 likes
bugsysha's avatar

@martinbean for me it was the insecurity of my programming skills and I wanted to match things as close as possible to what was shown in the videos here on Laracasts. It was like an easy win. Low hanging fruit that I can apply right away. Only when I gained enough confidence and really understood the pros and cons I started thinking on my own and realized how bad it is to sort them by length.

1 like
vmikulce's avatar

@bugsysha I use IDE. That is the thing I look into for the most of my working hours. In many and many years of my developer carrier, I have never manually searched for a class in-use statement. I am really curious to know of a use case, where it's faster to manually read through the use statement list and not use built-in IDE tools.

On the other side of things, if I am looking at something for most of my working hours, I want it to look pretty. That is why we have thems and the font and the entire styling. Sorting by character number has an aesthetic purpose. It is perfectly fine if aesthetics are not your thing and you prefer logical order. But saying that my aesthetic first approach is bad is just wrong. There is no good or bad way. Everyone does their own thing, and that is ok.

bugsysha's avatar

@vmikulce maybe you wanted to mention Martin because I do not understand how what I said a year ago relates to what you wrote. Please clarify if it is related to my post. And yes, sorting by length is bad because it makes it harder to find what you are looking for.

Rajabi's avatar

There is an option available to choose between alphabetic and length sorting style (File | Settings | Editor | Code Style | PHP > Code Conversion > "Sort Use Statements").

Please or to participate in this conversation.