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.
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:
curl http://get.sensiolabs.org/php-cs-fixer.phar -o /usr/local/bin/php-cs-fixer
sudo chmod a+x php-cs-fixer
homebrew is installed, just run
brew install php-cs-fixer
You can start fixing your code by running
php-cs-fixer --rules=@PSR2 --verbose fix FILENAME
But let’s integrate it in PhpStorm and add shortcut to run this command.
Go to PhpStorm and open the Preferences window. Navigate to
External Tools. Create a new item. Let’s call it
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
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 🙂