Skip to content
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

Add different sort algorithm for "uses" - also order by length THEN alpha #3591

Closed
garygreen opened this issue Mar 9, 2018 · 8 comments
Closed

Comments

@garygreen
Copy link

garygreen commented Mar 9, 2018

When using the ordered_imports option, you can only specify one sortAlgorithm, e.g. length:

'ordered_imports' => [
    'sortAlgorithm' => 'length',
],

This results in the following:

image

Which is correct however it also sorts the use statements inside the class.

However it would be nice if you could specify a different sort order for the "use" statements inside the class - I would like them to be alpha sorted. Ideally there should also be two sort options so it sorts by length THEN alpha.

Maybe something like:

'ordered_imports' => [
    'sortAlgorithm' => ['length', 'alpha'],
],
'ordered_use' => [
    'sortAlgorithm' => 'alpha',
]
@SpacePossum
Copy link
Contributor

Hi and thanks for opening this issue :)

When you state;

Which is correct however it also sorts the use statements inside the class.

do you mean the ordered_imports is also sorting your Trait uses/imports? It shouldn't touch those..

@garygreen
Copy link
Author

garygreen commented Mar 9, 2018

Oh yes my bad, it's not touching those I was under the impression it was. Maybe that's an idea for another option though? To sort the use ones - I just want those ones in alpha order though, not by length (as they are all on one line)

@SpacePossum
Copy link
Contributor

Thanks for clearing it up, all good 👍
I'm not sure but I think sorting the traits use/imports in different order might cause the code to be act different with respect to conflict handling of methods having the same name. I've to check though ;)

@ibrunotome
Copy link

Using enums in php 8.1, it tries to sort traits with outside imports too.

Screen Shot 2021-12-16 at 11 49 16

@SpacePossum
Copy link
Contributor

@ibrunotome I hope that is fixed in #6181 , you can try by using the latest commit. If not fixed please make a new issue for the problem

@zaherg
Copy link

zaherg commented May 3, 2022

you can use ordered_class_elements for this, sadly the only thins supported is alpha and none more info https://cs.symfony.com/doc/rules/class_notation/ordered_class_elements.html

@SpacePossum
Copy link
Contributor

You can use https://cs.symfony.com/doc/rules/class_notation/ordered_traits to order the import as alpha.

So there are a few fixers to check:

I'm going to close this ticket now as it is unclear what is left to do/asked, feel free to make a new issue with a request if something is still lacking.

@azimidev
Copy link

"ordered_imports": {"sort_algorithm":"length"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants