As part of PHP-FIG along with Michael Cullum, Korvin Szanto and Robert Deutz we’re working on the next coding standard. PSR-12. The idea is to fix PSR-2 minor issues and add up-to-date PHP stuff into it.
I often change the order of use statements and it is really easy to make it alphabetical.
Unordered use statements is a nightmare…
Coding standard PSRs are mostly for automated tools and it is hard to follow any standard without them - it is easy to miss some space or put the bracket in wrong place. Also, there are separated tools for fixing CS (https://github.com/FriendsOfPHP/PHP-CS-Fixer/), so you don’t need use IDEs for automatically fixing some CS issues.
Why? I don’t see any issue with that. Additionally, I saw some people are grouping but not alphabetically such as domain stuff is separated from infrastructure.
I’m following coding standard w/o tools same as many other developers.
I’m doing that without a tool and it is easy - you just need to know the alphabet.
It is much harder to find anything if use statements are in random order. Couple times I even try to add a statement, that already exists, because I can’t find them at first look. This is much more annoying than missing space after control structure or bracket in wrong place.
I can sort use statements alphabetically without any tool. More often I omit some spaces after control structures, so from my point of view, the PSR already has rules which are difficult to follow without tools for fixing CS, and refuse to add a rule which is not problematic at all.
Sorting is easy for 1st letter only. After 2nd letter match it becomes a nightmare to maintain w/o a tool. Anyway, that’s my personal opinion and I’m not the only who’s behind PSR-12: there are editor and sponsor + many projects to vote for it. I think it’s a good idea if you’ll post to mailing list about it https://groups.google.com/forum/#!forum/php-fig. Don’t forget to prefix your topic with [PSR-12].
Why? Sorting by the second and the next letters looks the same as sorting by first letter - you just sort the smaller group with the same prefix. It actually should be easier than sorting by first letter because you have less items to sort.
I don’t know. My eye doesn’t like doing that. I’d better leave it messy than do it by hand.
I never actually looking at imports. IDE solves it for me via quick info boxes which show fully qualified names and navigation which goes straight to implementation. Also it adds them automatically.
Overall there are two goals for the coding standard:
Make it human-readable.
Make merge fail less w/ nicer diffs.
As for #1, as I’ve mentioned, I saw imports grouped logically, not alphabetically and that makes more sense in some cases. Especially when domain layer is big and you’re working with more than a single bounded context. If we add the rule that everything should be sorted alphabetically we’re basically eliminating an ability to group imports in any other way.
As for #2, re-ordering or imports may affect diffs but it doesn’t really matter if these are ordered by name or not.
I thought that we are talking about the people who do not use the IDE. In your case, if the IDE manages imports it might as well sort them alphabetically. For people without IDE random order of imports is a problem because they must read them and manage by hand.
Namespaces should represent the structure of the application. In that case, alphabetic order naturally groups imports by domain and purpose of objects.
Well, seems I can’t really provide strong arguments against it Personally I’m not fond of it but I think you have a chance to have it in the PSR. Post to mailing list as I’ve suggested and I think it will be included in the style guide poll (other guys working with me are putting it together).