Skip to content

Commit f5dc20f

Browse files
authored
Fix #[RequiresPhpunit] version requirement being evaluated against PHP version (#313)
1 parent 1e573d3 commit f5dc20f

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

src/Rules/PHPUnit/AttributeVersionRequirementHelper.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use function is_numeric;
1818
use function preg_match;
1919
use function sprintf;
20+
use function strpos;
2021
use function substr_count;
2122
use function version_compare;
2223

@@ -96,11 +97,18 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
9697
continue;
9798
}
9899

100+
$pharIoVersions = strpos($attr->getName(), 'RequiresPhpunit') !== false
101+
? $this->PHPUnitVersion->getPharIoVersions()
102+
: $phpstanPharIoVersions;
103+
if ($pharIoVersions === []) {
104+
continue;
105+
}
106+
99107
try {
100108
// check composer like version constraints, e.g. ^1 or ~2
101109
$testPhpVersionConstraint = $parser->parse($versionRequirement);
102110

103-
foreach ($phpstanPharIoVersions as $pharIoVersion) {
111+
foreach ($pharIoVersions as $pharIoVersion) {
104112
if ($testPhpVersionConstraint->complies($pharIoVersion)) {
105113
// one of the versions within range matched, check next attribute
106114
continue 2;
@@ -120,7 +128,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
120128

121129
$operator = $matches['operator'] !== '' ? $matches['operator'] : '>=';
122130

123-
foreach ($phpstanPharIoVersions as $pharIoVersion) {
131+
foreach ($pharIoVersions as $pharIoVersion) {
124132
if (version_compare($pharIoVersion->getVersionString(), $matches['version'], $operator)) {
125133
// one of the versions within range matched, check next attribute
126134
continue 2;

src/Rules/PHPUnit/PHPUnitVersion.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace PHPStan\Rules\PHPUnit;
44

5+
use PharIo\Version\Version;
56
use PHPStan\TrinaryLogic;
7+
use function sprintf;
68

79
class PHPUnitVersion
810
{
@@ -17,6 +19,21 @@ public function __construct(?int $majorVersion, ?int $minorVersion)
1719
$this->minorVersion = $minorVersion;
1820
}
1921

22+
/**
23+
* @return array{}|array{Version, Version}
24+
*/
25+
public function getPharIoVersions(): array
26+
{
27+
if ($this->majorVersion === null || $this->minorVersion === null) {
28+
return [];
29+
}
30+
31+
return [
32+
new Version(sprintf('%d.%d.0', $this->majorVersion, $this->minorVersion)),
33+
new Version(sprintf('%d.%d.99', $this->majorVersion, $this->minorVersion)),
34+
];
35+
}
36+
2037
public function supportsDataProviderAttribute(): TrinaryLogic
2138
{
2239
if ($this->majorVersion === null) {

tests/Rules/PHPUnit/data/requires-phpunit-version.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
class TwoDigitVersionA extends TestCase
1111
{
12-
#[RequiresPhpunit('8.0')]
12+
#[RequiresPhpunit('11.0')]
1313
public function testFoo(): void {
1414

1515
}
1616
}
1717

18-
#[RequiresPhpunit('>=8.0')]
18+
#[RequiresPhpunit('>=11.0')]
1919
class TwoDigitVersionB extends TestCase
2020
{
2121
public function testBar(): void {
@@ -25,13 +25,18 @@ public function testBar(): void {
2525

2626
class CorrectRequirement extends TestCase
2727
{
28-
#[RequiresPhpunit('>=8.0.0')]
28+
#[RequiresPhpunit('>=11.0.0')]
2929
public function testBar(): void {
3030

3131
}
32+
33+
#[RequiresPhpunit('^12.0.0')]
34+
public function testBaz(): void {
35+
36+
}
3237
}
3338

34-
#[RequiresPhpunit('>=8.0.0')]
39+
#[RequiresPhpunit('>=11.0.0')]
3540
class CorrectClassRequirement extends TestCase
3641
{
3742
public function testBar(): void {

0 commit comments

Comments
 (0)