Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Type Coverage Laravel Package

tomasvotruba/type-coverage

CLI tool for measuring PHP type coverage. Scans your codebase and reports how much is covered by native types and PHPDoc (params, returns, properties), helping you spot missing types, raise strictness, and improve static analysis readiness.

View on GitHub
Deep Wiki
Context7

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?

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!

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport