Phpstan Phpstorm



Following PHPStan rules saves us from pushing bugs to GitHub. They also save us precious time in code-reviews commenting obvious errors - that are easy to miss in huge pull-requests. They allow us not to think about code details but about business logic or architecture design.

  • PhpStorm + PhpStan watcher. PhpStorm - Settings - Tools - File Watchers (create new) File type: PHP; Scope: Project files; Program (wherever your phpstan is located.
  • PhpStorm comes with support for popular static analysis tools and allows using them as first-class tools for highlighting code issues, as well as support for Psalm annotations and types. Xdebug 3 The major update of the Xdebug PHP debugger extension, Xdebug 3, is supported in PhpStorm.

1. When PHPStorm Imports the Wrong Short Class

PHPStan is a static code analyzer for PHP which can reduce the amount of bugs in a project and make it more maintainable in the long run. With the latest release of PHPStorm 2020.3 EAP, support for PHPStan is provided directly by PHPStorm - no plugins necessary.

Rector is using php-parser and it's nodes, e.g. PhpParserNodeExprVariable.

A Rector rule that should change $something would look like this:

Now look at the code again, and try to find the bug.

Found it!

There is more than 2 option actually:

PHPStorm has no idea which class you want to use. When we code, we don't have time or thought about which imports are right. 99 % it's the first one. Of course, PHPStorm can learn from our manual choices and prefer the most selected one. But that means every contributor has to teach their PHPStorm, to avoid these bugs. That's non-sense.

Instead, we added known miss-types that appeared in pull-requests to PHPStan checks. Not only the wrong type but neatly with a suggestion for the right node class.

The PreferredClassRule works in 100 % of our cases. We never had to use the left type in our code.

2. From Class to its Test and Back Again

When we work with Rector, we either create new rules or fix bug in existing rule. To fix a bug, we add failing test fixture, then we switch to the rule class and fixed the bug.Add a test fixture, fix it... Add a test fixture, fix it...

Phpstan Phpstorm

That means constant jumping between rule and its tests. To save cognitive overload, we add a @see annotation above every rule class:

This way we avoid a long search for the correct class and just jump right to it with one click.

Does every new rule has this annotation? No need to bother each other during code-reviews, because PHPStan has our back:


An unintended side effect of the SeeAnnotationToTestRule is that every Rector rule is tested.

Do you want to add this PHPStan rule yourself but don't like hundreds of PHPStan errors in your CI?Use this Rector rule to complete @see annotations for you.

Phpstan Phpstorm

3. Only one Optional Method

Rector test cases can provide configuration in 3 different methods:

  • only Rector class in getRectorClass() method
  • Rector class with configuration in getRectorsWithConfiguration() method
  • full path to config.php with services in provideConfigFileInfo() method

At least that was the idea. In reality, it was possible to mix these methods:

It didn't cause any runtime troubles, but it was a code smell. To avoid more broken windows, we've added a custom PHPStan rule.

This OnlyOneClassMethodRule checks classes of specific type(s), and makes sure there is exactly 1 method used from defined list:

4. Avoid Known Types Re-Checks

This rule is not configurable, but it saved us so much duplicated work we have to mention it.

Look at the following code. What would you improve type-wise?


What about this?

Yes, why would you check MethodCall that it's MethodCall again? It might be evident in the post example with ten lines and nothing else. But in real life, we can easily miss it in any pull-request of 30+ lines.Now PHPStan has again our back with the CheckTypehintCallerTypeRule:

Thanks to @samsonasik for contributing and further improving this last rule. It's a real time-saver.


You can find all mentioned rules in symplify/phpstan-rules. Get them, use them. Your code will thank you.

Phpstan For Phpstorm

That's all for today.

Phpstan Phpstorm Docker


Happy coding!