Run scripts & external tools in PhpStorm

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.

  1. Download PHP CS Fixer and install it globally.

  2. In PhpStorm open »Settings«, toogle »Tools«, click »External Tools«, then use the + symbol to add a new external tool.

  3. 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.

    PhpStorm Edit Tool screenshot

    Use /usr/local/bin/php-cs-fixer for the program, --verbose fix $FileDir$/$FileName$ --rules=@PSR2 as argument and $ProjectFileDir$ as working directory.

  4. 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_type as argument of one tool starter, or to fix all PHP files in the project at once use --verbose fix $ProjectFileDir$ --rules=@PSR2 for another.

  5. Optionally assign a keyboard shortcut. Open »Settings«, toogle »Keymap«, search for »External Tools« and click »Add Keyboard Shortcut«.

  6. 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.

    PhpStorm Run Tool screenshot

⌛ Warning! This post is old. The information may be outdated.

No comments on this notepad. If you would like to add something, then dont hesitate to contact me via E-Mail or Twitter.