PhpStorm with PHP-CS-Fixer and docker (and keybind)

Marcel Lamm
2 min readSep 23, 2019

--

I find https://github.com/FriendsOfPHP/PHP-CS-Fixer a nice way to ensure some sort of code-style in your project that is defined by in a config file php_cs and can therefore be committed and shared with your team.

The cool kids are using docker I heard

The other day I found myself in a project that was using https://www.docker.com/ + https://docs.docker.com/compose/ and therefore running on a different PHP-Version than my hostsystem was using.

PHPStorm and PHP-CS-Fixer

So from what I know of PHPStorm is currently capabale of using PHP-CS-Fixer for “realtime”-Inspection but not to apply any fixes. Therefore I am using an “External Tool” here to help out, together with a small bash-script.

Helper Script: docker-phpcs

This one uses docker-compose to find your php container to run php-cs-fixer in. The app-php service is the one that has PHP in it and the Code mounted in (see your docker-compose.yml). I assume here that /var/www is where your code is mounted to. Copy that into a script and make it chmod +x.

#!/bin/bash

/usr/bin/docker-compose exec -T app-php /var/www/vendor/bin/php-cs-fixer "$@"

Throw all together in PHPStorm

Add new External Tool

build/bin/docker-phpcs is the path here of my helper-script.

You can disable “Open console for tool output” after confirming that it works, this is just for debugging now.

Give it a keybind

Now you can press Ctrl+Alt+L all day long to have your currently opened file automatically formatted. Allow it some delay, since it runs on the container via docker-compose.

--

--