Skip to content

Fix changing keys of data providers for tests with optional arguments - #783

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
tunterreitmeier:fix-optional-named-arguments-data-provider
Sep 17, 2026
Merged

TomasVotruba merged 1 commit into
rectorphp:mainfrom
tunterreitmeier:fix-optional-named-arguments-data-provider

Conversation

@tunterreitmeier

Copy link
Copy Markdown
Contributor

Hi, I noticed that the NamedArgumentForDataProviderRector currently tries to fix named arguments, even when every key matches an argument. This can break tests, when there are optional arguments involved.

So when running the rule on this example

final class TestWithOptionalArguments extends TestCase
{
    public static function dataProvider(): Generator
    {
        yield [
            'keyA' => false,
            'keyC' => true,
        ];
    }

    #[DataProvider('dataProvider')]
    public function testFilter(bool $keyA, bool $keyB = false, bool $keyC = false): void
    {
    }
}

one would end up with this diff:

yield [
  'keyA' => false,
-  'keyC' => true,
+  'keyB' => true,
];

My proposed fix would be to skip if all the keys are already all named arguments, using array_diff.

Hope that helps. Thanks a lot!

@tunterreitmeier
tunterreitmeier force-pushed the fix-optional-named-arguments-data-provider branch from 9a3deed to 5d41dc9 Compare September 17, 2026 06:39
@TomasVotruba
TomasVotruba merged commit cca51c7 into rectorphp:main Sep 17, 2026
7 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member

LGTM, Thank you 👍

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.

2 participants