rector icon indicating copy to clipboard operation
rector copied to clipboard

Create a rule to annotate a data provider (PHPUnit) with correct array based the test method signature that uses the data provider

Open DaveLiddament opened this issue 1 year ago • 3 comments

Feature Request

I would like a Rector to add the correct return type docblock to a PHPUnit data provider method, based on the signature of the test method that references the data provider.

Rector would look for methods with the #[DataProvicder] attribute, it could then annotate the referenced data provider method, if it isn't already annotated, with relevant docblock,

E.g Before

final class NumericDataSetsTest extends TestCase
{
    public static function additionProvider(): array
    {
        return [
            [0, 0, 0],
            [0, 1, 1],
            [1, 0, 1],
            [1, 1, 3],
        ];
    }

    #[DataProvider('additionProvider')]
    public function testAdd(int $a, int $b, int $expected): void
    {
        $this->assertSame($expected, $a + $b);
    }
}

Rector can work out from the testAdd parameters that the data provider should be: /** @return array<array-key,array<int,int,int>> */

Diff

 final class NumericDataSetsTest extends TestCase
 {
+     /** @return array<array-key,array<int,int,int>> */
     public static function additionProvider(): array
     {
         return [
            [0, 0, 0],
            [0, 1, 1],
            [1, 0, 1],
            [1, 1, 3],
        ];
     }

     #[DataProvider('additionProvider')]
     public function testAdd(int $a, int $b, int $expected): void
     {
         $this->assertSame($expected, $a + $b);
     }
 }

DaveLiddament avatar Aug 20 '24 15:08 DaveLiddament

@DaveLiddament I guess it shoud be @return ?

carlos-granados avatar Aug 20 '24 17:08 carlos-granados

@DaveLiddament I guess it shoud be @return ?

Good spot. Updated. Thanks

DaveLiddament avatar Aug 21 '24 15:08 DaveLiddament

We had such a rule before but it was opinionated how complex array shapes should be included or skipped.

Proof of concept would be great 👍

TomasVotruba avatar Aug 21 '24 16:08 TomasVotruba

Closing as accepted to avoid keep issue tracker focus on unclear issues that needs our attention.

Feel free to kick of with a proof of concept PR 👍
https://github.com/rectorphp/rector-phpunit would be best to target

TomasVotruba avatar Mar 20 '25 10:03 TomasVotruba