Automatically apply PSR-2 coding style


In version 2 of php_cs_fixer some parameters and rules are renamed. Upgrade guide can be see on their Github page. This how-to is accordingly updated.

Applying PSR-2 coding style manually is really no fun and wasting time. A short visit in Github and there’s a tool which detects and fixes the coding style. The tool is called php-cs-fixer.

I’ll show you how to get the tool and use it to easily reformat all PHP file in project.

There are several ways to get this tool, the easiest way is just to download the phar file and make it executable:

or if homebrew is installed, just run

You can start fixing your code by running

But let’s integrate it in PhpStorm and add shortcut to run this command.

Go to PhpStorm and open the Preferences window. Navigate to Tools > External Tools. Create a new item. Let’s call it php-cs-fixer


The fields in the “Tool settings” are important one. Following values should be used:

Program: Absolute path to the php-cs-fixer, we downloaded earlier.

Parameters: you can set options for the php-cs-fixed. I use following option --rules=@PSR2 --verbose fix "$FilePath$"

Working directory: Just put $ProjectFileDir$


Now you can right click on any folder or file and run the php-cs-fixer from the external tools pop up. You can also set some keyboard shortcut to run the command. Just open the Preferences window once more and go to the Keymap. Search for php-cs-fixer and set new keyboard shortcurt.

Happy code styling 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *