Skip to content

Commit

Permalink
Improve error message with exact filled-type count and single decimal…
Browse files Browse the repository at this point in the history
… relatives (#21)
  • Loading branch information
TomasVotruba authored Aug 27, 2023
1 parent 97532b3 commit 65cd7bc
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 40 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"keywords": ["static analysis", "phpstan-extension"],
"require": {
"php": "^8.1",
"phpstan/phpstan": "^1.10.3"
"phpstan/phpstan": "^1.10"
},
"require-dev": {
"phpstan/extension-installer": "^1.3",
"phpunit/phpunit": "^10.1",
"symplify/easy-coding-standard": "^11.3",
"rector/rector": "^0.16",
"tracy/tracy": "^2.9",
"phpunit/phpunit": "^10.3",
"symplify/easy-coding-standard": "^12.0",
"rector/rector": "^0.18",
"tracy/tracy": "^2.10",
"php-parallel-lint/php-parallel-lint": "^1.3",
"tomasvotruba/unused-public": "^0.1.10"
"tomasvotruba/unused-public": "^0.2"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ parameters:
- src
- tests

type_coverage:
return_type: 100
param_type: 100
property_type: 100

checkGenericClassInNonGenericObjectType: false

excludePaths:
Expand Down
6 changes: 0 additions & 6 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\CodingStyle\Rector\ClassConst\VarConstantCommentRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
Expand All @@ -30,9 +28,5 @@
$rectorConfig->skip([
'*/Fixture/*',
'*/Source/*',

VarConstantCommentRector::class => [
__DIR__ . '/src/PublicClassMethodMatcher.php',
],
]);
};
11 changes: 7 additions & 4 deletions src/CollectorDataNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,28 @@
final class CollectorDataNormalizer
{
/**
* @param mixed[] $collectorDataByPath
* @param array<string, array<array{0: int, 1: array<string, int>}>> $collectorDataByPath
*/
public function normalize(array $collectorDataByPath): TypeCountAndMissingTypes
{
$totalCount = 0;
$missingCount = 0;

$missingTypeLinesByFilePath = [];

foreach ($collectorDataByPath as $filePath => $returnSeaLevelData) {
foreach ($returnSeaLevelData as $nestedData) {
foreach ($collectorDataByPath as $filePath => $typeCoverageData) {
foreach ($typeCoverageData as $nestedData) {
$totalCount += $nestedData[0];

$missingCount += count($nestedData[1]);

$missingTypeLinesByFilePath[$filePath] = array_merge(
$missingTypeLinesByFilePath[$filePath] ?? [],
$nestedData[1]
);
}
}

return new TypeCountAndMissingTypes($totalCount, $missingTypeLinesByFilePath);
return new TypeCountAndMissingTypes($totalCount, $missingCount, $missingTypeLinesByFilePath);
}
}
1 change: 0 additions & 1 deletion src/Collectors/ParamTypeDeclarationCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function processNode(Node $node, Scope $scope): ?array

if ($param->type === null) {
$missingTypeLines[] = $param->getLine();
continue;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Formatter/TypeCoverageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function formatErrors(
$errorMessage = sprintf(
$message,
$typeCountAndMissingTypes->getTotalCount(),
$typeCountAndMissingTypes->getFilledCount(),
$typeCoveragePercentage,
$minimalLevel
);
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/ParamTypeCoverageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class ParamTypeCoverageRule implements Rule
/**
* @var string
*/
public const ERROR_MESSAGE = 'Out of %d possible param types, only %d %% actually have it. Add more param types to get over %d %%';
public const ERROR_MESSAGE = 'Out of %d possible param types, only %d - %.1f %% actually have it. Add more param types to get over %d %%';

public function __construct(
private readonly TypeCoverageFormatter $typeCoverageFormatter,
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PropertyTypeCoverageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class PropertyTypeCoverageRule implements Rule
/**
* @var string
*/
public const ERROR_MESSAGE = 'Out of %d possible property types, only %d %% actually have it. Add more property types to get over %d %%';
public const ERROR_MESSAGE = 'Out of %d possible property types, only %d - %.1f %% actually have it. Add more property types to get over %d %%';

public function __construct(
private readonly TypeCoverageFormatter $typeCoverageFormatter,
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/ReturnTypeCoverageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class ReturnTypeCoverageRule implements Rule
/**
* @var string
*/
public const ERROR_MESSAGE = 'Out of %d possible return types, only %d %% actually have it. Add more return types to get over %d %%';
public const ERROR_MESSAGE = 'Out of %d possible return types, only %d - %.1f %% actually have it. Add more return types to get over %d %%';

public function __construct(
private readonly TypeCoverageFormatter $typeCoverageFormatter,
Expand Down
10 changes: 8 additions & 2 deletions src/ValueObject/TypeCountAndMissingTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class TypeCountAndMissingTypes
*/
public function __construct(
private readonly int $totalCount,
private readonly int $missingCount,
private readonly array $missingTypeLinesByFilePath
) {
}
Expand All @@ -20,6 +21,11 @@ public function getTotalCount(): int
return $this->totalCount;
}

public function getFilledCount(): int
{
return $this->totalCount - $this->missingCount;
}

/**
* @return array<string, int[]>
*/
Expand All @@ -28,9 +34,9 @@ public function getMissingTypeLinesByFilePath(): array
return $this->missingTypeLinesByFilePath;
}

public function getCoveragePercentage(): int
public function getCoveragePercentage(): float
{
return (int) (100 * ($this->getTypedCount() / $this->totalCount));
return 100 * ($this->getTypedCount() / $this->totalCount);
}

private function getTypedCount(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class UnknownParamType
{
public function run($name, $age)
public function run(string $name, $age)
{
}

Expand Down
12 changes: 4 additions & 8 deletions tests/Rules/ParamTypeCoverageRule/ParamTypeCoverageRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ public static function provideData(): Iterator
yield [[__DIR__ . '/Fixture/SkipVariadic.php'], []];
yield [[__DIR__ . '/Fixture/SkipCallableParam.php'], []];

$firstErrorMessage = sprintf(ParamTypeCoverageRule::ERROR_MESSAGE, 3, 0, 80);
$secondErrorMessage = sprintf(ParamTypeCoverageRule::ERROR_MESSAGE, 3, 0, 80);
$thirdErrorMessage = sprintf(ParamTypeCoverageRule::ERROR_MESSAGE, 3, 0, 80);
yield [[__DIR__ . '/Fixture/UnknownParamType.php'], [
[$firstErrorMessage, 9],
[$secondErrorMessage, 9],
[$thirdErrorMessage, 13],
]];
$firstErrorMessage = sprintf(ParamTypeCoverageRule::ERROR_MESSAGE, 3, 1, 33.3, 80);
$thirdErrorMessage = sprintf(ParamTypeCoverageRule::ERROR_MESSAGE, 3, 1, 33.3, 80);

yield [[__DIR__ . '/Fixture/UnknownParamType.php'], [[$firstErrorMessage, 9], [$thirdErrorMessage, 13]]];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TomasVotruba\TypeCoverage\PropertyTypeCoverageRule\Fixture;
namespace TomasVotruba\TypeCoverage\Tests\Rules\PropertyTypeCoverageRule\Fixture;

final class SkipCallableProperty
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TomasVotruba\TypeCoverage\PropertyTypeCoverageRule\Fixture;
namespace TomasVotruba\TypeCoverage\Tests\Rules\PropertyTypeCoverageRule\Fixture;

final class SkipKnownPropertyType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TomasVotruba\TypeCoverage\PropertyTypeCoverageRule\Fixture;
namespace TomasVotruba\TypeCoverage\Tests\Rules\PropertyTypeCoverageRule\Fixture;

use TomasVotruba\TypeCoverage\Tests\Rules\PropertyTypeCoverageRule\Source\ParentBlockingClass;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace TomasVotruba\TypeCoverage\PropertyTypeCoverageRule\Fixture;
namespace TomasVotruba\TypeCoverage\Tests\Rules\PropertyTypeCoverageRule\Fixture;

final class SkipResource
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace TomasVotruba\TypeCoverage\PropertyTypeCoverageRule\Fixture;
namespace TomasVotruba\TypeCoverage\Tests\Rules\PropertyTypeCoverageRule\Fixture;

final class UnknownPropertyType
{
public $name;

public $surname;
public string $surname;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static function provideData(): Iterator
yield [[__DIR__ . '/Fixture/SkipResource.php'], []];
yield [[__DIR__ . '/Fixture/SkipParentBlockingProperty.php'], []];

$errorMessage = sprintf(PropertyTypeCoverageRule::ERROR_MESSAGE, 2, 0, 80);
yield [[__DIR__ . '/Fixture/UnknownPropertyType.php'], [[$errorMessage, 9], [$errorMessage, 11]]];
$errorMessage = sprintf(PropertyTypeCoverageRule::ERROR_MESSAGE, 2, 1, 50.0, 80);
yield [[__DIR__ . '/Fixture/UnknownPropertyType.php'], [[$errorMessage, 9]]];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function provideData(): Iterator
yield [[__DIR__ . '/Fixture/SkipKnownReturnType.php', __DIR__ . '/Fixture/SkipAgainKnownReturnType.php'], []];
yield [[__DIR__ . '/Fixture/SkipConstructor.php'], []];

$errorMessage = sprintf(ReturnTypeCoverageRule::ERROR_MESSAGE, 2, 0, 80);
$errorMessage = sprintf(ReturnTypeCoverageRule::ERROR_MESSAGE, 2, 0, 0, 80);
yield [[__DIR__ . '/Fixture/UnknownReturnType.php'], [[$errorMessage, 9], [$errorMessage, 13]]];
}

Expand Down

0 comments on commit 65cd7bc

Please sign in to comment.