Include phpstan in PhpStorm

You added all your code. All the files are inspected by PhpStorm and are green. You want to commit. But first, you run phpstan, of course. And… 15 errors. Damn, why did I not run this earlier?

Reasons for not running phpstan constantly

  • I have to switch to my shell and type /vendor/bin/phpstan --level max (or composer check, which in our case will run phpstan and other code quality tools)
  • It takes too long. For a large repository, you will wait a two-figure amount of seconds. Us attention-deficit developers are already reading the latest jokes in our fun channel in Slack until this is done.

Ideally, I want PhpStorm to run that constantly in the background and have an error list somewhere where I can just click and jump to the error.

Until the guys at JetBrains have that, I have a solution that is definitly not as nice, but much more accessible.

Add phpstan as external tool

Go to PhpStorm Settings, look for “external tools“.

As argument, use analyse --level max $FilePathRelativeToProjectRoot$.

Working directory should be $ProjectFileDir$.

This will run phpstan only for the currently selected file or directory.

Execute the command

Now, we have the tool set up. So, remain in the settings, go to Keymap and assign a shortcut to launch it:

I chose <ctrl><alt><shift>-S, even though pressing that somehow resembles clawing at your keyboard in rage.

Now, you can select a folder in the project tree and hit your shortcut. The output will be displayed in the Run window.

You still cannot click on the lines and jump to the file, but it is much more accessible and because you only scan the part of the project you actually worked at, it is much faster!

If you have any ideas for improvement, please comment down below!

About the author

People Enabler at CHECK24