Try PSR-12 on Your Code Today

This post was updated at January 2023 with fresh know-how.
What is new?

Updated with ECS 12 and ECSConfig::configure() simple way to work with configs.


The standard is still behind the door, but feedback, before it gets accepted, is very important. After accepting it will be written down and it will be difficult to change anything.

Try PSR-12 today and see, how it works for your code.

PSR-12 meets ECS

Someone on Reddit referred a PSR Google Group, where they asked for real-life PSR-12 ruleset implementation in a coding standard tool. Korvin Szanto already prepared 1st implementation for PHP CS Fixer, at the moment only as a commit in the fork.

I've added PSR 12 set to ECS, so you can use it:

// ecs.php
use Symplify\EasyCodingStandard\Config\ECSConfig;
use PHP_CodeSniffer\Standards\Generic\Sniffs\Arrays\DisallowLongArraySyntaxSniff;

return ECSConfig::configure()
    ->withPreparedSets(psr12: true);

Do You Agree or Disagree with PSR12?

There are still few missed cases to be covered, but there is never to soon to get feedback from the community.

PR in PHP CS Fixer?

It will be a thing: PSR-12 set is definitely coming to PHP CS Fixer and PHP_CodeSniffer has also an active issue as well. Both of these tools are more stable, more popular and thus more rigid than ECS. So it will take time before there will be a pull-request and then stable release with PSR-12 set.

That's an advantage of smaller packages like ECS, they can evolve faster and live in the present. Only that way ECS 4 already has PSR-12 set on board and ready to use.

What do I Like?

I like that PSR-12 puts to standard rules that I consider standard for years and most of them are already integrated with ECS common sets:

What don't I Like?

Symplify code is already checked by PSR-12 (see pull-request):

Integration to project with ECS

It was easy to setup and works with 0 changes in the code. But as you can see, there is 1 rule I don't fully agree with.

PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixex

It takes care of spacing around !

if (!$isNotTrue) {
}

if ($isNotTrue) {
}

An important code should be visually clear, an unimportant code should not bother us. Personally, I prefer seeing the negation clearly, so I know it's a negation:

if (! $isNotTrue) {
}

Try It Yourself Today

Communicate, spread the ideas and find your way. This is only PSR - PS Recommendation. It's better to keep things standard for others, so they can drink water if they're thirsty and not start a research on bottle colors instead. But not a rigid rule that cannot be improved.


You love it or hate it? Let me know in the comments ↓


Happy coding!




Do you learn from my contents or use open-source packages like Rector every day?
Consider supporting it on GitHub Sponsors. I'd really appreciate it!