be.dev

Web development blog by Philipp Rieber

Integrating PHP Coding Standards Fixer with PhpStorm

Fixing coding standard issue individually is very tedious, e.g after running a static code analysis tool or after being declined by a code sniffer pre-commit hook. To ease these kind of tasks you may consider using SensioLabs’s awesome little CLI tool called PHP Coding Standards Fixer.

You can simply run the PHP Coding Standards Fixer recursively on a directory or on a single file and it’ll make your PHP files mostly compliant with PSR-1/PSR-2 or Symfony coding standards, respectively. This vastly unburdens you from doing mindless and repetitive tasks. In addition, your coding style then relies on accepted standards, so there’s no pointless dispute about details.

Using Homebrew, installing the PHP CS Fixer is as simple as:

$ brew install homebrew/php/php-cs-fixer

For other platforms and installation options check out the project page.

To fix code styles recursively in all PHP files of a directory:

$ php-cs-fixer fix /path/to/dir

To fix code styles in a single PHP file:

$ php-cs-fixer fix /path/to/file

There are very fine-grained options to define the fixes to apply, but using the default symfony level should usually be a good choice to be as close as possible to the Symfony defaults. This already saves much time when applied from time to time or before a new commit to your VCS. Of course, you can also do a dry run first and display a diff of all the changes to be made. Check out php-cs-fixer help fix for all the details.

You can also integrate the tool into PhpStorm so that you can apply style fixes by executing a custom shortcut. This is possible by setting up an External Tool definition in PhpStorm: Got to Preferences > Tools > External Tools > + (Create Tool) and fill in the details about how to execute the PHP CS Fixer (for convenience, you can simply copy&paste the field values given below the screenshot):

Setting up PHP CS Fixer as an external tool in PhpStorm

  • Name: php-cs-fixer
  • Program: /usr/local/bin/php-cs-fixer
  • Parameters: --level=symfony --verbose --config=sf23 fix "$FileDir$/$FileName$"
  • Working directory: $ProjectFileDir$

Then navigate to Preferences > Keymap, search for php-cs-fixer and assign a custom shortcut, e.g. Ctrl+Alt+P. Now, when you open up a PHP file in your editor and hit your assigned shortcut, the code gets formatted according to your external tool definition of php-cs-fixer.

Another nice feature of the PHP CS Fixer is to add a .php_cs file at the project’s root to improve the tool’s integration by giving sensible default options. This way it’ll be quite easy for example to additionally enforce PHP’s short array syntax or to give a default list of directories to be analysed.

Of course, it’s should also possible to integrate the PHP CS Fixer into an automated process.

Fixing code styles might also be possible by using PhpStorm’s Reformat Code action, and I also use it from time to time to clean things up a bit, but from my experience this tool is a bit greedy and tries to fix things you may not want to be fixed, like chained calls of a fluent interface. Moreover, sharing code style definitions across teams becomes much easier when they are not tied to an IDE and can even be stored within the code repository (.php_cs file). On the contrary what’s quite useful I think is PhpStorm’s Optimize imports action which cleans up and orders all the use statements – at least since PhpStorm is smart enough to not remove imports that are only used within annotations :-)

 

Philipp Rieber is a passionate and certified PHP and Symfony developer working at Paymill in Munich, Germany. He is also engaged in the frontend, the Cloud, on Mobile, as a DevOp and a technical writer. He is never missing the PHP User Group Munich and he is co-organizing the Symfony User Group Munich.

philipp-rieber.net

 

Liked this post? Follow me on Twitter for updates.