PSR-12 coding style

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.

Here it is: https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md

And here’s thoughts behind it: https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide-meta.md

What do you think? Anything to add/remove/fix?

I don’t have anything to add at the moment but I really like it. :)

Use statements should be in alphabetical order - it is really useful when we have many of them.

We had that previously but it’s too strong requirement for people without IDEs. And there are many.

Some progress on the document: https://github.com/php-fig/fig-standards/compare/34595546bde85c5a352308afcb0e2fe683ff9012...a77993bdf52eb6d5cb9ca37dba3af4df9e3b909c

After half a year I must disagree with that.

  1. I often change the order of use statements and it is really easy to make it alphabetical.

  2. Unordered use statements is a nightmare…

  3. 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.

  1. Not without a tool.

  2. 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.

  3. I’m following coding standard w/o tools same as many other developers.

  1. I’m doing that without a tool and it is easy - you just need to know the alphabet. :)

  2. 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.

  3. 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:

  1. Make it human-readable.

  2. 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).

Survey launched: https://groups.google.com/forum/#!topic/php-fig/beBQqv6PEyc

I agree with most of the propositions here.

Looks like I have to teach myself to write brackets at the end of $model = new Model; :lol: