Skip to content

Bond annotation to attribute rules to phpunit/phpunit >=10.0 - #744

Merged
TomasVotruba merged 2 commits into
mainfrom
annotation-rules-phpunit-10-constraint
Jul 31, 2026
Merged

Bond annotation to attribute rules to phpunit/phpunit >=10.0#744
TomasVotruba merged 2 commits into
mainfrom
annotation-rules-phpunit-10-constraint

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 31, 2026

Copy link
Copy Markdown
Member

PHPUnit attributes exist since PHPUnit 10.0. Until now these rules converted annotations to attributes regardless of the PHPUnit version installed in the analysed project, so a PHPUnit 9 project ended up with attributes it cannot use.

Each of the 7 annotation to attribute rules now declares the constraint:

-final class TicketAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface
+final class TicketAnnotationToAttributeRector extends AbstractRector implements MinPhpVersionInterface, ComposerPackageConstraintInterface
 {
+    public function provideComposerPackageConstraint(): ComposerPackageConstraint
+    {
+        return new ComposerPackageConstraint('phpunit/phpunit', '>=10.0');
+    }

Covered rules:

  • TicketAnnotationToAttributeRector
  • TestWithAnnotationToAttributeRector
  • DataProviderAnnotationToAttributeRector
  • CoversAnnotationWithValueToAttributeRector
  • RequiresAnnotationWithValueToAttributeRector
  • DependsAnnotationWithValueToAttributeRector
  • AnnotationWithValueToAttributeRector

On a project with phpunit/phpunit: ^9.6, the rules are now skipped:

// stays as is, PHPUnit 9 has no attributes
final class SomeTest extends TestCase
{
    /**
     * @dataProvider provideData
     */
    public function testSome(): void
    {
    }
}

Note: ComposerPackageConstraintFilter treats an unresolvable version as "not satisfied", so these rules are also skipped when phpunit/phpunit is missing from the analysed project's installed.json.

For the generic AnnotationToAttributeRector pairs in annotations-to-attributes.php no change is needed - the set is pulled in by phpunit100.php, and version-specific pairs added later to phpunit110.php stack on top, as repeated ruleWithConfiguration() calls for the same rule class are merged.

PHP version bond

Attributes are a PHP 8.0 feature, so a rule that adds an attribute must also declare MinPhpVersionInterface. The 7 rules above already did. Three more rules add attributes and were missing it:

-final class AllowMockObjectsForDataProviderRector extends AbstractRector
+final class AllowMockObjectsForDataProviderRector extends AbstractRector implements MinPhpVersionInterface
 {
+    public function provideMinPhpVersion(): int
+    {
+        return PhpVersionFeature::ATTRIBUTES;
+    }
  • AllowMockObjectsForDataProviderRector
  • AllowMockObjectsWhereParentClassRector
  • AllowMockObjectsWithoutExpectationsAttributeRector

On a PHP 7.4 project they no longer produce a parse-error-causing #[AllowMockObjectsWithoutExpectations].

@TomasVotruba
TomasVotruba merged commit c25a1c1 into main Jul 31, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the annotation-rules-phpunit-10-constraint branch July 31, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant