oliverdavies.uk/content/node.01893784-8f45-4466-8586-17df23e7e4b5.yml

116 lines
5.1 KiB
YAML

uuid:
- value: 01893784-8f45-4466-8586-17df23e7e4b5
langcode:
- value: en
type:
- target_id: daily_email
target_type: node_type
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
revision_timestamp:
- value: '2025-05-11T09:00:00+00:00'
revision_uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
revision_log: { }
status:
- value: true
uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
title:
- value: "What's the correct way to add PHPStan to an existing codebase?"
created:
- value: '2025-03-16T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:00+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2025/03/16/what-s-the-correct-way-to-add-phpstan-to-an-existing-codebase
langcode: en
body:
- value: |
<p>PHPStan is a static analysis tool for PHP.</p>
<p>It finds potential issues in PHP code without needing to run it, so Developers can find and resolve potential issues sooner.</p>
<p>I use it on all my projects including existing ones I've inherited.</p>
<p>But how can you add a static analysis tool to a codebase without getting a lot of errors from the existing code?</p>
<p>PHPStan has different levels of strictness.</p>
<p>Level 0 is the least strict and each level adds more rules and strictness, resulting in more errors.</p>
<p>Most of the time, people will start by running PHPStan on level 0, fixing any errors and committing the changes.</p>
<p>Then repeat the process as many times as needed until you reach the level you want to achieve.</p>
<p>I don't think this is the right approach.</p>
<p>This could mean that you need to edit the same files multiple times as you work through the levels.</p>
<p>There's also a period of time where Developers can still write suboptimal code whilst you work your way up to your desired level.</p>
<p>Another approach is to use a feature of PHPStan called the baseline.</p>
<p>The baseline is a way of capturing and saving all the existing errors up to the selected level so they are no longer reported.</p>
<p>If you did this for an existing project, it would return no errors as everything would be included in the baseline.</p>
<p>Once you decide what level you want your project to run, you can start as soon as the baseline is generated and without needing to change files multiple times.</p>
<p>Instead of spending time working through the levels one at a time, commit some time to pruning the baseline and reducing the errors in it.</p>
<p>This I think is a better approach and how I add PHPStan to existing codebases.</p>
<p>To learn more about static analysis and PHPStan, listen to <a href="/podcast/22-dave-liddament">episode 22 of the Beyond Blocks podcast</a> with Dave Liddament.</p>
format: full_html
processed: |
<p>PHPStan is a static analysis tool for PHP.</p>
<p>It finds potential issues in PHP code without needing to run it, so Developers can find and resolve potential issues sooner.</p>
<p>I use it on all my projects including existing ones I've inherited.</p>
<p>But how can you add a static analysis tool to a codebase without getting a lot of errors from the existing code?</p>
<p>PHPStan has different levels of strictness.</p>
<p>Level 0 is the least strict and each level adds more rules and strictness, resulting in more errors.</p>
<p>Most of the time, people will start by running PHPStan on level 0, fixing any errors and committing the changes.</p>
<p>Then repeat the process as many times as needed until you reach the level you want to achieve.</p>
<p>I don't think this is the right approach.</p>
<p>This could mean that you need to edit the same files multiple times as you work through the levels.</p>
<p>There's also a period of time where Developers can still write suboptimal code whilst you work your way up to your desired level.</p>
<p>Another approach is to use a feature of PHPStan called the baseline.</p>
<p>The baseline is a way of capturing and saving all the existing errors up to the selected level so they are no longer reported.</p>
<p>If you did this for an existing project, it would return no errors as everything would be included in the baseline.</p>
<p>Once you decide what level you want your project to run, you can start as soon as the baseline is generated and without needing to change files multiple times.</p>
<p>Instead of spending time working through the levels one at a time, commit some time to pruning the baseline and reducing the errors in it.</p>
<p>This I think is a better approach and how I add PHPStan to existing codebases.</p>
<p>To learn more about static analysis and PHPStan, listen to <a href="http://default/podcast/22-dave-liddament">episode 22 of the Beyond Blocks podcast</a> with Dave Liddament.</p>
summary: null
field_daily_email_cta: { }