Create a File Watcher for php-cs-fixer in PhpStorm
This is a quick an easy guide to setting up a file watcher on PhpStorm so that every time a php file is saved, php-cs-fixer is ran on the file. More than just a linter, php-cs-fixer will update your code to match coding standards such as the official PSR-1 and PSR-2, as well as community standards such as the Symfony one. It will also do some modernization and micro-optimizations.
There are a few different installation options, but the Composer install seemed like the easiest (like it always does). If you don't have composer installed you can follow the link above to install it. After Composer is installed, from the terminal run:
$ composer global require friendsofphp/php-cs-fixer
After it installs you can try and run it from the terminal.
$ php-cs-fixer --help
If it doesn't run, it'll need added to the path. Here is a linux command, my Windows box worked out of the box. I'm assuming the Composer Windows installer handled this for me.
$ export PATH="$PATH:$HOME/.composer/vendor/bin"
Create the PhpStorm File Watcher
From inside of a project in PhpStorm, open up the settings page. From the menu go to
File > Settings or use the keyboard
ctrl+alt+s (in Windows or linux).
From there you can start typing 'file watcher' to narrow down the options and then select
File Watchers from under the
Next either press
alt + insert or press the green plus sign icon in the upper right-hand corner to create a new file watcher.
Custom Template option.
In the dialog box that opens, give your watcher a name, I chose "php-cs-fixer", for the sake of simplicity.
Set the File type to "PHP" and Scope to "Current File".
Set the Program to "php-cs-fixer". In my Windows environment, I had to add the '.bat' file extension (example below).
In my linux environment I had to use the full path to my
~/composer/vendor/bin/php-cs-fixer (example above). Run
which php-cs-fixer from the terminal to get the full path. This command works on Windows 10, too.
The Arguments is where all the work is done. First type
fix which is the command for php-cs-fixer to actually do something.
Next add any flags you want to pass in for the rules. Finally add
$FilePath$ which is a PhpStorm variable for the full
path to current file, up to and including the name and extension.
Set the "Output paths to refresh" to
$FilePath$ so the editor reload the current file as soon as the changes are saved.
The final step and a very important one is to uncheck the "Auto-save edited files to trigger the watcher" checkbox under "Advanced Options." If this is checked, the watcher will run with each keystroke in the file editor and PhpStorm will become confused if the memory (editor) changes or the disk(php-cs-fixer) changes are the ones to use. If you are still on PhpStorm 2017.2 this option is labeled "Immediate file synchronization".
Click "OK" to save your file watcher, and then close the settings dialog.
Setting up this file watcher in PhpStorm is super easy and crazy useful.
Another option would have been to use the external tools option of PhpStorm, which is the original way I was going to do this, inspired by a Laracasts tutorial. The only downfall to using php-cs-fixer as an external tool, is that you have to manually trigger it. I wanted php-cs-fixer to be a fully automated step in my work flow.
One more, fully automated, way to integrate php-cs-fixer would be to use a git pre-commit hook. I still like the file watcher option better, because it allows me to see in the editor if there are still warnings from other linters and sniffers if there are things that could still be improved even after php-cs-fixer is ran.If you liked this post, you can subscribe to the rss feed or follow me @ToddEidson on Twitter to be notified of future blog posts.
Date Published: 30 November, 2017