tomasvotruba/type-coverage
Composer 安装命令:
composer require --dev tomasvotruba/type-coverage
包简介
Measure type coverage of your project
README 文档
README
A PHPStan extension, to check and require minimal type coverage of PHP code.
The type coverage rate = total count of defined type declarations / total count of possible type declarations.
E.g. we have 10 methods, but only 7 have defined return type = 70 % return type coverage.
PHPStan uses type declarations to determine the type of variables, properties and other expression. Sometimes it's hard to see what PHPStan errors are the important ones among thousands of others.
Instead of fixing all PHPStan errors at once, we can start with minimal require type coverage.
How to increase type coverage?
Here we have 3 possible type declarations:
- property,
- param
- and return type
final class ConferenceFactory { private $talkFactory; public function createConference(array $data) { $talks = $this->talkFactory->create($data); return new Conference($talks); } }
The param type is defined as array.
1 defined / 3 possible = 33.3 % type coverage
Our code quality is only at one-third of its potential. Let's get to 100 %!
final class ConferenceFactory
{
- private $talkFactory;
+ private TalkFactory $talkFactory;
- public function createConference(array $data)
+ public function createConference(array $data): Conference
{
$talks = $this->talkFactory->create($data);
return new Conference($talks);
}
}
This technique is very simple to start even on legacy project. Also, you're now aware exactly how high coverage your project has.
Install
composer require tomasvotruba/type-coverage --dev
The package is available on PHP 7.2+.
Usage
With PHPStan extension installer, everything is ready to run.
Enable each item on their own:
# phpstan.neon parameters: type_coverage: return: 50 param: 35.5 property: 70 # since PHP 8.3 constant: 85
Measure Strict Declares coverage
Once you've reached 100 % type coverage, make sure your code is strict and uses types:
<?php declare(strict_types=1);
Again, raise level percent by percent in your own pace:
parameters: type_coverage: declare: 40
Full Paths only
If you run PHPStan only on some subpaths that are different from your setup in phpstan.neon, e.g.:
vendor/bin/phpstan analyze src/Controller
This package could show false positives, as classes in the src/Controller could be slightly less typed. This would be spamming whole PHPStan output and make hard to see any other errors you look for.
That's why this package only triggers if there are full paths, e.g.:
vendor/bin/phpstan
Happy coding!
tomasvotruba/type-coverage 适用场景与选型建议
tomasvotruba/type-coverage 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.89M 次下载、GitHub Stars 达 215, 最近一次更新时间为 2022 年 12 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「static analysis」 「phpstan-extension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tomasvotruba/type-coverage 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tomasvotruba/type-coverage 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tomasvotruba/type-coverage 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
Canonical PHPStan rules enforcing war-room doctrine across script-development Laravel territories.
Detect unused public properties, constants and methods in your code
PHPStan extension for detecting parameters that should use SensitiveParameter
Sentiment analysis library for PHP.
统计信息
- 总下载量: 10.89M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 215
- 点击次数: 23
- 依赖项目数: 242
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-19