Samuel Hilson


How to configure PHPStorm to use PHP-CS-Fixer

The PHPStorm IDE by JetBrains is probably one of my favorite editors. It has its flaws (constant indexing…), but overall its a great tool for me. However, my focus today is not on PHPStorm per say, but rather how to add the awesome functionality of using PHP-CS-Fixer to PHPStorm’s already awesome tools. I am not going to cover the setup of PHP-CS-Fixer because its pretty well covered on the project page. Depending on your platform you will need to make some decisions, namely how you configure the rules of PHP-CS-Fixer. I am currently on Windows but I will try to make sure these instructions work in both *nix and Windows environments.

So we start are journey with some prerequisites:

  • PHPStorm installed
  • Composer Installed and added to your path
  • PHP-CS-Fixer installed via Composer

Decision time! Because I am on windows I found that passing any kind of complex configuration to the PHP-CS-Fixer was impossible because it would require passing valid Json through cmd.exe and cmd.exe strips out all the double quotes from the Json. Also, its gets pretty ridicules to pass a massive set of rules configuration to PHP-CS-Fixer so the configuration route provides a much better way to get this done. However, you can pass full configuration to the tool using --rules='{"rule_name": "value", ...}'

My recommendation is to use the config file though. Also, if you just want rules for a specific project you can drop the php_cs.dist file into the root of the project and skip adding the rules or config argument to this and PHP-CS-Fixer will grab it automagically.

Now you can setup the configuration for PHP-CS-Fixer. You can grab an example configuration from the project repo and modify it to match your needs. The documentation for all the rules are pretty extensive in their README, so check that out. Once you have that set note where you file is located for later steps.

Open PHPStorm, and go the File menu, and find Settings (maybe Preferences on OSX). The dialog that opens gives you access to all the many settings of PHPStorm. Look near the bottom of the list of settings for Tools and then External Tools or just Type External Tools in the search bar. When you select External Tools you will mostly likely see an empty list with some controls at the top. Hit the green plus icon.

You should see a blank dialog for adding an external tool like this:

Empty External Tool Dialog

Let’s setup this tool now. The name and description are arbitrary. I usually go for something like “Fix my stupid code Tool” but its up to you. Be creative! Most of the checked boxes are fine but once you have this working the way you want, you might want to come back and uncheck the Open Console option. Leave it for now we will need to see the output.

Program Field

Next we need to populate the Program field. This will need to point to the .phar or .bat for PHP-CS-Fixer depending on your platform. To get the correct value you can use which on *nix or where.exe on Windows.

OSX or Linux you will get something like this:

$ which php-cs-fixer
$ /home/username/.composer/vendor/bin/php-cs-fixer


$ where.exe php-cs-fixer
$ C:\Users\user\AppData\Roaming\Composer\vendor\bin\php-cs-fixer
$ C:\Users\user\AppData\Roaming\Composer\vendor\bin\php-cs-fixer.bat

Copy the path into the program field and be sure to include the .bat extension if you are on windows.


Now for Parameters. Parameters are appended after the program in the grand scheme of things so we start by adding fix --verbose then the rules or config argument --config=path/to/php_cs.dist or --rules=@PSR2 and then a few PHPStorm macros to get the file path and name "$FileDir$/$FileName$”. Additionally it may be a good idea to add--path-mode=intersection as well to prevent a problem where you pass a filename from the tool that has been excluded by your config. All together that might look like this:

fix --verbose --config=C:\path\to\php_cs.dist --path-mode=intersection "$FileDir$/$FileName$"

Working Directory

The final field on the dialog is for working directory and you can get that pretty easy from another PHPStorm macro $ProjectFileDir$. With all that in place your Tool settings should now look like this:

Tool Settings Section

Hit OK, and you now have the external tool configured. Don’t close settings just yet though. To make this tool super useful we are going to travel up the settings list to Keymap.


Click or search for Keymap in settings. Then in the keymap window find External Tools and then again External Tools and Then whatever you named your external tool we just configured. Double click your external tool or the green pencil at the top and select Add Keyboard Shortcut from the list. Then type a key combo (I use Alt+F) and hit OK, and then, hit OK again on settings.

Testing it out

Now click into an open php file, or open one from your project. Make sure the cursor is in the file and then hit your key combo. You will see the console open and you should get an output something like this:

PHP-CS-Fixer output

If you get some errors you will most probably have something wrong with the Parameters field or you config is not right. If everything goes well, you can open the external tool settings again an uncheck open console, unless you just like having it open.

Now go forth and program without worrying about all that formatting. You can clean it all up later with the click of a few buttons.

More by Samuel Hilson

Topics of interest

More Related Stories