Skip to content

Commit

Permalink
Bump tomasvotruba/type-coverage from 0.2.8 to 0.3.0 (#711)
Browse files Browse the repository at this point in the history
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Markus Staab <[email protected]>
  • Loading branch information
dependabot[bot] and staabm authored May 24, 2024
1 parent 9a754e7 commit b915b69
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 43 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"thecodingmachine/phpstan-safe-rule": "1.2",
"thecodingmachine/safe": "1.3.3",
"tomasvotruba/cognitive-complexity": "0.2.3",
"tomasvotruba/type-coverage": "0.2.8",
"tomasvotruba/type-coverage": "0.3.0",
"tomasvotruba/unused-public": "0.3.9"
},
"autoload": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -1697,25 +1697,25 @@
},
{
"name": "tomasvotruba/type-coverage",
"version": "0.2.8",
"version_normalized": "0.2.8.0",
"version": "0.3.0",
"version_normalized": "0.3.0.0",
"source": {
"type": "git",
"url": "https://github.com/TomasVotruba/type-coverage.git",
"reference": "ab4f0506f8df8c6418ec877e356c0efd2c50ed6b"
"reference": "efd97d9553fe2d2c21f08d9bd3d3af63564f07ff"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/TomasVotruba/type-coverage/zipball/ab4f0506f8df8c6418ec877e356c0efd2c50ed6b",
"reference": "ab4f0506f8df8c6418ec877e356c0efd2c50ed6b",
"url": "https://api.github.com/repos/TomasVotruba/type-coverage/zipball/efd97d9553fe2d2c21f08d9bd3d3af63564f07ff",
"reference": "efd97d9553fe2d2c21f08d9bd3d3af63564f07ff",
"shasum": ""
},
"require": {
"nette/utils": "^3.2 || ^4.0",
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.9.3"
},
"time": "2024-04-26T13:56:40+00:00",
"time": "2024-05-20T00:46:47+00:00",
"type": "phpstan-extension",
"extra": {
"phpstan": {
Expand All @@ -1741,7 +1741,7 @@
],
"support": {
"issues": "https://github.com/TomasVotruba/type-coverage/issues",
"source": "https://github.com/TomasVotruba/type-coverage/tree/0.2.8"
"source": "https://github.com/TomasVotruba/type-coverage/tree/0.3.0"
},
"funding": [
{
Expand Down
10 changes: 5 additions & 5 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => '__root__',
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'f216eb4ec30e6e9e49b38e7c1e891ce10fc12918',
'reference' => 'd23723d8def1ad4473333a37569bbf4a3f0443d1',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'__root__' => array(
'pretty_version' => 'dev-main',
'version' => 'dev-main',
'reference' => 'f216eb4ec30e6e9e49b38e7c1e891ce10fc12918',
'reference' => 'd23723d8def1ad4473333a37569bbf4a3f0443d1',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down Expand Up @@ -248,9 +248,9 @@
'dev_requirement' => false,
),
'tomasvotruba/type-coverage' => array(
'pretty_version' => '0.2.8',
'version' => '0.2.8.0',
'reference' => 'ab4f0506f8df8c6418ec877e356c0efd2c50ed6b',
'pretty_version' => '0.3.0',
'version' => '0.3.0.0',
'reference' => 'efd97d9553fe2d2c21f08d9bd3d3af63564f07ff',
'type' => 'phpstan-extension',
'install_path' => __DIR__ . '/../tomasvotruba/type-coverage',
'aliases' => array(),
Expand Down
30 changes: 25 additions & 5 deletions vendor/tomasvotruba/type-coverage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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 %?
Our code has only one third quality it could have. Let's get to 100 %!

```diff
final class ConferenceFactory
Expand All @@ -52,7 +52,7 @@ How do we get to the 100 %?
}
```

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.
This technique is very simple to start even on legacy project. Also, you're now aware exactly how high coverage your project has.

<br>

Expand All @@ -70,15 +70,35 @@ The package is available on PHP 7.2+ version in tagged releases.

With [PHPStan extension installer](https://github.com/phpstan/extension-installer), everything is ready to run.

Enable each item on their own with simple configuration:
Enable each item on their own:

```neon
```yaml
# phpstan.neon
parameters:
type_coverage:
return: 50
param: 35.5
property: 70
# also, how many files has declare strict types
```
## Measure Strict Declares coverage
Once you've reached 100 % type coverage, make use [your code is strict and uses types](https://tomasvotruba.com/blog/how-adding-type-declarations-makes-your-code-dangerous):
```php
<?php

declare(strict_types=1);
```

Again, raise level percent by percent in your own pace:

```yaml
parameters:
type_coverage:
declare: 40
```
<br>
Happy coding!
5 changes: 5 additions & 0 deletions vendor/tomasvotruba/type-coverage/config/extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ parametersSchema:
return: anyOf(schema(float(), nullable()), schema(int(), nullable()))
param: anyOf(schema(float(), nullable()), schema(int(), nullable()))
property: anyOf(schema(float(), nullable()), schema(int(), nullable()))

# measure
measure: bool()
])

# default parameters
Expand All @@ -28,6 +31,8 @@ parameters:
param: null
property: null

measure: false

services:
- TomasVotruba\TypeCoverage\Formatter\TypeCoverageFormatter
- TomasVotruba\TypeCoverage\CollectorDataNormalizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace TomasVotruba\TypeCoverage\Collectors;

use PhpParser\Node;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Declare_;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
Expand Down Expand Up @@ -35,7 +36,7 @@ public function processNode(Node $node, Scope $scope): bool
}

if (
! $declare->value instanceof Node\Scalar\LNumber
! $declare->value instanceof LNumber
|| $declare->value->value !== 1
) {
return false;
Expand Down
5 changes: 5 additions & 0 deletions vendor/tomasvotruba/type-coverage/src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public function getRequiredDeclareLevel()
{
return $this->parameters['declare'];
}

public function showOnlyMeasure(): bool
{
return $this->parameters['measure'];
}
}
30 changes: 20 additions & 10 deletions vendor/tomasvotruba/type-coverage/src/Rules/DeclareCoverageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,9 @@ public function processNode(Node $node, Scope $scope): array
{
$requiredDeclareLevel = $this->configuration->getRequiredDeclareLevel();

// not enabled
if ($requiredDeclareLevel === 0) {
return [];
}

$declareCollector = $node->get(DeclareCollector::class);
$totalPossibleDeclares = count($declareCollector);

// nothing to handle
if ($totalPossibleDeclares === 0) {
return [];
}

$coveredDeclares = 0;
$notCoveredDeclareFilePaths = [];

Expand All @@ -78,6 +68,26 @@ public function processNode(Node $node, Scope $scope): array

$declareCoverage = ($coveredDeclares / $totalPossibleDeclares) * 100;

if ($this->configuration->showOnlyMeasure()) {
return [
sprintf(
'Strict declares coverage is %.1f %% out of %d possible',
$declareCoverage,
$totalPossibleDeclares
),
];
}

// not enabled
if ($requiredDeclareLevel === 0) {
return [];
}

// nothing to handle
if ($totalPossibleDeclares === 0) {
return [];
}

// we meet the limit, all good
if ($declareCoverage >= $requiredDeclareLevel) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,24 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if ($this->configuration->getRequiredParamTypeLevel() === 0) {
return [];
}

$paramTypeDeclarationCollector = $node->get(ParamTypeDeclarationCollector::class);

$typeCountAndMissingTypes = $this->collectorDataNormalizer->normalize($paramTypeDeclarationCollector);

if ($this->configuration->showOnlyMeasure()) {
return [
sprintf(
'Param type coverage is %.1f %% out of %d possible',
$typeCountAndMissingTypes->getCoveragePercentage(),
$typeCountAndMissingTypes->getTotalCount()
),
];
}

if ($this->configuration->getRequiredParamTypeLevel() === 0) {
return [];
}

return $this->typeCoverageFormatter->formatErrors(
self::ERROR_MESSAGE,
$this->configuration->getRequiredParamTypeLevel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,23 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
$propertyTypeDeclarationCollector = $node->get(PropertyTypeDeclarationCollector::class);
$typeCountAndMissingTypes = $this->collectorDataNormalizer->normalize($propertyTypeDeclarationCollector);

if ($this->configuration->showOnlyMeasure()) {
return [
sprintf(
'Property type coverage is %.1f %% out of %d possible',
$typeCountAndMissingTypes->getCoveragePercentage(),
$typeCountAndMissingTypes->getTotalCount()
),
];
}

if ($this->configuration->getRequiredPropertyTypeLevel() === 0) {
return [];
}

$propertyTypeDeclarationCollector = $node->get(PropertyTypeDeclarationCollector::class);
$typeCountAndMissingTypes = $this->collectorDataNormalizer->normalize($propertyTypeDeclarationCollector);

return $this->typeCoverageFormatter->formatErrors(
self::ERROR_MESSAGE,
$this->configuration->getRequiredPropertyTypeLevel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ public function processNode(Node $node, Scope $scope): array
$returnSeaLevelDataByFilePath = $node->get(ReturnTypeDeclarationCollector::class);
$typeCountAndMissingTypes = $this->collectorDataNormalizer->normalize($returnSeaLevelDataByFilePath);

if ($this->configuration->showOnlyMeasure()) {
return [
sprintf(
'Return type coverage is %.1f %% out of %d possible',
$typeCountAndMissingTypes->getCoveragePercentage(),
$typeCountAndMissingTypes->getTotalCount()
),
];
}

if ($this->configuration->getRequiredReturnTypeLevel() === 0) {
return [];
}

return $this->typeCoverageFormatter->formatErrors(
self::ERROR_MESSAGE,
$this->configuration->getRequiredReturnTypeLevel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function getMissingTypeLinesByFilePath(): array

public function getCoveragePercentage(): float
{
if ($this->totalCount === 0) {
return 100.0;
}

$relative = 100 * ($this->getTypedCount() / $this->totalCount);

// round down with one decimal, to make error message clear that required value is not reached yet
Expand Down

0 comments on commit b915b69

Please sign in to comment.