Skip to content

Commit df82e59

Browse files
Fix internal error with named DataProvider arguments (phpstan#363)
* Read the data provider name from a named attribute argument * docs * Update EmptyDataProviderRuleTest.php --------- Co-authored-by: Mohannad Elemary <20709195+MohannadElemary@users.noreply.github.com> Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
1 parent a5027a5 commit df82e59

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎src/Rules/PHPUnit/DataProviderHelper.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use function array_merge;
2525
use function count;
2626
use function explode;
27+
use function is_string;
2728
use function preg_match;
2829
use function sprintf;
2930

@@ -287,9 +288,16 @@ private function yieldDataProviderAttributes($node, ClassReflection $classReflec
287288
continue;
288289
}
289290

291+
// phpunit does not support named arguments.
292+
// we handle them here just to make sure we don't crash on them.
293+
$methodName = $args[0] ?? $args['methodName'] ?? null;
294+
if (!is_string($methodName)) {
295+
continue;
296+
}
297+
290298
$startLine = $node->getStartLine();
291299

292-
yield [$classReflection, $args[0], $startLine];
300+
yield [$classReflection, $methodName, $startLine];
293301
}
294302

295303
return;

‎tests/Rules/PHPUnit/EmptyDataProviderRuleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function testRule(?int $version): void
7575
'Data provider method provideData() provides no data sets, which is an error in PHPUnit 10 and newer.',
7676
173,
7777
];
78+
$errors[] = [
79+
'Data provider method provideData() provides no data sets, which is an error in PHPUnit 10 and newer.',
80+
336,
81+
];
7882
}
7983

8084
$this->analyse([__DIR__ . '/data/empty-data-provider.php'], $errors);

‎tests/Rules/PHPUnit/data/empty-data-provider.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,20 @@ public function dataFileAsserts(): iterable
322322
*/
323323
public function testFileAsserts() {}
324324
}
325+
326+
class NamedArgumentAttributeTest extends TestCase
327+
{
328+
329+
// phpunit does not support named arguments.
330+
// we handle them here just to make sure we don't crash on them.
331+
#[DataProvider(methodName: 'provideData')]
332+
public function testFoo(string $input): void
333+
{
334+
}
335+
336+
public static function provideData(): iterable
337+
{
338+
return [];
339+
}
340+
341+
}

0 commit comments

Comments
 (0)