Skip to content

Commit 1532c26

Browse files
committed
support iterables
1 parent e31a234 commit 1532c26

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

src/Rules/PHPUnit/DataProviderDataRule.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,31 @@ public function getNodeType(): string
4040

4141
public function processNode(Node $node, Scope $scope): array
4242
{
43+
if ($scope->getFunction() === null) {
44+
return [];
45+
}
46+
if ($scope->isInAnonymousFunction()) {
47+
return [];
48+
}
49+
4350
if ($node instanceof Node\Stmt\Return_ || $node instanceof Node\Expr\YieldFrom) {
4451
if ($node->expr === null) {
4552
return [];
4653
}
4754

48-
$exprType = $scope->getType($node->expr);
49-
if (!$exprType->isConstantArray()->yes()) {
50-
return [];
51-
}
52-
53-
$constArrays = $exprType->getConstantArrays();
5455
$constantArrays = [];
55-
foreach ($constArrays as $constArray) {
56-
foreach ($constArray->getValueTypes() as $valueType) {
57-
foreach ($valueType->getConstantArrays() as $constValueArray) {
58-
$constantArrays[] = $constValueArray;
56+
$exprType = $scope->getType($node->expr);
57+
$exprConstArrays = $exprType->getConstantArrays();
58+
if ($exprConstArrays !== []) {
59+
foreach ($exprConstArrays as $constArray) {
60+
foreach ($constArray->getValueTypes() as $valueType) {
61+
foreach ($valueType->getConstantArrays() as $constValueArray) {
62+
$constantArrays[] = $constValueArray;
63+
}
5964
}
6065
}
66+
} else {
67+
$constantArrays = $exprType->getIterableValueType()->getConstantArrays();
6168
}
6269
} elseif ($node instanceof Node\Expr\Yield_) {
6370
if ($node->value === null) {
@@ -74,11 +81,7 @@ public function processNode(Node $node, Scope $scope): array
7481
return [];
7582
}
7683

77-
if ($scope->getFunction() === null) {
78-
return [];
79-
}
80-
81-
if ($scope->isInAnonymousFunction()) {
84+
if ($constantArrays === []) {
8285
return [];
8386
}
8487

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ public function testRule(): void
138138
'Parameter #1 $s of method DataProviderDataTest\TestInvalidVariadic2::testFoo() expects string, int given.',
139139
355,
140140
],
141+
[
142+
'Unknown parameter $foo in call to method DataProviderDataTest\TestIterable::testBar().',
143+
371,
144+
],
145+
[
146+
'Missing parameter $si (int) in call to method DataProviderDataTest\TestIterable::testBar().',
147+
371,
148+
],
141149
]);
142150
}
143151

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,23 @@ public function aProvider(): iterable
358358
];
359359
}
360360
}
361+
362+
class TestIterable extends TestCase
363+
{
364+
/** @dataProvider aProvider */
365+
public function testBar(int $si): void
366+
{
367+
}
368+
369+
public function aProvider(): iterable
370+
{
371+
return $this->data();
372+
}
373+
374+
/**
375+
* @return iterable<array{foo: string}>
376+
*/
377+
public function data(): iterable
378+
{
379+
}
380+
}

0 commit comments

Comments
 (0)