From de2ec08da1c5467c177ac112796e2e5c4765dbd9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 25 Nov 2026 16:07:46 +0100 Subject: [PATCH 1/7] Some `#[RequiresPhp]` errors are autofixable --- .../AttributeRequiresPhpVersionRule.php | 1 + .../AttributeVersionRequirementHelper.php | 121 ++++++++++++++++-- .../ClassAttributeRequiresPhpVersionRule.php | 1 + .../AttributeRequiresPhpVersionRuleTest.php | 19 +++ ...assAttributeRequiresPhpVersionRuleTest.php | 9 ++ .../data/requires-php-version-fixable.php | 78 +++++++++++ .../requires-php-version-fixable.php.fixed | 78 +++++++++++ .../requires-php-version-on-class-fixable.php | 24 ++++ ...res-php-version-on-class-fixable.php.fixed | 24 ++++ .../data/requires-php-version.php.fixed | 32 +++++ 10 files changed, 377 insertions(+), 10 deletions(-) create mode 100644 tests/Rules/PHPUnit/data/requires-php-version-fixable.php create mode 100644 tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed create mode 100644 tests/Rules/PHPUnit/data/requires-php-version-on-class-fixable.php create mode 100644 tests/Rules/PHPUnit/data/requires-php-version-on-class-fixable.php.fixed create mode 100644 tests/Rules/PHPUnit/data/requires-php-version.php.fixed diff --git a/src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php b/src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php index b5521f74..66e74fb7 100644 --- a/src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php +++ b/src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php @@ -50,6 +50,7 @@ public function processNode(Node $node, Scope $scope): array $reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'), $reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhpunit'), ), + $node->getOriginalNode()->attrGroups, $scope, ); } diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index 203b3fe7..ff42135a 100644 --- a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php +++ b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php @@ -5,6 +5,8 @@ use PharIo\Version\UnsupportedVersionConstraintException; use PharIo\Version\Version; use PharIo\Version\VersionConstraintParser; +use PhpParser\Node; +use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\BetterReflection\Reflection\ReflectionAttribute; use PHPStan\Php\ConfiguredPhpVersionRangeHelper; @@ -13,6 +15,8 @@ use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; use function count; +use function explode; +use function implode; use function is_numeric; use function is_string; use function preg_match; @@ -24,6 +28,12 @@ final class AttributeVersionRequirementHelper { + /** + * Matches requirements whose version can be completed and given an explicit `>=` operator + * without changing their meaning, e.g. `8`, `8.1`, `>=8.1` or `^8.1`. + */ + private const FIXABLE_VERSION_REQUIREMENT = '/^(?P>=|\^)?(?P\s*)(?P\d+(?:\.\d+){0,2})$/'; + private const VERSION_COMPARISON = "/(?P!=|<|<=|<>|=|==|>|>=)?\s*(?P[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m"; private PHPUnitVersion $PHPUnitVersion; @@ -59,19 +69,30 @@ public function __construct( /** * @param array $attributes + * @param array $attrGroups AST attribute groups of the node $attributes were read from * * @return list */ - public function checkVersionRequirement(array $attributes, Scope $scope): array + public function checkVersionRequirement(array $attributes, array $attrGroups, Scope $scope): array { $errors = []; $parser = new VersionConstraintParser(); + $attributeNodes = $this->groupAttributeNodesByName($attrGroups); + $attributeIndexes = []; foreach ($attributes as $attr) { + $attributeIndex = $attributeIndexes[$attr->getName()] ?? 0; + $attributeIndexes[$attr->getName()] = $attributeIndex + 1; + $args = $attr->getArguments(); if (count($args) !== 1) { continue; } + $versionNode = $this->getFixableVersionNode( + $attributeNodes[$attr->getName()][$attributeIndex] ?? null, + $args, + ); + // the following block is mimicing PHPUnit version parsing // see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php @@ -82,11 +103,11 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array } if ($this->warnAboutIncompleteVersion($versionRequirement)) { - $errors[] = RuleErrorBuilder::message( + $errorBuilder = RuleErrorBuilder::message( sprintf('Version requirement %s is incomplete. Expect a version composed of major, minor and patch.', $versionRequirement), ) - ->identifier('phpunit.attributeRequiresPhpVersion') - ->build(); + ->identifier('phpunit.attributeRequiresPhpVersion'); + $errors[] = $this->buildError($errorBuilder, $versionNode); } if ( @@ -161,25 +182,105 @@ public function checkVersionRequirement(array $attributes, Scope $scope): array } if ($this->PHPUnitVersion->requiresPhpversionAttributeWithOperator()->yes()) { - $errors[] = RuleErrorBuilder::message( + $errorBuilder = RuleErrorBuilder::message( sprintf('Version requirement %s is missing operator.', $versionRequirement), ) - ->identifier('phpunit.attributeRequiresPhpVersion') - ->build(); + ->identifier('phpunit.attributeRequiresPhpVersion'); + $errors[] = $this->buildError($errorBuilder, $versionNode); } elseif ( $this->deprecationRulesInstalled && $this->PHPUnitVersion->deprecatesPhpversionAttributeWithoutOperator()->yes() ) { - $errors[] = RuleErrorBuilder::message( + $errorBuilder = RuleErrorBuilder::message( sprintf('Version requirement %s without operator is deprecated.', $versionRequirement), ) - ->identifier('phpunit.attributeRequiresPhpVersion') - ->build(); + ->identifier('phpunit.attributeRequiresPhpVersion'); + $errors[] = $this->buildError($errorBuilder, $versionNode); } } return $errors; } + /** + * @param array $attrGroups + * + * @return array> + */ + private function groupAttributeNodesByName(array $attrGroups): array + { + $attributeNodes = []; + foreach ($attrGroups as $attrGroup) { + foreach ($attrGroup->attrs as $attributeNode) { + $attributeNodes[$attributeNode->name->toString()][] = $attributeNode; + } + } + + return $attributeNodes; + } + + /** + * Returns the string literal holding the version requirement, when it is a plain literal in the analysed file. + * + * @param array $args + */ + private function getFixableVersionNode(?Node\Attribute $attributeNode, array $args): ?String_ + { + if ($attributeNode === null || count($attributeNode->args) !== 1) { + return null; + } + + $versionNode = $attributeNode->args[0]->value; + if (!$versionNode instanceof String_) { + return null; + } + + // make sure the AST node is the one the reflection attribute was read from + if ($versionNode->value !== ($args[0] ?? null)) { + return null; + } + + return $versionNode; + } + + /** + * @param RuleErrorBuilder $errorBuilder + */ + private function buildError(RuleErrorBuilder $errorBuilder, ?String_ $versionNode): IdentifierRuleError + { + if ($versionNode === null) { + return $errorBuilder->build(); + } + + $fixedVersionRequirement = $this->fixVersionRequirement($versionNode->value); + if ($fixedVersionRequirement === null) { + return $errorBuilder->build(); + } + + return $errorBuilder->fixNode( + $versionNode, + static fn (String_ $versionNode): String_ => new String_($fixedVersionRequirement, ['kind' => $versionNode->getAttribute('kind')]), + )->build(); + } + + /** + * Completes the version to major.minor.patch and adds the implicit `>=` operator. + * Only applied where the meaning of the requirement stays the same. + */ + private function fixVersionRequirement(string $versionRequirement): ?string + { + if (preg_match(self::FIXABLE_VERSION_REQUIREMENT, $versionRequirement, $matches) !== 1) { + return null; + } + + $operator = $matches['operator'] !== '' ? $matches['operator'] : '>='; + $versionParts = explode('.', $matches['version']); + while (count($versionParts) < 3) { + $versionParts[] = '0'; + } + + return $operator . $matches['space'] . implode('.', $versionParts); + } + /** * @return list */ diff --git a/src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php b/src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php index a9e68ff3..1e2a8319 100644 --- a/src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php +++ b/src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php @@ -41,6 +41,7 @@ public function processNode(Node $node, Scope $scope): array $classReflection->getNativeReflection()->getBetterReflection()->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'), $classReflection->getNativeReflection()->getBetterReflection()->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhpunit'), ), + $node->getOriginalNode()->attrGroups, $scope, ); } diff --git a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php index 370d0a93..db91238f 100644 --- a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php @@ -207,6 +207,25 @@ public function testNonStringArguments(): void ]); } + public function testFixIncompleteVersion(): void + { + $this->phpunitMajorVersion = 13; + $this->phpunitMinorVersion = 0; + $this->deprecationRulesInstalled = false; + $this->warnAboutIncompleteVersion = true; + + $this->fix(__DIR__ . '/data/requires-php-version-fixable.php', __DIR__ . '/data/requires-php-version-fixable.php.fixed'); + } + + public function testFixVersionWithoutOperator(): void + { + $this->phpunitMajorVersion = 12; + $this->phpunitMinorVersion = 4; + $this->deprecationRulesInstalled = true; + + $this->fix(__DIR__ . '/data/requires-php-version.php', __DIR__ . '/data/requires-php-version.php.fixed'); + } + protected function getRule(): Rule { $phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion); diff --git a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php index 47e92dec..e082ddd7 100644 --- a/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php @@ -67,6 +67,15 @@ public function testNonStringArguments(): void ]); } + public function testFixIncompleteVersion(): void + { + $this->phpunitMajorVersion = 12; + $this->phpunitMinorVersion = 5; + $this->warnAboutIncompleteVersion = true; + + $this->fix(__DIR__ . '/data/requires-php-version-on-class-fixable.php', __DIR__ . '/data/requires-php-version-on-class-fixable.php.fixed'); + } + protected function getRule(): Rule { $phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion); diff --git a/tests/Rules/PHPUnit/data/requires-php-version-fixable.php b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php new file mode 100644 index 00000000..7a34ee50 --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php @@ -0,0 +1,78 @@ +=8.0')] + public function testGreaterOrEqual(): void { + + } + + #[RequiresPhp('>= 8')] + public function testGreaterOrEqualWithSpace(): void { + + } + + #[RequiresPhp('^8.0')] + public function testCaret(): void { + + } + + #[RequiresPhp("8.0")] + public function testDoubleQuoted(): void { + + } + + #[RequiresPhp('8.0'), RequiresPhpunit('>=11')] + public function testMultipleAttributes(): void { + + } + + #[RequiresPhp('>= 8.0.0')] + #[RequiresPhp('8.1')] + public function testRepeatedAttribute(): void { + + } + + // changing these would change their meaning + #[RequiresPhp('<8.0')] + public function testLessThan(): void { + + } + + #[RequiresPhp('>8.0')] + public function testGreaterThan(): void { + + } + + #[RequiresPhp('~8.0')] + public function testTilde(): void { + + } + + #[RequiresPhp('<=8.5')] + public function testLessOrEqual(): void { + + } +} diff --git a/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed new file mode 100644 index 00000000..4bb1b2c2 --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed @@ -0,0 +1,78 @@ +=8.0.0')] + public function testMajorOnly(): void { + + } + + #[RequiresPhp('>=8.0.0')] + public function testMissingOperator(): void { + + } + + // not reported, as it is not numeric + #[RequiresPhp('8.0.1')] + public function testCompleteMissingOperator(): void { + + } + + #[RequiresPhp('>=8.0.0')] + public function testGreaterOrEqual(): void { + + } + + #[RequiresPhp('>= 8.0.0')] + public function testGreaterOrEqualWithSpace(): void { + + } + + #[RequiresPhp('^8.0.0')] + public function testCaret(): void { + + } + + #[RequiresPhp(">=8.0.0")] + public function testDoubleQuoted(): void { + + } + + #[RequiresPhp('>=8.0.0'), RequiresPhpunit('>=11.0.0')] + public function testMultipleAttributes(): void { + + } + + #[RequiresPhp('>= 8.0.0')] + #[RequiresPhp('>=8.1.0')] + public function testRepeatedAttribute(): void { + + } + + // changing these would change their meaning + #[RequiresPhp('<8.0')] + public function testLessThan(): void { + + } + + #[RequiresPhp('>8.0')] + public function testGreaterThan(): void { + + } + + #[RequiresPhp('~8.0')] + public function testTilde(): void { + + } + + #[RequiresPhp('<=8.5')] + public function testLessOrEqual(): void { + + } +} diff --git a/tests/Rules/PHPUnit/data/requires-php-version-on-class-fixable.php b/tests/Rules/PHPUnit/data/requires-php-version-on-class-fixable.php new file mode 100644 index 00000000..8d97a731 --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-php-version-on-class-fixable.php @@ -0,0 +1,24 @@ +=8.0')] +#[RequiresPhpunit('^11.0')] +class FixableOnClassTest extends TestCase +{ + public function testBar(): void { + + } +} + +#[RequiresPhp('< 7.0')] +class NotFixableOnClassTest extends TestCase +{ + public function testBar(): void { + + } +} diff --git a/tests/Rules/PHPUnit/data/requires-php-version-on-class-fixable.php.fixed b/tests/Rules/PHPUnit/data/requires-php-version-on-class-fixable.php.fixed new file mode 100644 index 00000000..106b9e46 --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-php-version-on-class-fixable.php.fixed @@ -0,0 +1,24 @@ +=8.0.0')] +#[RequiresPhpunit('^11.0.0')] +class FixableOnClassTest extends TestCase +{ + public function testBar(): void { + + } +} + +#[RequiresPhp('< 7.0')] +class NotFixableOnClassTest extends TestCase +{ + public function testBar(): void { + + } +} diff --git a/tests/Rules/PHPUnit/data/requires-php-version.php.fixed b/tests/Rules/PHPUnit/data/requires-php-version.php.fixed new file mode 100644 index 00000000..aa61966f --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-php-version.php.fixed @@ -0,0 +1,32 @@ +=8.0.0')] + public function testFoo(): void { + + } +} + +class TwoDigitVersionB extends TestCase +{ + #[RequiresPhp('>=8.0')] + public function testBar(): void { + + } +} + +class CorrectRequirement extends TestCase +{ + #[RequiresPhp('>=8.0.0')] + public function testBar(): void { + + } +} From c75b1557d1e3056249140ad91fddcb4acf6c6aa3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 25 Nov 2026 16:15:16 +0100 Subject: [PATCH 2/7] Attach requirement version fix to first error per attribute only Several errors fixing the same string node caused a MergeConflictException. --- .../PHPUnit/AttributeVersionRequirementHelper.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index ff42135a..272f5357 100644 --- a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php +++ b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php @@ -243,21 +243,28 @@ private function getFixableVersionNode(?Node\Attribute $attributeNode, array $ar } /** + * Attaches the fix only to the first error of an attribute, + * as fixes of several errors replacing the same node conflict with each other. + * * @param RuleErrorBuilder $errorBuilder + * @param-out null $versionNode */ - private function buildError(RuleErrorBuilder $errorBuilder, ?String_ $versionNode): IdentifierRuleError + private function buildError(RuleErrorBuilder $errorBuilder, ?String_ &$versionNode): IdentifierRuleError { if ($versionNode === null) { return $errorBuilder->build(); } - $fixedVersionRequirement = $this->fixVersionRequirement($versionNode->value); + $node = $versionNode; + $versionNode = null; + + $fixedVersionRequirement = $this->fixVersionRequirement($node->value); if ($fixedVersionRequirement === null) { return $errorBuilder->build(); } return $errorBuilder->fixNode( - $versionNode, + $node, static fn (String_ $versionNode): String_ => new String_($fixedVersionRequirement, ['kind' => $versionNode->getAttribute('kind')]), )->build(); } From 6c2f62b2630771b62910fdf018f509332fdc29a1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 25 Nov 2026 16:26:41 +0100 Subject: [PATCH 3/7] Attach requirement version fix to first error per attribute only Several errors fixing the same string node caused a MergeConflictException. --- tests/Rules/PHPUnit/data/requires-php-version-fixable.php | 3 ++- .../Rules/PHPUnit/data/requires-php-version-fixable.php.fixed | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Rules/PHPUnit/data/requires-php-version-fixable.php b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php index 7a34ee50..361065aa 100644 --- a/tests/Rules/PHPUnit/data/requires-php-version-fixable.php +++ b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php @@ -44,7 +44,8 @@ public function testDoubleQuoted(): void { } - #[RequiresPhp('8.0'), RequiresPhpunit('>=11')] + #[RequiresPhp('8.0')] + #[RequiresPhpunit('>=11')] public function testMultipleAttributes(): void { } diff --git a/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed index 4bb1b2c2..f6ad1847 100644 --- a/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed +++ b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed @@ -44,7 +44,8 @@ class FixableTest extends TestCase } - #[RequiresPhp('>=8.0.0'), RequiresPhpunit('>=11.0.0')] + #[RequiresPhp('>=8.0.0')] + #[RequiresPhpunit('>=11.0.0')] public function testMultipleAttributes(): void { } From 8dfba1682734a9dfd12062816132fc9eb8c9d1ad Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 25 Nov 2026 16:27:42 +0100 Subject: [PATCH 4/7] Revert "Attach requirement version fix to first error per attribute only" This reverts commit ec258ff5f35e56373dfa67364b59380a0a6e8ebb. --- .../PHPUnit/AttributeVersionRequirementHelper.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index 272f5357..ff42135a 100644 --- a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php +++ b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php @@ -243,28 +243,21 @@ private function getFixableVersionNode(?Node\Attribute $attributeNode, array $ar } /** - * Attaches the fix only to the first error of an attribute, - * as fixes of several errors replacing the same node conflict with each other. - * * @param RuleErrorBuilder $errorBuilder - * @param-out null $versionNode */ - private function buildError(RuleErrorBuilder $errorBuilder, ?String_ &$versionNode): IdentifierRuleError + private function buildError(RuleErrorBuilder $errorBuilder, ?String_ $versionNode): IdentifierRuleError { if ($versionNode === null) { return $errorBuilder->build(); } - $node = $versionNode; - $versionNode = null; - - $fixedVersionRequirement = $this->fixVersionRequirement($node->value); + $fixedVersionRequirement = $this->fixVersionRequirement($versionNode->value); if ($fixedVersionRequirement === null) { return $errorBuilder->build(); } return $errorBuilder->fixNode( - $node, + $versionNode, static fn (String_ $versionNode): String_ => new String_($fixedVersionRequirement, ['kind' => $versionNode->getAttribute('kind')]), )->build(); } From af181bb77e7a3ba8761b6f8f6acd593c3a5546ae Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 27 Nov 2026 07:07:00 +0100 Subject: [PATCH 5/7] Don't crash on named arguments --- .../AttributeVersionRequirementHelper.php | 21 +++++++------- .../AttributeRequiresPhpVersionRuleTest.php | 29 +++++++++++++++++++ .../requires-php-version-named-argument.php | 16 ++++++++++ ...uires-php-version-named-argument.php.fixed | 16 ++++++++++ 4 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 tests/Rules/PHPUnit/data/requires-php-version-named-argument.php create mode 100644 tests/Rules/PHPUnit/data/requires-php-version-named-argument.php.fixed diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index ff42135a..f989fddb 100644 --- a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php +++ b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php @@ -88,20 +88,21 @@ public function checkVersionRequirement(array $attributes, array $attrGroups, Sc continue; } + // phpunit does not support named arguments. + // we handle them here just to make sure we don't crash on them. + $versionRequirement = $args[0] ?? $args['versionRequirement'] ?? null; + if (!is_string($versionRequirement)) { + continue; + } + $versionNode = $this->getFixableVersionNode( $attributeNodes[$attr->getName()][$attributeIndex] ?? null, - $args, + $versionRequirement, ); // the following block is mimicing PHPUnit version parsing // see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php - // named arguments are handled just to make sure we don't crash on them. - $versionRequirement = $args[0] ?? $args['versionRequirement'] ?? null; - if (!is_string($versionRequirement)) { - continue; - } - if ($this->warnAboutIncompleteVersion($versionRequirement)) { $errorBuilder = RuleErrorBuilder::message( sprintf('Version requirement %s is incomplete. Expect a version composed of major, minor and patch.', $versionRequirement), @@ -220,10 +221,8 @@ private function groupAttributeNodesByName(array $attrGroups): array /** * Returns the string literal holding the version requirement, when it is a plain literal in the analysed file. - * - * @param array $args */ - private function getFixableVersionNode(?Node\Attribute $attributeNode, array $args): ?String_ + private function getFixableVersionNode(?Node\Attribute $attributeNode, string $versionRequirement): ?String_ { if ($attributeNode === null || count($attributeNode->args) !== 1) { return null; @@ -235,7 +234,7 @@ private function getFixableVersionNode(?Node\Attribute $attributeNode, array $ar } // make sure the AST node is the one the reflection attribute was read from - if ($versionNode->value !== ($args[0] ?? null)) { + if ($versionNode->value !== $versionRequirement) { return null; } diff --git a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php index db91238f..aa7c9195 100644 --- a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php @@ -226,6 +226,35 @@ public function testFixVersionWithoutOperator(): void $this->fix(__DIR__ . '/data/requires-php-version.php', __DIR__ . '/data/requires-php-version.php.fixed'); } + public function testNamedArgument(): void + { + $this->phpunitMajorVersion = 13; + $this->phpunitMinorVersion = 0; + $this->deprecationRulesInstalled = false; + $this->warnAboutIncompleteVersion = true; + + $this->analyse([__DIR__ . '/data/requires-php-version-named-argument.php'], [ + [ + 'Version requirement 8.0 is incomplete. Expect a version composed of major, minor and patch.', + 12, + ], + [ + 'Version requirement 8.0 is missing operator.', + 12, + ], + ]); + } + + public function testFixNamedArgument(): void + { + $this->phpunitMajorVersion = 13; + $this->phpunitMinorVersion = 0; + $this->deprecationRulesInstalled = false; + $this->warnAboutIncompleteVersion = true; + + $this->fix(__DIR__ . '/data/requires-php-version-named-argument.php', __DIR__ . '/data/requires-php-version-named-argument.php.fixed'); + } + protected function getRule(): Rule { $phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion); diff --git a/tests/Rules/PHPUnit/data/requires-php-version-named-argument.php b/tests/Rules/PHPUnit/data/requires-php-version-named-argument.php new file mode 100644 index 00000000..ecda29d0 --- /dev/null +++ b/tests/Rules/PHPUnit/data/requires-php-version-named-argument.php @@ -0,0 +1,16 @@ +=8.0.0')] + public function testFoo(): void { + + } +} From d495fdb3d682f24516e8f2a38ce1c0771e0652e7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 27 Nov 2026 07:20:37 +0100 Subject: [PATCH 6/7] Don't auto-fix versions without operator, as this can change meaning when tests will run --- .../AttributeVersionRequirementHelper.php | 25 ++++++++------- .../AttributeRequiresPhpVersionRuleTest.php | 6 ++-- .../requires-php-version-fixable.php.fixed | 10 +++--- ...uires-php-version-named-argument.php.fixed | 16 ---------- .../data/requires-php-version.php.fixed | 32 ------------------- 5 files changed, 21 insertions(+), 68 deletions(-) delete mode 100644 tests/Rules/PHPUnit/data/requires-php-version-named-argument.php.fixed delete mode 100644 tests/Rules/PHPUnit/data/requires-php-version.php.fixed diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index f989fddb..b30f59bd 100644 --- a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php +++ b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php @@ -29,10 +29,11 @@ final class AttributeVersionRequirementHelper { /** - * Matches requirements whose version can be completed and given an explicit `>=` operator - * without changing their meaning, e.g. `8`, `8.1`, `>=8.1` or `^8.1`. + * Matches requirements whose version can be completed to major.minor.patch + * without changing their meaning, e.g. `>=8`, `>=8.1` or `^8.1`. + * Versions without an operator are deliberately not matched. */ - private const FIXABLE_VERSION_REQUIREMENT = '/^(?P>=|\^)?(?P\s*)(?P\d+(?:\.\d+){0,2})$/'; + private const FIXABLE_VERSION_REQUIREMENT = '/^(?P>=|\^)(?P\s*)(?P\d+(?:\.\d+){0,2})$/'; private const VERSION_COMPARISON = "/(?P!=|<|<=|<>|=|==|>|>=)?\s*(?P[\d\.-]+(dev|(RC|alpha|beta)[\d\.])?)[ \t]*\r?$/m"; @@ -183,20 +184,20 @@ public function checkVersionRequirement(array $attributes, array $attrGroups, Sc } if ($this->PHPUnitVersion->requiresPhpversionAttributeWithOperator()->yes()) { - $errorBuilder = RuleErrorBuilder::message( + $errors[] = RuleErrorBuilder::message( sprintf('Version requirement %s is missing operator.', $versionRequirement), ) - ->identifier('phpunit.attributeRequiresPhpVersion'); - $errors[] = $this->buildError($errorBuilder, $versionNode); + ->identifier('phpunit.attributeRequiresPhpVersion') + ->build(); } elseif ( $this->deprecationRulesInstalled && $this->PHPUnitVersion->deprecatesPhpversionAttributeWithoutOperator()->yes() ) { - $errorBuilder = RuleErrorBuilder::message( + $errors[] = RuleErrorBuilder::message( sprintf('Version requirement %s without operator is deprecated.', $versionRequirement), ) - ->identifier('phpunit.attributeRequiresPhpVersion'); - $errors[] = $this->buildError($errorBuilder, $versionNode); + ->identifier('phpunit.attributeRequiresPhpVersion') + ->build(); } } return $errors; @@ -262,8 +263,9 @@ private function buildError(RuleErrorBuilder $errorBuilder, ?String_ $versionNod } /** - * Completes the version to major.minor.patch and adds the implicit `>=` operator. + * Completes the version to major.minor.patch. * Only applied where the meaning of the requirement stays the same. + * Versions without an operator are not fixed. */ private function fixVersionRequirement(string $versionRequirement): ?string { @@ -271,13 +273,12 @@ private function fixVersionRequirement(string $versionRequirement): ?string return null; } - $operator = $matches['operator'] !== '' ? $matches['operator'] : '>='; $versionParts = explode('.', $matches['version']); while (count($versionParts) < 3) { $versionParts[] = '0'; } - return $operator . $matches['space'] . implode('.', $versionParts); + return $matches['operator'] . $matches['space'] . implode('.', $versionParts); } /** diff --git a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php index aa7c9195..84e886c4 100644 --- a/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php +++ b/tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php @@ -217,13 +217,13 @@ public function testFixIncompleteVersion(): void $this->fix(__DIR__ . '/data/requires-php-version-fixable.php', __DIR__ . '/data/requires-php-version-fixable.php.fixed'); } - public function testFixVersionWithoutOperator(): void + public function testDoNotFixVersionWithoutOperator(): void { $this->phpunitMajorVersion = 12; $this->phpunitMinorVersion = 4; $this->deprecationRulesInstalled = true; - $this->fix(__DIR__ . '/data/requires-php-version.php', __DIR__ . '/data/requires-php-version.php.fixed'); + $this->fix(__DIR__ . '/data/requires-php-version.php', __DIR__ . '/data/requires-php-version.php'); } public function testNamedArgument(): void @@ -252,7 +252,7 @@ public function testFixNamedArgument(): void $this->deprecationRulesInstalled = false; $this->warnAboutIncompleteVersion = true; - $this->fix(__DIR__ . '/data/requires-php-version-named-argument.php', __DIR__ . '/data/requires-php-version-named-argument.php.fixed'); + $this->fix(__DIR__ . '/data/requires-php-version-named-argument.php', __DIR__ . '/data/requires-php-version-named-argument.php'); } protected function getRule(): Rule diff --git a/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed index f6ad1847..ab3f6312 100644 --- a/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed +++ b/tests/Rules/PHPUnit/data/requires-php-version-fixable.php.fixed @@ -8,12 +8,12 @@ use PHPUnit\Framework\Attributes\RequiresPhpunit; class FixableTest extends TestCase { - #[RequiresPhp('>=8.0.0')] + #[RequiresPhp('8')] public function testMajorOnly(): void { } - #[RequiresPhp('>=8.0.0')] + #[RequiresPhp('8.0')] public function testMissingOperator(): void { } @@ -39,19 +39,19 @@ class FixableTest extends TestCase } - #[RequiresPhp(">=8.0.0")] + #[RequiresPhp("8.0")] public function testDoubleQuoted(): void { } - #[RequiresPhp('>=8.0.0')] + #[RequiresPhp('8.0')] #[RequiresPhpunit('>=11.0.0')] public function testMultipleAttributes(): void { } #[RequiresPhp('>= 8.0.0')] - #[RequiresPhp('>=8.1.0')] + #[RequiresPhp('8.1')] public function testRepeatedAttribute(): void { } diff --git a/tests/Rules/PHPUnit/data/requires-php-version-named-argument.php.fixed b/tests/Rules/PHPUnit/data/requires-php-version-named-argument.php.fixed deleted file mode 100644 index d19a26c8..00000000 --- a/tests/Rules/PHPUnit/data/requires-php-version-named-argument.php.fixed +++ /dev/null @@ -1,16 +0,0 @@ -=8.0.0')] - public function testFoo(): void { - - } -} diff --git a/tests/Rules/PHPUnit/data/requires-php-version.php.fixed b/tests/Rules/PHPUnit/data/requires-php-version.php.fixed deleted file mode 100644 index aa61966f..00000000 --- a/tests/Rules/PHPUnit/data/requires-php-version.php.fixed +++ /dev/null @@ -1,32 +0,0 @@ -=8.0.0')] - public function testFoo(): void { - - } -} - -class TwoDigitVersionB extends TestCase -{ - #[RequiresPhp('>=8.0')] - public function testBar(): void { - - } -} - -class CorrectRequirement extends TestCase -{ - #[RequiresPhp('>=8.0.0')] - public function testBar(): void { - - } -} From 4be5956b682467c087855d3e879b159f99434185 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 27 Nov 2026 08:21:19 +0100 Subject: [PATCH 7/7] remove unnecessary work --- src/Rules/PHPUnit/AttributeVersionRequirementHelper.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php index b30f59bd..6f3b59c4 100644 --- a/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php +++ b/src/Rules/PHPUnit/AttributeVersionRequirementHelper.php @@ -96,11 +96,6 @@ public function checkVersionRequirement(array $attributes, array $attrGroups, Sc continue; } - $versionNode = $this->getFixableVersionNode( - $attributeNodes[$attr->getName()][$attributeIndex] ?? null, - $versionRequirement, - ); - // the following block is mimicing PHPUnit version parsing // see https://github.com/sebastianbergmann/phpunit/blob/43c2cd7b96ee1e800b35e4df23b419a88b53111d/src/Metadata/Version/Requirement.php @@ -109,6 +104,10 @@ public function checkVersionRequirement(array $attributes, array $attrGroups, Sc sprintf('Version requirement %s is incomplete. Expect a version composed of major, minor and patch.', $versionRequirement), ) ->identifier('phpunit.attributeRequiresPhpVersion'); + $versionNode = $this->getFixableVersionNode( + $attributeNodes[$attr->getName()][$attributeIndex] ?? null, + $versionRequirement, + ); $errors[] = $this->buildError($errorBuilder, $versionNode); }