Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DaveLiddament opened this issue Aug 20, 2024 · 3 comments
Labels

Comments

@DaveLiddament
Copy link
Contributor

DaveLiddament commented Aug 20, 2024

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);
     }
 }
@carlos-granados
Copy link

@DaveLiddament I guess it shoud be @return ?

@DaveLiddament
Copy link
Contributor Author

@DaveLiddament I guess it shoud be @return ?

Good spot. Updated. Thanks

@TomasVotruba
Copy link
Member

TomasVotruba commented Aug 21, 2024

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants