tomasvotruba / type-coverage
Measure type coverage of your project
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Installs: 2 795 882
Dependents: 112
Suggesters: 0
Security: 0
Stars: 158
Watchers: 6
Forks: 11
Open Issues: 4
Type:phpstan-extension
Requires
- php: ^7.4 || ^8.0
- nette/utils: ^3.2 || ^4.0
- phpstan/phpstan: ^2.0
- dev-main
- 2.0.0
- 1.0.0
- 0.3.1
- 0.3.0
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0.72
- 0.1.0
- 0.0.12
- 0.0.11.72
- 0.0.11
- 0.0.10.72
- 0.0.10
- 0.0.9.72
- 0.0.9
- 0.0.8.72
- 0.0.8
- 0.0.7.72
- 0.0.7
- 0.0.6
- 0.0.5.72
- 0.0.5
- 0.0.4.72
- 0.0.4
- 0.0.3.72
- 0.0.3
- 0.0.2.72
- 0.0.2
- 0.0.1.72
- 0.0.1
- dev-release-0.0.1
- dev-tv-downgrades
This package is auto-updated.
Last update: 2024-12-13 13:19:31 UTC
README
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 4 type possible declarations in total here:
final class ConferenceFactory { const SPEAKER_TAG = 'speaker'; private $talkFactory; public function createConference(array $data) { $talks = $this->talkFactory->create($data); return new Conference($talks); } }
Note: Class constant types require PHP 8.3 to run.
The param type is defined. But the property, return and constant types are missing.
- 1 out of 4 = 25 % coverage
Our code quality is only at one-quarter of its potential. Let's get to 100 %!
final class ConferenceFactory { - public const SPEAKER_TAG = 'speaker'; + public const string SPEAKER_TAG = 'speaker'; - 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+ version in tagged releases.
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 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
Happy coding!