Hidden Gems of PHP Packages: Static Analysis Results Baseliner

Found a typo? Edit me
This post is deprecated since November 2019. Its knowledge is old and should not be used.

PHPStan itself generates the baseline since 0.11.17. Use it instead.

Have you used PHPStan, Psalm or Easy Coding Standard on a very old project and got 10 000+ errors?
Do you wish to skip fixing these 10 000 errors and check new code only?

Yes and yes? You'll love SARB.

I found this package by random clicking on meetups on Friends of PHP and reading program of the meetup. One of them was Dave with his SARB - Static Analysis Results Baseliner tool. What an interesting name. In the Czech, I read SRAB, which means coward... what does such tool do?

What Does SARB Help You With?

"SARB is used to create a baseline of these results. As work on the project progresses SARB can take the latest static analysis results, removes those issues in the baseline and report the issues raised since the baseline. SARB does this, in conjunction with it, by tracking lines of code between commits."

Such a simple idea with huge application in practice - amazing! What does it mean? Let me show you on a project:

No stress with the legacy you didn't even write, but full focus on the present moment.

How can You use it?

  1. Install it
composer require dave-liddament/sarb --dev
  1. Create a baseline file
# for psalm
vendor/bin/psalm --report reports/psalm-result.json
vendor/bin/sarb create-baseline reports/psalm-result.json reports/sarb_baseline.json psalm-json

# for phpstan
vendor/bin/phpstan analyse --error-format json > reports/phpstan-result.json
vendor/bin/sarb create-baseline reports/phpstan-result.json reports/sarb_baseline.json phpstan-json
  1. Commit the code

  2. Rerun the static analysis on the latest code

# for psalm
vendor/bin/psalm --report reports/psalm-result.json

# for phpstan
vendor/bin/phpstan analyse --error-format json > reports/phpstan-result.json
  1. Use SARB to remove baseline results
# psalm
vendor/bin/sarb remove-baseline-results reports/psalm-result.json reports/sarb_baseline.json reports/issues_since_baseline.json

# phpstan
vendor/bin/sarb remove-baseline-results reports/phpstan-result.json reports/sarb_baseline.json reports/issues_since_baseline.json

That's it!

You can use this also on EasyCodingStandard:

vendor/bin/easy-coding-standard check src --output-format reports/ecs-result.json
vendor/bin/sarb remove-baseline-results reports/ecs-result.json reports/sarb_baseline.json reports/issues_since_baseline.json

The sky is the limit :)

Read more about the tool in Introduction post and in README of the project on Github.

Great job David!

Happy coding!

Have you find this post useful? Do you want more?

Follow me on Twitter, RSS or support me on GitHub Sponsors.