Skip to content

Commit e53e34b

Browse files
committed
Warn about incomplete versions in #[RequiresPhp]
1 parent d42f8a2 commit e53e34b

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,28 @@ class AttributeRequiresPhpVersionRule implements Rule
3333
private TestMethodsHelper $testMethodsHelper;
3434

3535
/**
36-
* When phpstan-deprecation-rules is installed, it reports deprecated usages.
36+
* When phpstan-deprecation-rules is installed, rule reports deprecated usages.
3737
*/
3838
private bool $deprecationRulesInstalled;
3939

40+
/**
41+
* Whether warnings about incomplete versions are allowed to be emitted
42+
*/
43+
private bool $warnAboutIncompleteVersion;
44+
4045
public function __construct(
4146
PHPUnitVersion $PHPUnitVersion,
4247
TestMethodsHelper $testMethodsHelper,
4348
bool $deprecationRulesInstalled,
44-
PhpVersion $phpVersion
49+
PhpVersion $phpVersion,
50+
bool $warnAboutIncompleteVersion = true
4551
)
4652
{
4753
$this->PHPUnitVersion = $PHPUnitVersion;
4854
$this->testMethodsHelper = $testMethodsHelper;
4955
$this->deprecationRulesInstalled = $deprecationRulesInstalled;
50-
5156
$this->phpstanPhpVersion = new Version($phpVersion->getVersionString());
57+
$this->warnAboutIncompleteVersion = $warnAboutIncompleteVersion;
5258
}
5359

5460
public function getNodeType(): string
@@ -80,6 +86,15 @@ public function processNode(Node $node, Scope $scope): array
8086
// see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php
8187

8288
$versionRequirement = $args[0];
89+
90+
if ($this->warnAboutIncompleteVersion($versionRequirement)) {
91+
$errors[] = RuleErrorBuilder::message(
92+
sprintf('Version requirement is incomplete.'),
93+
)
94+
->identifier('phpunit.incompletePhpVersion')
95+
->build();
96+
}
97+
8398
if (
8499
!is_numeric($versionRequirement)
85100
) {
@@ -139,4 +154,18 @@ public function processNode(Node $node, Scope $scope): array
139154
return $errors;
140155
}
141156

157+
// see https://github.com/sebastianbergmann/phpunit/issues/6451
158+
function warnAboutIncompleteVersion(string $versionRequirement): bool
159+
{
160+
if (!$this->warnAboutIncompleteVersion) {
161+
return false;
162+
}
163+
164+
if (!$this->PHPUnitVersion->warnsAboutIncompleteVersion()->yes()) {
165+
return false;
166+
}
167+
168+
return substr_count($versionRequirement, '.') !== 2;
169+
}
170+
142171
}

src/Rules/PHPUnit/PHPUnitVersion.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function deprecatesPhpversionAttributeWithoutOperator(): TrinaryLogic
6262
return $this->minVersion(12, 4);
6363
}
6464

65+
public function warnsAboutIncompleteVersion(): TrinaryLogic
66+
{
67+
return $this->minVersion(12, 5);
68+
}
69+
6570
private function minVersion(int $major, int $minor): TrinaryLogic
6671
{
6772
if ($this->majorVersion === null || $this->minorVersion === null) {

tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ final class AttributeRequiresPhpVersionRuleTest extends RuleTestCase
2121

2222
private bool $deprecationRulesInstalled = true;
2323

24+
private bool $warnAboutIncompleteVersion = false;
25+
2426
public function testRuleOnPHPUnitUnknown(): void
2527
{
2628
$this->phpunitMajorVersion = null;
@@ -137,6 +139,35 @@ public function testInvalidPhpVersion(): void
137139
]);
138140
}
139141

142+
public function testNoWarnAboutIncompleteVersionInOldPhpunit(): void
143+
{
144+
$this->phpunitMajorVersion = 12;
145+
$this->phpunitMinorVersion = 0;
146+
$this->deprecationRulesInstalled = false;
147+
$this->warnAboutIncompleteVersion = true;
148+
149+
$this->analyse([__DIR__ . '/data/requires-php-version.php'], []);
150+
}
151+
152+
public function testWarnAboutIncompleteVersion(): void
153+
{
154+
$this->phpunitMajorVersion = 12;
155+
$this->phpunitMinorVersion = 5;
156+
$this->deprecationRulesInstalled = false;
157+
$this->warnAboutIncompleteVersion = true;
158+
159+
$this->analyse([__DIR__ . '/data/requires-php-version.php'], [
160+
[
161+
'Version requirement is incomplete.',
162+
12,
163+
],
164+
[
165+
'Version requirement is incomplete.',
166+
20,
167+
],
168+
]);
169+
}
170+
140171
protected function getRule(): Rule
141172
{
142173
$phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion);
@@ -149,6 +180,7 @@ protected function getRule(): Rule
149180
),
150181
$this->deprecationRulesInstalled,
151182
new PhpVersion($this->phpVersion),
183+
$this->warnAboutIncompleteVersion
152184
);
153185
}
154186

0 commit comments

Comments
 (0)