Skip to content

Instantly share code, notes, and snippets.

@nienkedekker
Last active February 2, 2024 03:09
Show Gist options
  • Save nienkedekker/3ddb9ece42233698c0e3f3e42cf1ff34 to your computer and use it in GitHub Desktop.
Save nienkedekker/3ddb9ece42233698c0e3f3e42cf1ff34 to your computer and use it in GitHub Desktop.
Set up PHP-CS-Fixer in PHPStorm

Use PHP-CS-Fixer in PHPStorm

  • Install PHP-CS-Fixer on your local machine according to these instructions: https://github.com/FriendsOfPHP/PHP-CS-Fixer
  • Open PHPStorm, Preferences > Tools > External Tools and enter these values: img
  • Program, edit to match your path where PHP-CS-Fixer lives: /.composer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer
  • Parameters: --rules=@PSR2 --verbose fix $FileDir$/$FileName$. Note that previous verions of PHP-CS-Fixer used --levels instead of --rules.
  • Working directory: $ProjectFileDir$

Click OK and Apply. Now we'll set up a shortcut.

  • Go to Preferences > Keymap and search for "PHP Fixer" (or whatever name you gave it). Add whatever shortcut you like, I'm using ctrl + cmd + ]: img

You should be ready to go. Open a file that's not up to standards and run your shortcut, and it should be fixed once you Save the file. It will only run on Save, otherwise every keystroke will be checked.

@imzyf
Copy link

imzyf commented Apr 16, 2018

Note that previous verions of PHP-CS-Fixer used --levels instead of --rules.

sorry, I don't find it. Could you tell me where is it? thanks

@marty8zhang
Copy link

For me, fix $FilePath$ works better than fix $FileDir$/$FileName$. The former works against both a file and a folder, and the latter only works on a single file but not a folder.

@honsa
Copy link

honsa commented Nov 24, 2018

@biplobice
Copy link

@Bobcui001
Copy link

@marty8zhang Great!This way solves my batch formatting problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment