dave-liddament/sarb
Composer 安装命令:
composer create-project dave-liddament/sarb
包简介
Provides tools for baselining static analysis results and comparing against that baseline
README 文档
README
Why SARB?
If you've tried to introduce advanced static analysis tools (e.g. Psalm, PHPStan) to legacy projects the tools have probably reported thousands of problems. It's unrealistic to fix all but the most critical ones before continuing development.
SARB is used to create a baseline of these results. As work on the project progresses SARB takes 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 git, by tracking lines of code between commits. Currently, SARB only supports git, but it is possible to add support for other SCMs.
SARB is written in PHP, however it can be used to baseline results for any language and any static analysis tool.
Why not SARB?
SARB should not be used on greenfield projects. If you're lucky enough to work on a greenfield project make sure you fix all problems raised by static analysis as you go along.
Requirements
Currently, SARB only supports projects that use git.
SARB requires PHP >= 8.2 to run. The project being analysed does not need to run PHP 8.2 or even be a PHP project at all.
Installing
You can either add directly to the project you wish to run analysis on:
composer require --dev dave-liddament/sarb
Or you can install SARB globally (e.g. if you want to use it on a non PHP project):
composer global require dave-liddament/sarb
If you install globally make sure the composer bin directory is in your path.
If SARB's dependencies clash with your projects then investigate using composer-bin-plugin.
Using SARB
If you're using version 0.x see the old documentation and how to upgrade.
1. Make sure the current git commit is the one to be used in the baseline
When creating the baseline, SARB needs to know the git commit SHA of the baseline. Make sure your code is in the state you want it to be in for the baseline and that the current commit represents that state.
2. Create the baseline
Run the static analyser of choice and pipe the results into SARB:
E.g. using Psalm's JSON output:
vendor/bin/psalm --output-format=json | vendor/bin/sarb create --input-format="psalm-json" psalm.baseline
This creates a baseline file called psalm.baseline. You'll want to check this in to your repository.
3. Update code and then use SARB to remove baseline results
Continue coding. Then rerun static analyser and pipe results into SARB:
vendor/bin/psalm --output-format=json | vendor/bin/sarb remove psalm.baseline
Running SARB from a global installation
If you are running SARB from a global installation you will need to specify the root of the project (where the .git directory lives).
The above would become:
psalm --output-format=json | sarb create --project-root=/path/to/project/root --input-format="psalm-json" psalm.baseline
Supported tools
To see a list of supported tools and formats use:
vendor/bin/sarb list-static-analysis-tools
How to create and remove baseline for each supported tool:
PHP CodeSniffer
vendor/bin/phpcs src --report=json | vendor/bin/sarb create --input-format="phpcodesniffer-json" phpcs.baseline vendor/bin/phpcs src --report=json | vendor/bin/sarb remove phpcs.baseline
Phan
vendor/bin/phan -m json | vendor/bin/sarb create --input-format="phan-json" phan.baseline vendor/bin/phan -m json | vendor/bin/sarb remove phan.baseline
See notes on relative paths.
Exakat
php exakat.phar report -p <project> -format sarb | vendor/bin/sarb create --input-format="exakat-sarb" exakat.baseline php exakat.phar report -p <project> -format sarb | vendor/bin/sarb remove exakat.baseline
PHPMD
vendor/bin/phpmd src json <ruleset> | vendor/bin/sarb create --input-format="phpmd-json" phpmd.baseline vendor/bin/phpmd src json <ruleset> | vendor/bin/sarb remove phpmd.baseline
Psalm
vendor/bin/psalm --output-format=json | vendor/bin/sarb create --input-format="psalm-json" psalm.baseline vendor/bin/psalm --output-format=json | vendor/bin/sarb remove psalm.baseline
NOTE: Checkout Psalm's built in baseline feature. Learn how it differs from SARB.
PHPStan
vendor/bin/phpstan analyse --error-format=json | vendor/bin/sarb create --input-format="phpstan-json" phpstan.baseline vendor/bin/phpstan analyse --error-format=json | vendor/bin/sarb remove phpstan.baseline
NOTE: Checkout PHPStan's built in baseline feature. Learn how it differs from SARB.
PHP Magic Number Detector
vendor/bin/phpmnd . | vendor/bin/sarb create --input-format="phpmnd" phpmnd.baseline vendor/bin/phpmnd . | vendor/bin/sarb remove phpmnd.baseline
See notes on relative paths.
My tool isn't supported...
That's no problem there are 3 methods to integrate a static analysis tool with SARB.
Output formats
The format for showing issues after the baseline is removed can be specified using --output-format option.
Possible values are: table, text, json or github (for GitHub actions).
Ignoring warnings
Some static analysis tools (e.g. PHP Code Sniffer) classify issues with a severity or either error or warning.
By default, SARB will report all of these. If you wish to ignore warnings you can use the --ignore-warnings option.
E.g.
vendor/bin/phpcs src --report=json | vendor/bin/sarb remove phpcs.baseline --ignore-warnings
SARB with GitHub Actions
If you're using actions/checkout@v2 to check out your code you'll need to add set fetch-depth to 0.
By default checkout only gets that latest state of the code and none of the history.
SARB uses git, which needs the full git history, to track file changes since the baseline.
To get the full history checked out use this:
- uses: actions/checkout@v2
with:
fetch-depth: 0
Also don't forget to use the SARB option --output-format=github.
It will annotate your PR with any issues that have been added since the baseline.
Gradually improving the codebase
In an ideal world SARB should not be required. SARB prevents you from adding new issues to your codebase.
It also provides a --clean-up option when running remove.
Running SARB with this option will pick out 5 random issues that are still in the baseline.
Challenge your team to fix 5 issues in the baseline every day.
Over a working year that'll be 1000 issues gone from the baseline!
Soon you'll be able to ditch SARB for good!
Further Reading
- How SARB works
- Adding support for new static analysis tools / format
- Adding support for SCMs other than git
- How to contribute
- Code of Conduct
- Unified Diff Terminology
- SARB format
- SARB vs other tools with baselining functionality
Authors
dave-liddament/sarb 适用场景与选型建议
dave-liddament/sarb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.6M 次下载、GitHub Stars 达 165, 最近一次更新时间为 2018 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「static analysis」 「baseline」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dave-liddament/sarb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dave-liddament/sarb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dave-liddament/sarb 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
Sentiment analysis library for PHP.
Database Standardization and Analysis Tool for Laravel
Enforce architecture by defining modules with allowed dependencies. Detects forbidden, uncovered, missing and unused module dependencies in PHP projects.
Arc5 provides a set of static classes for the Mvc5 Framework
统计信息
- 总下载量: 1.6M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 165
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-02