53 lines
1.8 KiB
Markdown
53 lines
1.8 KiB
Markdown
---
|
|
title: >
|
|
Which PHPStan level should you use?
|
|
pubDate: 2023-11-28
|
|
permalink: >-
|
|
archive/2023/11/28/which-phpstan-level-should-you-use
|
|
tags:
|
|
- software-development
|
|
- clean-code
|
|
- static-analysis
|
|
- phpstan
|
|
---
|
|
|
|
Which PHPStan level should you use?
|
|
|
|
PHPStan has different levels.
|
|
|
|
When you run it on your code, the errors you see will depend on what level you set.
|
|
|
|
[In yesterday's email][yesterday], the first example code block didn't generate an error until level 5 was used.
|
|
|
|
So, how do you know which level to use?
|
|
|
|
## For greenfield projects
|
|
|
|
For new (greenfield) code, install and configure PHPStan before you write any code and have and have it run automatically as part of a CI pipeline.
|
|
|
|
If you work on a team, speak with the other members and decide how strict you want PHPStan to be.
|
|
|
|
Read the rule levels and decide which are the most valuable for your team.
|
|
|
|
If you haven't used PHPStan or static analysis before, maybe start with a lower level.
|
|
|
|
For me, typehints and return type checking are a must, though I like to use as high a level as possible.
|
|
|
|
The more information you can provide to PHPStan, the more it will understand your code, give better results and be more likely to find potential bugs or issues.
|
|
|
|
## For brownfield projects
|
|
|
|
For existing (brownfield) code, start at the lowest level, which will give you the least number of errors.
|
|
|
|
Fix any errors, exclude any rules you want to ignore or generate a baseline containing any existing errors.
|
|
|
|
If you like, increase the level and repeat the process.
|
|
|
|
Keep increasing the level as long as you feel comfortable, and PHPStan gives you meaningful results.
|
|
|
|
Again, if you haven't used PHPStan or static analysis before, maybe stick with a lower level.
|
|
|
|
If you start with a lower level, you can increase it later.
|
|
|
|
[yesterday]: https://www.oliverdavies.uk/archive/2023/11/27/finding-the-best-test-base
|