PHP-CS-Fixer icon indicating copy to clipboard operation
PHP-CS-Fixer copied to clipboard

`ordered_class_elements` should have per-item sorting setting

Open PatchRanger opened this issue 1 year ago • 7 comments

Problem description

There is no way to switch on/off sorting on per-item basis. sort_algorithm works for all of them, all at once: either sort them all by alpha or sort them all using none. One can't set another sorting for some items.

Feature request

E.g., it could be useful in such case:

'order' => ['use_trait', 'case', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method' => ['sort_algorithm' => 'none']],
'sort_algorithm' => 'alpha',

I mean sort everything except any methods. Or other cases - in any combinations that are allowed by this setting syntax. It looks totally reasonable: to have one option as a default for non-mentioned (sort_algorihm) while another could re-define, overwrite its behavior by per-item setting.

I guess this feature could help resolving other issues reported here in issue queue, such as #2746 (if applied together with #6360 ) and partially #4513 .

PatchRanger avatar Aug 28 '22 08:08 PatchRanger

I think 2746 and 6360 don't benefit from none sorting, these ask for another/special way of sorting (so not do-not-sort) of some elements.

If you not configure 'method' => ['sort_algorithm' => 'none'] but leave method out of the configuration, doesn't this already do what you want? I struggle to see how none would work otherwise as when you configure every type of element to get sorted expect one type, where should elements of that type end up if not at the end of the class?

Can you maybe provide an PHP examples with expected input, config and output?

SpacePossum avatar Aug 28 '22 09:08 SpacePossum

Thanks for your response, I appreciate.

If you not configure 'method' => ['sort_algorithm' => 'none'] but leave method out of the configuration, doesn't this already do what you want?

I've checked it locally - and looks like I got it wrong. I thought that it's expected behavior - but according to your reply (and my test that I've created to illustrate it) I understood that we should clarify it.

Can you maybe provide an PHP examples with expected input, config and output?

Please review the test I've added to #6592 - and let's investigate whether it's expected or not.

PatchRanger avatar Aug 28 '22 14:08 PatchRanger

It is kind of expected, as the feature was indeed build as sort everything of the same type. We can keep this issue as a valid RFC :+1:

SpacePossum avatar Sep 05 '22 10:09 SpacePossum