Skip to content

AddParamTypeBasedOnPHPUnitDataProviderRector: Be able to handle #[DataProvider] attribute as well as PHPDoc #8179

@mcampbell508

Description

@mcampbell508

Feature Request

Currently, it seems this rule only works for test providers declared with the PHPDoc and not for ones using the new Attribute available in PHPUnit 10 (#[\PHPUnit\Framework\Attributes\DataProvider('provideData')]). I am not sure if this was a decision made in the past due to the fact PHPUnit 10 being fairly new or it was something not available when this rule was added.

If desired, I think there is potential to add the ability for this if we add something like (pseduo):

$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ATTRIBUTES) && $this->phpAttributeAnalyzer->hasPhpAttribute(
    $classMethod,
    'PHPUnit\Framework\Attributes\DataProvider'
); 

possibly making use of Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer https://github.com/rectorphp/rector-src/blob/824bb97770867b1584fa7c2398d0d59b7cd4f36e/rules/Php80/NodeAnalyzer/PhpAttributeAnalyzer.php#L31.

a bit like the following code does in https://github.com/rectorphp/rector-phpunit/blob/main/rules/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php#L129-L132 to check if an attribute exists.

We would also have to add the code that gets the name of the provider, if one exists.

Demo

https://getrector.com/demo/8c46ffbc-8b0a-4c11-9a97-248f70116383

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions