tomasvotruba/type-coverage

Measure type coverage of your project

Installs: 1 265 354

Dependents: 42

Suggesters: 0

Security: 0

Stars: 100

Watchers: 5

Forks: 8

Open Issues: 3

Type:phpstan-extension

0.2.7 2024-04-16 13:08 UTC

README


AI abilities sea level rising... as way to rise type coverage for class elements

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.


What is the type coverage you ask? We have 3 type possible declarations in total here:

final class ConferenceFactory
{
    private $talkFactory;

    public function createConference(array $data)
    {
        $talks = $this->talkFactory->create($data);

        return new Conference($talks);
    }
}

The param type is defined, but property and return types are missing.

  • 1 out of 3 = 33 % coverage

How do we get to the 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 and useful to start with even on legacy project. You also know, how high coverage your project has right now.


Install

composer require tomasvotruba/type-coverage --dev

The package is available on PHP 7.2+ version in tagged releases.


Usage

With PHPStan extension installer, everything is ready to run.

Enable each item on their own with simple configuration:

# phpstan.neon
parameters:
    type_coverage:
        return: 50
        param: 35.5
        property: 70