Skip to content

Commit

Permalink
[DX] Allow float value to more precise level bumping, add aliases for…
Browse files Browse the repository at this point in the history
… "param", "return" and "property" keywords (#28)

* bump ecs config

* bump to PHP 8.2

* allow float configuration

* allow floats

* allow floats

* add aliases to param, return and property keywords

* use short aliases
  • Loading branch information
TomasVotruba authored Mar 15, 2024
1 parent 74ec6b4 commit a2f456d
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 64 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Enable each item on their own with simple configuration:
# phpstan.neon
parameters:
type_coverage:
return_type: 50
param_type: 30
property_type: 70
return: 50
param: 35.5
property: 70
```
15 changes: 12 additions & 3 deletions config/extension.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
parametersSchema:
# see https://doc.nette.org/en/schema for configuration
type_coverage: structure([
return_type: int()
param_type: int()
property_type: int()
return_type: anyOf(float(), int())
param_type: anyOf(float(), int())
property_type: anyOf(float(), int())
print_suggestions: bool()
# aliases to avoid typos
return: anyOf(schema(float(), nullable()), schema(int(), nullable()))
param: anyOf(schema(float(), nullable()), schema(int(), nullable()))
property: anyOf(schema(float(), nullable()), schema(int(), nullable()))
])

# default parameters
Expand All @@ -14,6 +19,10 @@ parameters:
property_type: 99
# default, yet deprecated
print_suggestions: true
# aliases
return: null
param: null
property: null

services:
- TomasVotruba\TypeCoverage\Formatter\TypeCoverageFormatter
Expand Down
14 changes: 3 additions & 11 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@
declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);

$ecsConfig->sets([
SetList::COMMON,
SetList::PSR_12,
SetList::CLEAN_CODE,
SetList::SYMPLIFY,
]);
};
return ECSConfig::configure()
->withPaths([__DIR__ . '/src', __DIR__ . '/tests'])
->withPreparedSets(common: true, psr12: true, cleanCode: true, symplify: true);
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ parameters:
- tests

type_coverage:
return_type: 100
return_type: 99.9
param_type: 100
property_type: 100

Expand Down
28 changes: 7 additions & 21 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,16 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$rectorConfig->importNames();

$rectorConfig->sets([
\Rector\PHPUnit\Set\PHPUnitSetList::PHPUNIT_100,
LevelSetList::UP_TO_PHP_81,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
SetList::NAMING,
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
]);

$rectorConfig->skip([
])
->withPhpSets()
->withPreparedSets(deadCode: true, codeQuality: true, codingStyle: true, typeDeclarations: true, privatization: true, naming: true)
->withImportNames(removeUnusedImports: true)
->withSkip([
'*/Fixture/*',
'*/Source/*',
]);
};
16 changes: 8 additions & 8 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@

namespace TomasVotruba\TypeCoverage;

final class Configuration
final readonly class Configuration
{
/**
* @param array<string, mixed> $parameters
*/
public function __construct(
private readonly array $parameters
private array $parameters
) {
}

public function getRequiredPropertyTypeLevel(): int
public function getRequiredPropertyTypeLevel(): float|int
{
return $this->parameters['property_type'];
return $this->parameters['property'] ?? $this->parameters['property_type'];
}

public function getRequiredParamTypeLevel(): int
public function getRequiredParamTypeLevel(): float|int
{
return $this->parameters['param_type'];
return $this->parameters['param'] ?? $this->parameters['param_type'];
}

public function getRequiredReturnTypeLevel(): int
public function getRequiredReturnTypeLevel(): float|int
{
return $this->parameters['return_type'];
return $this->parameters['return'] ?? $this->parameters['return_type'];
}
}
2 changes: 1 addition & 1 deletion src/Formatter/TypeCoverageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class TypeCoverageFormatter
*/
public function formatErrors(
string $message,
int $minimalLevel,
float|int $minimalLevel,
TypeCountAndMissingTypes $typeCountAndMissingTypes
): array {
if ($typeCountAndMissingTypes->getTotalCount() === 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/Rules/ParamTypeCoverageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*
* @implements Rule<CollectedDataNode>
*/
final class ParamTypeCoverageRule implements Rule
final readonly class ParamTypeCoverageRule implements Rule
{
/**
* @var string
*/
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,
private readonly Configuration $configuration,
private readonly CollectorDataNormalizer $collectorDataNormalizer,
private TypeCoverageFormatter $typeCoverageFormatter,
private Configuration $configuration,
private CollectorDataNormalizer $collectorDataNormalizer,
) {
}

Expand Down
8 changes: 4 additions & 4 deletions src/Rules/PropertyTypeCoverageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*
* @implements Rule<CollectedDataNode>
*/
final class PropertyTypeCoverageRule implements Rule
final readonly class PropertyTypeCoverageRule implements Rule
{
/**
* @var string
*/
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,
private readonly Configuration $configuration,
private readonly CollectorDataNormalizer $collectorDataNormalizer,
private TypeCoverageFormatter $typeCoverageFormatter,
private Configuration $configuration,
private CollectorDataNormalizer $collectorDataNormalizer,
) {
}

Expand Down
8 changes: 4 additions & 4 deletions src/Rules/ReturnTypeCoverageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*
* @implements Rule<CollectedDataNode>
*/
final class ReturnTypeCoverageRule implements Rule
final readonly class ReturnTypeCoverageRule implements Rule
{
/**
* @var string
*/
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,
private readonly Configuration $configuration,
private readonly CollectorDataNormalizer $collectorDataNormalizer,
private TypeCoverageFormatter $typeCoverageFormatter,
private Configuration $configuration,
private CollectorDataNormalizer $collectorDataNormalizer,
) {
}

Expand Down
8 changes: 4 additions & 4 deletions src/ValueObject/TypeCountAndMissingTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace TomasVotruba\TypeCoverage\ValueObject;

final class TypeCountAndMissingTypes
final readonly class TypeCountAndMissingTypes
{
/**
* @param array<string, int[]> $missingTypeLinesByFilePath
*/
public function __construct(
private readonly int $totalCount,
private readonly int $missingCount,
private readonly array $missingTypeLinesByFilePath
private int $totalCount,
private int $missingCount,
private array $missingTypeLinesByFilePath
) {
}

Expand Down

0 comments on commit a2f456d

Please sign in to comment.