Skip to content

Commit 2ab3331

Browse files
committed
Utilize ConfiguredPhpVersionRangeHelper
1 parent f5dc20f commit 2ab3331

5 files changed

Lines changed: 21 additions & 44 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"require": {
1010
"php": "^7.4 || ^8.0",
1111
"phar-io/version": "^3.2",
12-
"phpstan/phpstan": "^2.2.3"
12+
"phpstan/phpstan": "^2.2.6"
1313
},
1414
"conflict": {
1515
"phpunit/phpunit": "<7.0"

src/Rules/PHPUnit/AttributeVersionRequirementHelper.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
use PharIo\Version\VersionConstraintParser;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\BetterReflection\Reflection\ReflectionAttribute;
10+
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
1011
use PHPStan\Php\PhpMinorVersionIterator;
11-
use PHPStan\Php\PhpVersion;
1212
use PHPStan\Rules\IdentifierRuleError;
1313
use PHPStan\Rules\RuleErrorBuilder;
14-
use PHPStan\Type\Constant\ConstantIntegerType;
15-
use PHPStan\Type\IntegerRangeType;
1614
use function count;
1715
use function is_numeric;
1816
use function preg_match;
1917
use function sprintf;
20-
use function strpos;
2118
use function substr_count;
2219
use function version_compare;
2320

@@ -28,8 +25,6 @@ final class AttributeVersionRequirementHelper
2825

2926
private PHPUnitVersion $PHPUnitVersion;
3027

31-
private PhpVersion $fallbackPhpVersion;
32-
3328
/**
3429
* When phpstan-deprecation-rules is installed, rule reports deprecated usages.
3530
*/
@@ -42,19 +37,21 @@ final class AttributeVersionRequirementHelper
4237

4338
private bool $bleedingEdge;
4439

40+
private ConfiguredPhpVersionRangeHelper $phpVersionRangeHelper;
41+
4542
public function __construct(
4643
PHPUnitVersion $PHPUnitVersion,
47-
PhpVersion $phpVersion,
44+
ConfiguredPhpVersionRangeHelper $phpVersionRangeHelper,
4845
bool $deprecationRulesInstalled = false,
4946
bool $bleedingEdge = false,
5047
bool $warnAboutIncompleteVersion = true
5148
)
5249
{
5350
$this->PHPUnitVersion = $PHPUnitVersion;
5451
$this->deprecationRulesInstalled = $deprecationRulesInstalled;
55-
$this->fallbackPhpVersion = $phpVersion;
5652
$this->warnAboutIncompleteVersion = $warnAboutIncompleteVersion;
5753
$this->bleedingEdge = $bleedingEdge;
54+
$this->phpVersionRangeHelper = $phpVersionRangeHelper;
5855
}
5956

6057
/**
@@ -64,7 +61,7 @@ public function __construct(
6461
*/
6562
public function checkVersionRequirement(array $attributes, Scope $scope): array
6663
{
67-
$phpstanPharIoVersions = $this->getAnalyzedPhpVersions($scope);
64+
$phpstanPharIoVersions = $this->getAnalyzedPhpVersions();
6865
if ($phpstanPharIoVersions === []) {
6966
return [];
7067
}
@@ -97,18 +94,11 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
9794
continue;
9895
}
9996

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

111-
foreach ($pharIoVersions as $pharIoVersion) {
101+
foreach ($phpstanPharIoVersions as $pharIoVersion) {
112102
if ($testPhpVersionConstraint->complies($pharIoVersion)) {
113103
// one of the versions within range matched, check next attribute
114104
continue 2;
@@ -128,7 +118,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
128118

129119
$operator = $matches['operator'] !== '' ? $matches['operator'] : '>=';
130120

131-
foreach ($pharIoVersions as $pharIoVersion) {
121+
foreach ($phpstanPharIoVersions as $pharIoVersion) {
132122
if (version_compare($pharIoVersion->getVersionString(), $matches['version'], $operator)) {
133123
// one of the versions within range matched, check next attribute
134124
continue 2;
@@ -168,29 +158,22 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
168158
/**
169159
* @return Version[]
170160
*/
171-
private function getAnalyzedPhpVersions(Scope $scope): array
161+
private function getAnalyzedPhpVersions(): array
172162
{
173-
$scopePhpVersion = $scope->getPhpVersion()->getType();
174-
if ($scopePhpVersion instanceof ConstantIntegerType) {
175-
$v = new PhpVersion($scopePhpVersion->getValue());
176-
return [new Version($v->getVersionString())];
177-
} elseif ($scopePhpVersion instanceof IntegerRangeType) {
178-
if ($scopePhpVersion->getMin() === null || $scopePhpVersion->getMax() === null) {
179-
return [];
180-
}
181-
163+
[$minVersion, $maxVersion] = $this->phpVersionRangeHelper->getVersionRange();
164+
if ($minVersion !== null && $maxVersion !== null) {
182165
$versions = [];
183166
$minorVersionIterator = new PhpMinorVersionIterator(
184-
new PhpVersion($scopePhpVersion->getMin()),
185-
new PhpVersion($scopePhpVersion->getMax()),
167+
$minVersion,
168+
$maxVersion,
186169
);
187170
foreach ($minorVersionIterator as $phpstanVersion) {
188171
$versions[] = new Version($phpstanVersion->getVersionString());
189172
}
190173
return $versions;
191174
}
192175

193-
return [new Version($this->fallbackPhpVersion->getVersionString())];
176+
return [];
194177
}
195178

196179
// see https://github.com/sebastianbergmann/phpunit/issues/6451

tests/Rules/PHPUnit/AttributeRequiresPhpVersionRangeRuleTest.php

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

33
namespace PHPStan\Rules\PHPUnit;
44

5-
use PHPStan\Php\PhpVersion;
5+
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
88
use PHPStan\Type\FileTypeMapper;
@@ -13,8 +13,6 @@
1313
final class AttributeRequiresPhpVersionRangeRuleTest extends RuleTestCase
1414
{
1515

16-
private int $phpVersion = 80500;
17-
1816
public function testPhpVersionMismatch(): void
1917
{
2018
$this->analyse([__DIR__ . '/data/requires-php-version-mismatch.php'], [
@@ -52,7 +50,7 @@ protected function getRule(): Rule
5250
),
5351
new AttributeVersionRequirementHelper(
5452
$phpunitVersion,
55-
new PhpVersion($this->phpVersion),
53+
self::getContainer()->getByType(ConfiguredPhpVersionRangeHelper::class),
5654
false,
5755
true,
5856
),

tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php

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

33
namespace PHPStan\Rules\PHPUnit;
44

5-
use PHPStan\Php\PhpVersion;
5+
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
88
use PHPStan\Type\FileTypeMapper;
@@ -13,8 +13,6 @@
1313
final class AttributeRequiresPhpVersionRuleTest extends RuleTestCase
1414
{
1515

16-
private int $phpVersion = 80500;
17-
1816
private ?int $phpunitMajorVersion;
1917

2018
private ?int $phpunitMinorVersion;
@@ -196,7 +194,7 @@ protected function getRule(): Rule
196194
),
197195
new AttributeVersionRequirementHelper(
198196
$phpunitVersion,
199-
new PhpVersion($this->phpVersion),
197+
self::getContainer()->getByType(ConfiguredPhpVersionRangeHelper::class),
200198
$this->deprecationRulesInstalled,
201199
true,
202200
$this->warnAboutIncompleteVersion,

tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php

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

33
namespace PHPStan\Rules\PHPUnit;
44

5-
use PHPStan\Php\PhpVersion;
5+
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
66
use PHPStan\Rules\Rule;
77
use PHPStan\Testing\RuleTestCase;
88

@@ -12,8 +12,6 @@
1212
final class ClassAttributeRequiresPhpVersionRuleTest extends RuleTestCase
1313
{
1414

15-
private int $phpVersion = 80500;
16-
1715
private int $phpunitMajorVersion;
1816

1917
private int $phpunitMinorVersion;
@@ -59,7 +57,7 @@ protected function getRule(): Rule
5957
return new ClassAttributeRequiresPhpVersionRule(
6058
new AttributeVersionRequirementHelper(
6159
$phpunitVersion,
62-
new PhpVersion($this->phpVersion),
60+
self::getContainer()->getByType(ConfiguredPhpVersionRangeHelper::class),
6361
false,
6462
true,
6563
$this->warnAboutIncompleteVersion,

0 commit comments

Comments
 (0)