Skip to content

Commit 6983f95

Browse files
committed
1:1 reimplement php-version detection
1 parent 03d1ecd commit 6983f95

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "^7.4 || ^8.0",
10-
"composer/semver": "^3.4",
10+
"phar-io/version": "^3.2",
1111
"phpstan/phpstan": "^2.1.32"
1212
},
1313
"conflict": {

src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use Composer\Semver\Constraint\ConstraintInterface;
66
use Composer\Semver\VersionParser;
7+
use PharIo\Version\UnsupportedVersionConstraintException;
8+
use PharIo\Version\Version;
9+
use PharIo\Version\VersionConstraint;
10+
use PharIo\Version\VersionConstraintParser;
711
use PhpParser\Node;
812
use PHPStan\Analyser\Scope;
913
use PHPStan\Node\InClassMethodNode;
@@ -15,15 +19,18 @@
1519
use function count;
1620
use function is_numeric;
1721
use function method_exists;
22+
use function preg_match;
1823
use function sprintf;
1924

2025
/**
2126
* @implements Rule<InClassMethodNode>
2227
*/
2328
class AttributeRequiresPhpVersionRule implements Rule
2429
{
30+
private const VERSION_COMPARISON = "/(?P<operator>!=|<|<=|<>|=|==|>|>=)?\s*(?P<version>[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m";
2531

26-
private ConstraintInterface $phpstanVersionConstraint;
32+
33+
private Version $phpstanPhpVersion;
2734

2835
private PHPUnitVersion $PHPUnitVersion;
2936

@@ -45,8 +52,7 @@ public function __construct(
4552
$this->testMethodsHelper = $testMethodsHelper;
4653
$this->deprecationRulesInstalled = $deprecationRulesInstalled;
4754

48-
$parser = new VersionParser();
49-
$this->phpstanVersionConstraint = $parser->parseConstraints($phpVersion->getVersionString());
55+
$this->phpstanPhpVersion = new Version($phpVersion->getVersionString());
5056
}
5157

5258
public function getNodeType(): string
@@ -72,7 +78,7 @@ public function processNode(Node $node, Scope $scope): array
7278
}
7379

7480
$errors = [];
75-
$parser = new VersionParser();
81+
$parser = new VersionConstraintParser();
7682
foreach ($reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\RequiresPhp') as $attr) {
7783
$args = $attr->getArguments();
7884
if (count($args) !== 1) {
@@ -82,21 +88,28 @@ public function processNode(Node $node, Scope $scope): array
8288
if (
8389
!is_numeric($args[0])
8490
) {
85-
8691
try {
87-
$testPhpVersionConstraint = $parser->parseConstraints($args[0]);
88-
} catch (UnexpectedValueException $e) {
89-
$errors[] = RuleErrorBuilder::message(
90-
sprintf($e->getMessage()),
91-
)
92-
->identifier('phpunit.attributeRequiresPhpVersion')
93-
->build();
94-
95-
continue;
96-
}
97-
98-
if ($this->phpstanVersionConstraint->matches($testPhpVersionConstraint)) {
99-
continue;
92+
$testPhpVersionConstraint = $parser->parse($args[0]);
93+
94+
if ($testPhpVersionConstraint->complies($this->phpstanPhpVersion)) {
95+
continue;
96+
}
97+
} catch (UnsupportedVersionConstraintException $e) {
98+
if (preg_match(self::VERSION_COMPARISON, $args[0], $matches) > 0) {
99+
$operator = $matches['operator'] !== '' ? $matches['operator'] : '>=';
100+
101+
if (version_compare($this->phpstanPhpVersion->getVersionString(), $matches['version'], $operator)) {
102+
continue;
103+
}
104+
} else {
105+
$errors[] = RuleErrorBuilder::message(
106+
sprintf($e->getMessage()),
107+
)
108+
->identifier('phpunit.attributeRequiresPhpVersion')
109+
->build();
110+
111+
continue;
112+
}
100113
}
101114

102115
$errors[] = RuleErrorBuilder::message(

tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function testInvalidPhpVersion(): void
103103

104104
$this->analyse([__DIR__ . '/data/requires-php-version-invalid.php'], [
105105
[
106-
'Could not parse version constraint abc: Invalid version string "abc"',
106+
'Version constraint abc is not supported.',
107107
12,
108108
],
109109
]);

0 commit comments

Comments
 (0)