Skip to content

Commit

Permalink
add isConstantTypeCoverageEnabled()
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 14, 2024
1 parent 656d9e5 commit 62b05c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ What is the type coverage you ask? We have 4 type possible declarations in total
```php
final class ConferenceFactory
{
const SPEAKER_TAG = 'speaker';

private $talkFactory;
public const SPEAKER_TAG = 'speaker';

public function createConference(array $data)
{
Expand All @@ -31,7 +32,9 @@ final class ConferenceFactory
}
```

The param type is defined, but property and return types are missing.
*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

Expand All @@ -40,12 +43,11 @@ Our code quality is only at one-quarter of its potential. Let's get to 100 %!
```diff
final class ConferenceFactory
{
- private $talkFactory;
+ private TalkFactory $talkFactory;

- 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
Expand Down
9 changes: 9 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public function getRequiredPropertyTypeLevel(): float|int
return $this->parameters['property'] ?? $this->parameters['property_type'];
}

public function isConstantTypeCoverageEnabled(): bool
{
if (PHP_VERSION_ID < 80300) {
return false;
}

return $this->getRequiredConstantTypeLevel() > 0;
}

public function getRequiredConstantTypeLevel(): float|int
{
return $this->parameters['constant'] ?? $this->parameters['constant_type'];
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/ConstantTypeCoverageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function processNode(Node $node, Scope $scope): array
];
}

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

Expand Down

0 comments on commit 62b05c3

Please sign in to comment.