tl;dr It is easy to execute any bash script or external tool within PhpStorm to enhance development.
PhpStorm easens PHP development a lot with out-of-the-box features like autocompletion, code analysis, quick navigation to files & methods, visual debugging, and VCS & Composer support. More than 1.000 plugins are available to extend PhpStorm core functionality.
Additionally, PhpStorm offers a very convient way to add any bash script or external tool and run them with a click of a button, or a keyboard shortcut. In contrast to other editors it is not required to use a plugin skeleton or a meta language for this, but a slick GUI only.
This is the perfect solution to run custom linters or start tinkered tasks. For instance a script which uses phpDocumentor to generate a documentation of your codebase and push it to a designated server. Or execute a code style fixer with custom rules.
In the following example I'll show you how to run a PHP codestyle fixer let it convert the currently edited PHP file to PSR-2, the basic coding standard established by the PHP Framework Interop Group.
- Download PHP CS Fixer and install it globally.
- In PhpStorm open »Settings«, toogle »Tools«, click »External Tools«, then
+symbol to add a new external tool.
Add the path to the script in »Program«, any options in »Arguments« and the path where the script is called from in »Working directory«. The fields accept placeholders, called macros. See button »Insert Macro« for a complete list of a all available placeholders.
/usr/local/bin/php-cs-fixerfor the program,
--verbose fix $FileDir$/$FileName$ --rules=@PSR2as argument and
$ProjectFileDir$as working directory.
Select »Editor menu« to add the starter to the context menu in a file. The default group for the tool is called »External Tools«, just type any other name into the field to create a new group. I have different groups for globally installed tools, scripts for individual projects, and a duplicated set of my most used starters.
You could add a group which allows running the code style fixer with different levels. To change indentation type and line endings only use
--verbose fix $FileDir$/$FileName$ --rules=line_ending,indentation_typeas argument of one tool starter, or to fix all PHP files in the project at once use
--verbose fix $ProjectFileDir$ --rules=@PSR2for another.
- Optionally assign a keyboard shortcut. Open »Settings«, toogle »Keymap«, search for »External Tools« and click »Add Keyboard Shortcut«.
Done. You can now run the tool using the context menu and the designated group (“External tools” by default, but as said above you may change this), or with a custom key.