01-05-2021



  1. Phpstan Phpstorm
  2. Phpstan Phpstorm Docker
  3. Phpstan For Phpstorm
  4. Phpstorm Phpstan No Rules Detected

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.

Compatible with IntelliJ IDEA, PhpStorm. Provide additional support for PHPStan, Psalm and Generics related PHP features!!! Work in progress!!! You can add PHPStan or Psalm as your dev-dependency in composer.json. Near the corresponding line, you’ll see a wrench icon that will open the tool settings. PhpStorm will detect the path to the tool binary, though you can also specify it manually. Class-string parameter inspection. Array-Types: Object-like arrays. Inspection for readonly access on property based on @psalm-readonly and @psalm-immutable tag. Class-string template return type detection. PHPStan and Psalm quality tool support to show report result directly in IDE. PHPStorm will probably complain about file sync being slow when you start it - that's because we did not install inotify yet, which PHPStorm can utilize. To do so, type: sudo apt-get install inotify-tools. PHPStan plugin for PhpStorm This plugin adds dedicated functionality for PHPStanto PhpStorm. The plugin is under active development and will be bundled into PhpStorm.

1. When PHPStorm Imports the Wrong Short Class

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:

Docker

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

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.

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.

Phpstorm

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?

Phpstan PhpstormPhpstorm


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:

Phpstan Phpstorm

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.

That's all for today.


Happy coding!

Releases

PhpStorm 2020.1.3 is now available!

Phpstan Phpstorm

This a bug-fix update for PhpStorm 2020.1.

Phpstan Phpstorm Docker

Notable changes:

  • Fixed: Subpixel(LCD) text is slow with UI font in 2020.1, switching to greyscale antialiasing helps (IDEA-238301 +40)
  • Fixed: Shorten tab names in DataGrid with placement on the right/left (DBE-8336 +11)
  • Fixed: Trait collision false positive in case one trait uses another and overrides the method (WI-53171 +9)
  • Fixed: Database – Adding datasource as DDL – adding whole folders besides files only (DBE-5756 +4)
  • Fixed: UI freeze: com.intellij.openapi.module.impl.ModuleGroupersKt.splitByDotsJoiningIncorrectIdentifiers (IDEA-237974 +2)
  • Fixed: IDE freeze / crash when large file is truncated (IDEA-243589 +1)
  • Fixed: PHPStorm Freezes – Win10 x64 (WI-53220)

You can find the full list of bug fixes and improvements in the release notes.

Download PhpStorm 2020.1.3 build 201.8538.41 for your platform on the website or click Update in your JetBrains Toolbox App.

Phpstan For Phpstorm

We appreciate you reporting any bugs and feature requests to our Issue Tracker.

Phpstorm Phpstan No Rules Detected

Your JetBrains PhpStorm team
The Drive to Develop