Type coverage is a metric that measures the percentage of variables, properties, parameters, and return values in your code that have explicitly defined types. It's a key indicator of code quality, especially in languages that support type systems like PHP, TypeScript, and others.
High type coverage offers several benefits:
Type coverage is typically calculated as:
Type Coverage = (Number of Typed Elements / Total Number of Elements) × 100%
Elements include function parameters, return types, property declarations, and variable declarations, depending on the language and tool being used.
In PHP, type coverage can be measured using tools like PHPStan or Psalm. These tools analyze your codebase and report on missing type declarations. With PHP 7.0 and later versions, you can add scalar type hints, return type declarations, and with PHP 7.4, property type declarations.
For PHP projects, OtterWise helps you track type coverage over time to ensure your codebase maintains good typing practices.
Type coverage is related to but distinct from code coverage. While code coverage measures how much of your code is executed by tests, type coverage measures how much of your code has proper type declarations.
Related Concepts: Type coverage is a form of static analysis that helps improve code quality metrics and reduce technical debt. It's particularly valuable in test-driven development workflows and can be enforced through status checks in pull requests.
To learn more about type coverage implementation in PHP, check out our PHP/Laravel type coverage guide and documentation.
With OtterWise, you can track Code Coverage, contributor stats, code health, and much more.