How to Migrate From PHP CS Fixer to EasyCodingStandard in 6 Steps

We looked at how to migrate from PHP_CodeSniffer to Easy Coding Standard on Monday. But what if your weapon of choice is PHP CS Fixer and you'd to run also some sniffs?
There are a few simple A → B changes, but one has to know about them or will get stuck. Let's learn about them.

ECS is a tool build on Symfony 3.4 components that combines PHP_CodeSniffer and PHP CS Fixer. It's super easy to start to use from scratch:

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

But what if you already have PHP CS Fixer on your project and want to switch?

1. From String Codes to Autocompleted Classes

You use string references like strict_types in your .php_cs file. You need to remember them, copy paste them from README and copy-paste them right.

return PhpCsFixer\Config::create()
        'strict_types' => true,

That can actually cause typos like:

 return PhpCsFixer\Config::create()
-        'strict_types' => true,
+        'declare_strict_types' => true,

How to do that in EasyCodingStandard? Copy paste the name from README, capitalize first letter and remove _:

# ecs.yml

Then hit the "ctrl" + "space" for class autocomplete in PHPStorm (it works even now when I write this post in markdown, nice!).

    PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer: ~

That way Symfony plugin will autocomplete the class for you:

No more typos with strong over string typing.

2. From notPath() to skip Parameter

If you'd like to skip nasty code from being analyzed, you'd probably use this in PHP CS Fixer.

$finder = PhpCsFixer\Finder::create()

One big cons of this is that all fixers will skip this code, not just one. Do you need DeclareStrictTypesFixer to skip thisf ile? Sorry, all fixers will skip it.

EasyCodingStandard solves this common case - to skip a file, just use skip parameter:

            - 'my-nasty-dirty-file.php'

Do you have more such cases?

            - 'my-nasty-dirty-file.php'
            - 'sooo-dirty-file.php'
            - 'teribly-dirty-file.php'
            - 'sexy-dirty-file.php'

You don't have to list them all like a typing monkey. Just use fnmatch() format instead:

            - '*dirty-file.php'

In case you need to skip 1 fixer, put it under exclude_checkers:

        - PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer

For all other skip options, see README.

3. From .php_cs to YML Config

PHP CS Fixer looks for .php_cs file in the root directory by default.

And EasyCodingStandard looks for:

- ecs.yml
- ecs.yaml
- easy-coding-standard.yml
- easy-coding-standard.yaml

What about non-default locations or names?


vendor/bin/php-cs-fixer fix /path/to/project --config=custom/location.yml --dry-run


vendor/bin/ecs check /path/to/project --config custom/location.yml

4. Configuring Fixer Values

From PHP configuration in PHP CS Fixer:

return PhpCsFixer\Config::create()
        'array_syntax' => ['syntax' => 'short'],

to YML parameters in EasyCodingStandard:

        syntax: short

Nice and clear!

5. From no --dry-run to --fix option

From PHP CS Fixer:

vendor/bin/php-cs-fixer fix /path/to/project --dry-run
vendor/bin/php-cs-fixer fix /path/to/project

to EasyCodingStandard equivalent:

vendor/bin/ecs check /path/to/project
vendor/bin/ecs check /path/to/project --fix

6. From @Rules to imports

Do you like to use standards like PSR-2 or even PSR-12?

From @strings in PHP CS Fixer:

$config = PhpCsFixer\Config::create()
        '@PSR2' => true,

to autocompleted config in YAML file in Easy Coding Standard:

    - { resource: 'vendor/symplify/easy-coding-standard/config/psr2.yml' }

Do you want to see all the PSR-2 rules? Easy, just click on the file.

Give it a Try...

...and you won't regret it. Sylius, LMC, Shopsys, Nette, and SunFox did and never came back.

Did I forget a step that you had to fight with? Please, let me know in the comments or just send PR to this post to add it, so we help other readers.

Happy code sniffixing!

Do you want more on this topic? You might like these related posts: Combine power of PHP_CodeSniffer and PHP CS Fixer in 3 lines, 7 New Features in Easy Coding Standard 2.2, New in Easy Coding Standard 4: Clean Symfony Standard with Yaml and Services, How to Migrate From PHP_CodeSniffer to EasyCodingStandard in 7 Steps

What do you think?

GitHub RSS @votrubaT Runs on Statie Hosted on GitHub

Like what I write about? Hire me & we can work together