The IDE by is probably one of my favorite editors. It has its flaws (constant indexing…), but overall its a great tool for me. However, my focus today is not on PHPStorm per say, but rather how to add the awesome functionality of using to PHPStorm’s already awesome tools. I am not going to cover the setup of PHP-CS-Fixer because its pretty well covered on the . Depending on your platform you will need to make some decisions, namely how you configure the rules of PHP-CS-Fixer. I am currently on Windows but I will try to make sure these instructions work in both *nix and Windows environments. PHPStorm JetBrains PHP-CS-Fixer project page : So we start are journey with some prerequisites PHPStorm installed Installed and added to your path Composer PHP-CS-Fixer installed via Composer Decision time! Because I am on windows I found that passing any kind of complex configuration to the PHP-CS-Fixer was impossible because it would require passing valid Json through cmd.exe and cmd.exe strips out all the double quotes from the Json. Also, its gets pretty ridicules to pass a massive set of rules configuration to PHP-CS-Fixer so the configuration route provides a much better way to get this done. However, you can pass full configuration to the tool using --rules='{"rule_name": "value", ...}' My recommendation is to use the config file though. Also, if you just want rules for a specific project you can drop the file into the root of the project and skip adding the rules or config argument to this and PHP-CS-Fixer will grab it automagically. php_cs.dist Now you can setup the configuration for PHP-CS-Fixer. You can grab an example from the project repo and modify it to match your needs. The documentation for all the rules are pretty extensive in their , so check that out. Once you have that set note where you file is located for later steps. configuration README Open PHPStorm, and go the menu, and find (maybe Preferences on OSX). The dialog that opens gives you access to all the many settings of PHPStorm. Look near the bottom of the list of settings for Tools and then External Tools or just Type External Tools in the search bar. When you select External Tools you will mostly likely see an empty list with some controls at the top. Hit the green plus icon. File Settings You should see a blank dialog for adding an external tool like this: Empty External Tool Dialog Let’s setup this tool now. The name and description are arbitrary. I usually go for something like “Fix my stupid code Tool” but its up to you. Be creative! Most of the checked boxes are fine but once you have this working the way you want, you might want to come back and uncheck the option. Leave it for now we will need to see the output. Open Console Program Field Next we need to populate the Program field. This will need to point to the or for PHP-CS-Fixer depending on your platform. To get the correct value you can use on *nix or on Windows. .phar .bat which where.exe OSX or Linux you will get something like this: $ which php-cs-fixer$ /home/username/.composer/vendor/bin/php-cs-fixer Windows: $ where.exe php-cs-fixer$ C:\Users\user\AppData\Roaming\Composer\vendor\bin\php-cs-fixer$ C:\Users\user\AppData\Roaming\Composer\vendor\bin\php-cs-fixer.bat Copy the path into the field and be sure to include the .bat extension if you are on windows. program Parameters Now for Parameters. Parameters are appended after the program in the grand scheme of things so we start by adding then the rules or config argument or and then a few PHPStorm macros to get the file path and name " . Additionally it may be a good idea to add as well to prevent a problem where you pass a filename from the tool that has been excluded by your config. All together that might look like this: fix --verbose --config=path/to/php_cs.dist --rules=@PSR2 $FileDir$/$FileName$” --path-mode=intersection fix --verbose --config=C:\path\to\php_cs.dist --path-mode=intersection "$FileDir$/$FileName$" Working Directory The final field on the dialog is for working directory and you can get that pretty easy from another PHPStorm macro . With all that in place your settings should now look like this: $ProjectFileDir$ Tool Tool Settings Section Hit OK, and you now have the external tool configured. Don’t close settings just yet though. To make this tool super useful we are going to travel up the settings list to Keymap. Keymap Click or search for in settings. Then in the keymap window find and then again and Then whatever you named your external tool we just configured. Double click your external tool or the green pencil at the top and select from the list. Then type a key combo (I use ) and hit OK, and then, hit OK again on settings. Keymap External Tools External Tools Add Keyboard Shortcut Alt+F Testing it out Now click into an open php file, or open one from your project. Make sure the cursor is in the file and then hit your key combo. You will see the console open and you should get an output something like this: PHP-CS-Fixer output If you get some errors you will most probably have something wrong with the Parameters field or you config is not right. If everything goes well, you can open the external tool settings again an uncheck open console, unless you just like having it open. Now go forth and program without worrying about all that formatting. You can clean it all up later with the click of a few buttons.
Share Your Thoughts