When I come to a new project, I want to make a rough idea of what I'm dealing with in a few minutes. I usually check composer.json
and measure the lines.
Then, I'd love to run PHPStan and get a rough idea of the current state. But are there baselines, custom extensions, global ignores, or no PHPStan at all?
PHPStan is a great tool to check your code for static errors. It's also a great way to visualize where we are today and how much work it would take to reach the next level. The problem is that it can only analyze one level at a time—usually, the one defined in the phpstan.neon
file.
Also, even if we switch the level easily with the --level
CLI option, we might get a false positive result: 0 errors.
Why false positive? That's because 400 errors are ignored in either baselines or in phpstan.neon
itself. We'd have to go through this, create a bare phpstan.neon
and run PHPStan again.
However, there are also extensions that add their own errors. When we run the same PHPStan on the same code, we'll get different results based on enabled extensions.
What I need are standardized results so we can:
We need a bare phpstan.neon
that runs, records error count, and then runs again with the next level, from 0 to 1, 1 to 2, etc. Creating phpstan.neon
per project and running PHPStan multiple times is not my favorite activity, so I thought about automating it.
I needed a package you can install anywhere on PHP 7.2+:
composer require tomasvotruba/phpstan-bodyscan --dev
Then you can run it locally in the current project:
vendor/bin/phpstan-bodyscan
Or on the project in a different directory:
vendor/bin/phpstan-bodyscan run ../favorite-project
And then, it would be great to see a simple result table:
+-------+-------------+
| Level | Error count |
+-------+-------------+
| 0 | 0 |
| 1 | 35 |
| 2 | 59 |
| 3 | 93 |
| 4 | 120 |
| 5 | 125 |
| 6 | 253 |
| 7 | 350 |
| 8 | 359 |
+-------+-------------+
That I could copy-paste to GPT and get a simple chart:
So, I made the package and used it for the first project.
Here it is - phpstan-bodyscan.
Happy coding!
Do you learn from my contents or use open-source packages like Rector every day?
Consider supporting it on GitHub Sponsors.
I'd really appreciate it!