Try PSR-12 on Your Code Today

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.


composer require symplify/easy-coding-standard --dev
vendor/bin/ecs check /src --level psr12

And to fix the code:

vendor/bin/ecs check /src --level psr12 --fix

Now in more detailed way.

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 put the ruleset to psr12.yml level in ECS and it looks like this in time of being:

    - { resource: 'php_cs_fixer/psr2.yml' }

    PhpCsFixer\Fixer\CastNotation\LowercaseCastFixer: ~
    PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer: ~
    PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer: ~
    PhpCsFixer\Fixer\Import\NoLeadingImportSlashFixer: ~
            - 'class'
            - 'const'
            - 'function'
        space: 'none'
    PhpCsFixer\Fixer\Operator\NewWithBracesFixer: ~
        'allow_single_line_closure': false
        'position_after_functions_and_oop_constructs': 'next'
        'position_after_control_structures': 'same'
        'position_after_anonymous_constructs': 'same'

    PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer: ~
            - 'use_trait'
            - 'const'
            - 'method'
            - 'property'
    PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer: ~
    PhpCsFixer\Fixer\Operator\TernaryOperatorSpacesFixer: ~
    PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixer: ~
    PhpCsFixer\Fixer\FunctionNotation\ReturnTypeDeclarationFixer: ~
    PhpCsFixer\Fixer\Whitespace\NoTrailingWhitespaceFixer: ~

        spacing: 'one'

    PhpCsFixer\Fixer\Semicolon\NoSinglelineWhitespaceBeforeSemicolonsFixer: ~

        - 'PhpCsFixer\Fixer\Import\SingleImportPerStatementFixer'
        - 'PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer'

Do You Agree or Disagree with PSR12?

There are still many missed cases to be integrated into the standard, 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 2 rules I don't fully agree with.

1. PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer

This rule creates this code:



Namespace changes, file doc changes, use, class, interface... that all changes in every file, so it should be on a standalone line, that will force you to notice it and orientate. But not declare(strict_types=1);, that is the same in every file.

I think our attention deserves to ignore anything that is the same in every file so inline its line:

<?php declare(strict_types=1);

2. PhpCsFixer\Fixer\Operator\UnaryOperatorSpacesFixex

It takes care of spacing around !

if (!$isNotTrue) {

if ($isNotTrue) {

Here we can apply the same approach we did in 1. 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.

So just try it. Maybe your code is already PSR-12 ready.

And let me know in the comments, if you love it or rather hate it :)

Happy coding!

What do you think?

GitHub RSS @votrubaT Like what I write about? Hire me & we can work together