PHPackages                             tomasvotruba/type-coverage - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. tomasvotruba/type-coverage

ActivePhpstan-extension[Testing &amp; Quality](/categories/testing)

tomasvotruba/type-coverage
==========================

Measure type coverage of your project

2.1.0(5mo ago)2088.9M—0.9%14[3 issues](https://github.com/TomasVotruba/type-coverage/issues)20MITPHPPHP ^7.4 || ^8.0CI passing

Since Dec 19Pushed 4mo ago5 watchersCompare

[ Source](https://github.com/TomasVotruba/type-coverage)[ Packagist](https://packagist.org/packages/tomasvotruba/type-coverage)[ Fund](https://www.paypal.me/rectorphp)[ GitHub Sponsors](https://github.com/tomasvotruba)[ RSS](/packages/tomasvotruba-type-coverage/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (46)Used By (20)

Require Minimal Type Coverage
=============================

[](#require-minimal-type-coverage)

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?
------------------------------

[](#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
-------

[](#install)

```
composer require tomasvotruba/type-coverage --dev
```

The package is available on PHP 7.2+.

Usage
-----

[](#usage)

With [PHPStan extension installer](https://github.com/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
--------------------------------

[](#measure-strict-declares-coverage)

Once you've reached 100 % type coverage, make sure [your code is strict and uses types](https://tomasvotruba.com/blog/how-adding-type-declarations-makes-your-code-dangerous):

```
