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 :

$ wget https://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -O php-cs-fixer
Get php cs fixer

Add execution permission :

$ sudo chmod a+x php-cs-fixer

You have to move it in your bin directory :

$ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer

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

$ php-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.