Skip to content

Commit bcc2219

Browse files
committed
support yield
1 parent 1cae77c commit bcc2219

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/Rules/PHPUnit/DataProviderDataRule.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
88
use PHPStan\Rules\Rule;
9+
use PHPStan\ShouldNotHappenException;
910
use PHPUnit\Framework\TestCase;
1011
use function count;
1112

@@ -30,12 +31,29 @@ public function __construct(
3031

3132
public function getNodeType(): string
3233
{
33-
return Node\Stmt\Return_::class;
34+
return Node::class;
3435
}
3536

3637
public function processNode(Node $node, Scope $scope): array
3738
{
38-
if (!$node->expr instanceof Node\Expr\Array_) {
39+
if ($node instanceof Node\Stmt\Return_) {
40+
if (!$node->expr instanceof Node\Expr\Array_) {
41+
return [];
42+
}
43+
44+
$arrayExprs = [];
45+
foreach ($node->expr->items as $item) {
46+
if (!$item->value instanceof Node\Expr\Array_) {
47+
return [];
48+
}
49+
$arrayExprs[] = $item->value;
50+
}
51+
} elseif ($node instanceof Node\Expr\Yield_) {
52+
if (!$node->value instanceof Node\Expr\Array_) {
53+
return [];
54+
}
55+
$arrayExprs = [$node->value];
56+
} else {
3957
return [];
4058
}
4159

@@ -77,12 +95,12 @@ public function processNode(Node $node, Scope $scope): array
7795
return [];
7896
}
7997

80-
foreach ($node->expr->items as $item) {
81-
if (!$item->value instanceof Node\Expr\Array_) {
82-
continue;
98+
foreach ($arrayExprs as $arrayExpr) {
99+
if (!$arrayExpr instanceof Node\Expr\Array_) {
100+
throw new ShouldNotHappenException();
83101
}
84102

85-
$args = $this->arrayItemsToArgs($item->value);
103+
$args = $this->arrayItemsToArgs($arrayExpr);
86104
if ($args === null) {
87105
continue;
88106
}
@@ -92,7 +110,7 @@ public function processNode(Node $node, Scope $scope): array
92110
$var,
93111
$testsWithProvider[0]->getName(),
94112
$args,
95-
['startLine' => $item->getStartLine()],
113+
['startLine' => $arrayExpr->getStartLine()],
96114
));
97115
}
98116

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public function testRule(): void
5656
'Parameter #2 $input of method DataProviderDataTest\BarTest::testWithAnnotation() expects string, false given.',
5757
54,
5858
],
59+
[
60+
'Parameter #2 $input of method DataProviderDataTest\YieldingTest::testYield() expects string, int given.',
61+
79,
62+
],
63+
[
64+
'Parameter #2 $input of method DataProviderDataTest\YieldingTest::testYield() expects string, false given.',
65+
85,
66+
],
5967
]);
6068
}
6169

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,33 @@ public function aProvider(): array
5959
}
6060
}
6161

62+
class YieldingTest extends TestCase
63+
{
64+
65+
/** @dataProvider yieldProvider */
66+
public function testYield(string $expectedResult, string $input): void
67+
{
68+
}
69+
70+
public function yieldProvider(): iterable
71+
{
72+
yield
73+
[
74+
'Hello World',
75+
" Hello World \n",
76+
];
77+
78+
yield
79+
[
80+
'Hello World',
81+
123,
82+
];
83+
84+
yield
85+
[
86+
'Hello World',
87+
false,
88+
];
89+
}
90+
}
91+

0 commit comments

Comments
 (0)