Skip to content

Commit 2178b23

Browse files
committed
Use sebastian/version-requirement
1 parent 2297f6c commit 2178b23

3 files changed

Lines changed: 24 additions & 40 deletions

File tree

‎composer.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"require": {
1010
"php": "^7.4 || ^8.0",
1111
"phar-io/version": "^3.2",
12-
"phpstan/phpstan": "^2.2.6"
12+
"phpstan/phpstan": "^2.2.6",
13+
"sebastian/version-requirement": "dev-main"
1314
},
1415
"conflict": {
1516
"phpunit/phpunit": "<7.0"
@@ -19,7 +20,7 @@
1920
"php-parallel-lint/php-parallel-lint": "^1.2",
2021
"phpstan/phpstan-deprecation-rules": "^2.0",
2122
"phpstan/phpstan-strict-rules": "^2.0",
22-
"phpunit/phpunit": "^9.6",
23+
"phpunit/phpunit": "^12",
2324
"shipmonk/name-collision-detector": "^2.1"
2425
},
2526
"config": {

‎src/Rules/PHPUnit/AttributeVersionRequirementHelper.php‎

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22

33
namespace PHPStan\Rules\PHPUnit;
44

5-
use PharIo\Version\UnsupportedVersionConstraintException;
6-
use PharIo\Version\Version;
7-
use PharIo\Version\VersionConstraintParser;
85
use PHPStan\Analyser\Scope;
96
use PHPStan\BetterReflection\Reflection\ReflectionAttribute;
107
use PHPStan\Php\ConfiguredPhpVersionRangeHelper;
118
use PHPStan\Php\PhpMinorVersionIterator;
129
use PHPStan\Rules\IdentifierRuleError;
1310
use PHPStan\Rules\RuleErrorBuilder;
11+
use SebastianBergmann\VersionRequirement\InvalidVersionRequirementException;
12+
use SebastianBergmann\VersionRequirement\Requirement;
1413
use function count;
1514
use function is_numeric;
16-
use function preg_match;
1715
use function sprintf;
1816
use function strpos;
1917
use function substr_count;
20-
use function version_compare;
2118

2219
final class AttributeVersionRequirementHelper
2320
{
@@ -63,7 +60,6 @@ public function __construct(
6360
public function checkVersionRequirement(array $attributes, Scope $scope): array
6461
{
6562
$errors = [];
66-
$parser = new VersionConstraintParser();
6763
foreach ($attributes as $attr) {
6864
$args = $attr->getArguments();
6965
if (count($args) !== 1) {
@@ -90,43 +86,31 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
9086
continue;
9187
}
9288

93-
$pharIoVersions = strpos($attr->getName(), 'RequiresPhpunit') !== false
94-
? $this->PHPUnitVersion->getPharIoVersions()
89+
$versionStrings = strpos($attr->getName(), 'RequiresPhpunit') !== false
90+
? $this->PHPUnitVersion->getMinMaxVersion()
9591
: $this->getAnalyzedPhpVersions();
96-
if ($pharIoVersions === []) {
92+
if ($versionStrings === []) {
9793
continue;
9894
}
9995

10096
try {
10197
// check composer like version constraints, e.g. ^1 or ~2
102-
$testPhpVersionConstraint = $parser->parse($versionRequirement);
98+
$requirement = Requirement::from($versionRequirement);
10399

104-
foreach ($pharIoVersions as $pharIoVersion) {
105-
if ($testPhpVersionConstraint->complies($pharIoVersion)) {
100+
foreach ($versionStrings as $versionString) {
101+
if ($requirement->isSatisfiedBy($versionString)) {
106102
// one of the versions within range matched, check next attribute
107103
continue 2;
108104
}
109105
}
110-
} catch (UnsupportedVersionConstraintException $e) {
111-
// test php-src builtin operators as in version_compare()
112-
if (preg_match(self::VERSION_COMPARISON, $versionRequirement, $matches) <= 0) {
113-
$errors[] = RuleErrorBuilder::message(
114-
sprintf($e->getMessage()),
115-
)
116-
->identifier('phpunit.attributeRequiresPhpVersion')
117-
->build();
118-
119-
continue;
120-
}
121-
122-
$operator = $matches['operator'] !== '' ? $matches['operator'] : '>=';
106+
} catch (InvalidVersionRequirementException $e) {
107+
$errors[] = RuleErrorBuilder::message(
108+
sprintf($e->getMessage()),
109+
)
110+
->identifier('phpunit.attributeRequiresPhpVersion')
111+
->build();
123112

124-
foreach ($pharIoVersions as $pharIoVersion) {
125-
if (version_compare($pharIoVersion->getVersionString(), $matches['version'], $operator)) {
126-
// one of the versions within range matched, check next attribute
127-
continue 2;
128-
}
129-
}
113+
continue;
130114
}
131115

132116
$errors[] = RuleErrorBuilder::message(
@@ -159,7 +143,7 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array
159143
}
160144

161145
/**
162-
* @return Version[]
146+
* @return list<string>
163147
*/
164148
private function getAnalyzedPhpVersions(): array
165149
{
@@ -172,7 +156,7 @@ private function getAnalyzedPhpVersions(): array
172156
$maxVersion,
173157
);
174158
foreach ($minorVersionIterator as $phpstanVersion) {
175-
$versions[] = new Version($phpstanVersion->getVersionString());
159+
$versions[] = $phpstanVersion->getVersionString();
176160
}
177161
return $versions;
178162
}

‎src/Rules/PHPUnit/PHPUnitVersion.php‎

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

33
namespace PHPStan\Rules\PHPUnit;
44

5-
use PharIo\Version\Version;
65
use PHPStan\TrinaryLogic;
76
use function sprintf;
87

@@ -20,17 +19,17 @@ public function __construct(?int $majorVersion, ?int $minorVersion)
2019
}
2120

2221
/**
23-
* @return array{}|array{Version, Version}
22+
* @return array{}|array{string, string}
2423
*/
25-
public function getPharIoVersions(): array
24+
public function getMinMaxVersion(): array
2625
{
2726
if ($this->majorVersion === null || $this->minorVersion === null) {
2827
return [];
2928
}
3029

3130
return [
32-
new Version(sprintf('%d.%d.0', $this->majorVersion, $this->minorVersion)),
33-
new Version(sprintf('%d.%d.99', $this->majorVersion, $this->minorVersion)),
31+
sprintf('%d.%d.0', $this->majorVersion, $this->minorVersion),
32+
sprintf('%d.%d.99', $this->majorVersion, $this->minorVersion),
3433
];
3534
}
3635

0 commit comments

Comments
 (0)