Using External Tools in PhpStorm
In a recent ticket on YouTrack (our issue tracker), our community member Jared Morgan documented how you can use the “External Tools” feature of PhpStorm to run everyday tools from your toolchain from a custom key binding, without ever leaving your IDE.
Jared wanted to use phpDocumentor, so let’s take a look at how we can configure and run that, plus PHP Copy/Paste Detector.
phpDocumentor is a tool that looks at the annotations in your codebase and generates API documentation in HTML for you. It’s a useful tool written by Mike van Riel (thanks, Mike!) and is something I’ve often used during my contributions to Zend Framework.
Assuming you have phpDocumentor installed as part of your composer dependencies for the project, configuring phpDocumentor as an external tool in PhpStorm is relatively straightforward. First, we need to manage our external tools; you can find it in the standard Preferences window under Tools -> External Tools. We can click the + to add a new external tool, and then name it “Generate Documentation” or similar.
The command I would use from the terminal to generate the docs for Zend\Stratigility (the package I chose to generate API docs for) is:
vendor/bin/phpdoc -d src/ -t doc/API/
This command, when run from the root of my Zend\Stratigility clone, will run the `phpdoc` tool on the `src/` directory (the PSR-4 namespace root of this project), and put the docs into the `doc/API/` folder. To configure this in the External Tools settings pane, it would look like this:
Basically, all we need to do is to tell PhpStorm where to find the executable for `phpdoc`, which parameters to pass, and finally to run as the project root as working directory.
Next, we want to bind a keyboard shortcut to run this external tool. To do this, navigate to Keymap in the preference pane, and then select External Tools and Generate Documentation (or whatever you named your external tool configuration). Now you can bind a key to generate the phpDocumentor documentation just like you would with any of the built-in features.
PHP Copy/Paste Detector
Another useful tool to run over your codebase is Sebastian Bergmann’s PHP Copy/Paste Detector. In this case, we’ve again installed the tool using composer, and can configure it in a very similar way. To run the tool, I would just use the following command in my terminal:
To add this as a tool so we can trigger it without needing to leave PhpStorm, we’d just add the following configuration as an external tool:
Notice in this instance I have used relative paths rather than the full paths, and this works just fine too.
I don’t intend to run this tool often, so instead of using a keybinding, I can just run this directly from the Tools menu of the IDE. Under the External Tools option, you can see that, like any bundled feature, our external tools can be run, and we even get prompted with the shortcut we’ve bound just in case we forget.
Try adding your favorite tools to PhpStorm, and let me know which tools you find useful to be integrated into the IDE.
— Gary and the PhpStorm Team