Artisan Développeur

Qualité, amélioration continue, agilité.

Add php-cs-fixer support to phpstorm 9

Updated: 2018-07-13 new url for php-cs-fixer-v2

How To add php-cs-fixer from sensio to JetBrain phpStorm 9.

I want to have good code quality, follow best practices and coding standard. Some tools help to achieve that goal.

Php cs fixer is one of them. (Github repository)

You have two big steps :

  • install php cs fixer
  • add it as an phpstorm external tool

1 – Install php-cs-fixer (linux method)

I choose to install it globally and manually (other methods are available, see official website).

Get the tool with wget :

[pastacode lang= »bash » manual= »%24%20wget%20https%3A%2F%2Fcs.sensiolabs.org%2Fdownload%2Fphp-cs-fixer-v2.phar%20-O%20php-cs-fixer%0A » message= »Get php cs fixer » highlight= » » provider= »manual »/]

Add execution permission :

[pastacode lang= »bash » message= » » highlight= » » provider= »manual » manual= »%24%20sudo%20chmod%20a%2Bx%20php-cs-fixer%0A »/]

You have to move it in your bin directory :

[pastacode lang= »bash » message= » » highlight= » » provider= »manual » manual= »%24%20sudo%20mv%20php-cs-fixer%20%2Fusr%2Flocal%2Fbin%2Fphp-cs-fixer »/]

That’s it for this part, you have php-cs-fixer installed globally in your system, you can use it simply with :

[pastacode lang= »bash » message= » » highlight= » » provider= »manual » manual= »%24%20php-cs-fixer »/]

 

Second part, add it as external phpstorm9 tool. (This should work as well for phpstorm8)

In the phpStorm menu:
« File->Settings »
MENU_SETTINGS_redim

« Tools ->External tools » and click the + button :
config_tool

You have to fill the field :

  • the name, is the name visible in the menu, when you use your external tool, you need to fill with something clear:
    • php-cs-fixer
  • description, is for you, to remember the parameters
    • in my example, i use --dry-run and --diff to view all corrections, but rot apply them, description can be : dry run of php-cs-fixer to check without make modifications
  • Program field is for the command tool, we install php-cs-fixer globally, here we just need :
    • php-cs-fixer
  • Parameters are the command tool parameters :
    • I use -vv to verbose, fix (self-explained ^^), --dry-run to not apply fixes but display files list, $FilePrompt$ is phpStorm placeholder to ask for a « browse » window to browse where you want to use php-cs-fixer, to finish --diff show you the fixes.

      (In the official doc A combination of --dry-run and --diff will display summary of proposed fixes, leaving your files unchanged.)

       

  • Working directory :
    • $ProjectFileDir$ phpStorm placeholder

You can select « Ok » now you can use php-cs-fixer directly in phpStorm.

I mostly do Symfony2 project, but, sometimes is « just » php, or WordPress, so my first tool is generic, i use it to only check code.
I want to duplicate this external tool, to have some Symfony2 specific configurations.

Best way is to use the copy button, and modify « Parameters » after.

I use two more configurations :

  • Symfony specific with --config=sf2 parameter
  • Same with the --dry-run and --diff to check without making changes

config_tool_copy_redim

config_tool2_redim

Once you have done your settings, you go to the menu, « Tools -> external Tolls » and choose the one you want to use:

external_tool

If you select one with the --config=sf2 parameter, remember that you need to select the root directory of your symfony2 project, because php-cs-fixer will search for the « src » directory.

 

 

 

 

Leave a reply

Your email address will not be published.

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.