🥋
psalm や PHPStanによる型情報の可視化 ~ type coverage
psalmはデフォルトで解析したコードベースに対する型推論の割合を表示します。
- 実行例
➜ laminas-hydrator git:(5.0.x) ✗ ./vendor/bin/psalm --no-progress
------------------------------
No errors found!
------------------------------
203 other issues found.
You can display them with --show-info=true
------------------------------
Psalm can automatically fix 11 of these issues.
Run Psalm again with
--alter --issues=PossiblyUnusedMethod,MissingParamType --dry-run
to see what it can fix.
------------------------------
Checks took 1.06 seconds and used 160.847MB of memory
Psalm was able to infer types for 97.7027% of the codebase
最後の行にて、Psalmはコードベースの97.7027%の型を推測できた。
と表示されます。
-
--stats
オプションをつけることにより、各ファイルでの割合も確認できます。
➜ laminas-hydrator git:(5.0.x) ✗ ./vendor/bin/psalm --no-progress --stats | tail -n 10
95.327% test/Strategy/CollectionStrategyTest.php (5 mixed)
93.636% test/Strategy/DateTimeFormatterStrategyTest.php (7 mixed)
95.833% test/Strategy/DateTimeImmutableFormatterStrategyTest.php (1 mixed)
100.000% test/Strategy/ExplodeStrategyTest.php (0 mixed)
96.032% test/Strategy/HydratorStrategyTest.php (5 mixed)
100.000% test/Strategy/NullableStrategyTest.php (0 mixed)
100.000% test/Strategy/ScalarTypeStrategyTest.php (0 mixed)
92.308% test/Strategy/SerializableStrategyTest.php (1 mixed)
100.000% test/Strategy/StrategyChainTest.php (0 mixed)
- なお、mixed の件数については、
json-summary
オプションなどで確認できます。
➜ laminas-hydrator git:(5.0.x) ✗ ./vendor/bin/psalm --no-progress --output-format=json-summary
{"issue_counts":[],"mixed_expression_count":100,"total_expression_count":4353}
--shepherd
による type coverageの履歴
- 主に OSSプロジェクト向けとなると思いますが、
--shepherd
オプションをつけることにより、https://shepherd.dev/ で型カバレッジの履歴が記録されます
PHPStan type coverage拡張
- PHPStan では Rectorの作者による type-coverage拡張があり、型カバレッジの閾値を設定できるようです
Discussion