Stefano php, milano

A little handy tool: PSR Police

The PSR specifications have taken the PHP community by storm, and gave different developers working on different projects a common ground to share code and improve the speed and quality of their respective works.

The PSR-1 and PSR-2 specifications, in particular, are extremely important as they suggest a common formatting standard for PHP files. While this may seem unimportant, in the days of GitHub and distributed version control, keeping a consistent coding standard can help make pull requests little, and lessens the risk of having to perform a manual merge.

Furthermore, thanks to SensioLabs, we now have a [nifty tool] ( to automatically correct our code style to make it compliant with PSR-1 and PSR-2. But after using it for a while, I noticed I kept forgetting to run it before commit. The common suggestion to avoid this is to set up a precommit-hook in your working tree, but the process is a bit involved and difficult to remember.

For this reason I came up with a simple tool to automate the steps: PSR Police.


To use PSR Police you have to first install PHP-CS-Fixer system-wide.

wget -O php-cs-fixer
sudo chmod a+x php-cs-fixer
sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer

Then you can install PSR Police:

phpcomposer global require tacone/psr-police

After that, make sure that ~/.composer/vendor/bin is in your PATH.

All done!


From now on you can enforce the PSR-1/2 coding standard on any project with one command:

cd /var/www/myproject

You just need to run the command once per project. PSR Police will kick in at every commit, and format any committed file.

A few notes:

  • blade files are ignored
  • only the new and modified files since the previous commit will be re-formatted
  • if you clone your repository in another location, you'll need to run PSR Police again, as GIT hooks are not persisted in the remote repository
  • if you have a GIT pre-commit already set-up, PSR Police won't do anything, and just spit a warning

You can remove the PSR Police hook from your repository by just running:

rm .git/hooks/

I you will find this little simple tool as useful as I do.

Tags: php, git