Skip to content

Commit 9b69121

Browse files
committed
use types
1 parent 1506a06 commit 9b69121

File tree

5 files changed

+68
-34
lines changed

5 files changed

+68
-34
lines changed

src/Rules/PHPUnit/DataProviderDataRule.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Node\Expr\TypeExpr;
78
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
89
use PHPStan\Rules\Rule;
9-
use PHPStan\ShouldNotHappenException;
10+
use PHPStan\Type\Constant\ConstantArrayType;
1011
use PHPUnit\Framework\TestCase;
1112
use function array_slice;
1213
use function count;
@@ -40,22 +41,28 @@ public function getNodeType(): string
4041
public function processNode(Node $node, Scope $scope): array
4142
{
4243
if ($node instanceof Node\Stmt\Return_ || $node instanceof Node\Expr\YieldFrom) {
43-
if (!$node->expr instanceof Node\Expr\Array_) {
44+
$exprType = $scope->getType($node->expr);
45+
if (!$exprType->isConstantArray()->yes()) {
4446
return [];
4547
}
4648

47-
$arrayExprs = [];
48-
foreach ($node->expr->items as $item) {
49-
if (!$item->value instanceof Node\Expr\Array_) {
50-
return [];
49+
$constArrays = $exprType->getConstantArrays();
50+
$constantArrays = [];
51+
foreach ($constArrays as $constArray) {
52+
foreach ($constArray->getValueTypes() as $valueType) {
53+
if (!$valueType->isConstantArray()->yes()) {
54+
return [];
55+
}
56+
$constantArrays[] = $valueType;
5157
}
52-
$arrayExprs[] = $item->value;
5358
}
5459
} elseif ($node instanceof Node\Expr\Yield_) {
55-
if (!$node->value instanceof Node\Expr\Array_) {
60+
$exprType = $scope->getType($node->value);
61+
if (!$exprType->isConstantArray()->yes()) {
5662
return [];
5763
}
58-
$arrayExprs = [$node->value];
64+
65+
$constantArrays = $exprType->getConstantArrays();
5966
} else {
6067
return [];
6168
}
@@ -105,8 +112,8 @@ public function processNode(Node $node, Scope $scope): array
105112
}
106113

107114
foreach ($testsWithProvider as $testMethod) {
108-
foreach ($arrayExprs as $arrayExpr) {
109-
$args = $this->arrayItemsToArgs($arrayExpr);
115+
foreach ($constantArrays as $constantArray) {
116+
$args = $this->arrayItemsToArgs($constantArray);
110117
if ($args === null) {
111118
continue;
112119
}
@@ -120,7 +127,7 @@ public function processNode(Node $node, Scope $scope): array
120127
$var,
121128
$testMethod->getName(),
122129
$args,
123-
['startLine' => $arrayExpr->getStartLine()],
130+
['startLine' => $node->getStartLine()],
124131
));
125132
}
126133
}
@@ -131,15 +138,13 @@ public function processNode(Node $node, Scope $scope): array
131138
/**
132139
* @return array<Node\Arg>
133140
*/
134-
private function arrayItemsToArgs(Node\Expr\Array_ $array): ?array
141+
private function arrayItemsToArgs(ConstantArrayType $array): ?array
135142
{
136143
$args = [];
137144

138-
foreach ($array->items as $item) {
145+
foreach ($array->getValueTypes() as $valueType) {
139146
// XXX named args
140-
$value = $item->value;
141-
142-
$arg = new Node\Arg($value);
147+
$arg = new Node\Arg(new TypeExpr($valueType));
143148
$args[] = $arg;
144149
}
145150

src/Rules/PHPUnit/TestMethodsHelper.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace PHPStan\Rules\PHPUnit;
44

55
use PHPStan\Analyser\Scope;
6-
use PHPStan\Parser\Parser;
76
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
87
use PHPStan\Reflection\ClassReflection;
9-
use PHPStan\Reflection\ReflectionProvider;
108
use PHPStan\Type\FileTypeMapper;
119
use ReflectionMethod;
1210
use function str_starts_with;

src/Rules/PHPUnit/TestMethodsHelperFactory.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace PHPStan\Rules\PHPUnit;
44

5-
use PHPStan\Parser\Parser;
6-
use PHPStan\Reflection\ReflectionProvider;
75
use PHPStan\Type\FileTypeMapper;
86

97
class TestMethodsHelperFactory
108
{
119

12-
1310
private FileTypeMapper $fileTypeMapper;
1411

1512
private PHPUnitVersionDetector $PHPUnitVersionDetector;

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,51 +52,55 @@ public function testRule(): void
5252
$this->analyse([__DIR__ . '/data/data-provider-data.php'], [
5353
[
5454
'Parameter #2 $input of method DataProviderDataTest\FooTest::testWithAttribute() expects string, int given.',
55-
24,
55+
19,
5656
],
5757
[
5858
'Parameter #2 $input of method DataProviderDataTest\FooTest::testWithAttribute() expects string, false given.',
59-
28,
59+
19,
6060
],
6161
[
6262
'Parameter #2 $input of method DataProviderDataTest\BarTest::testWithAnnotation() expects string, int given.',
63-
51,
63+
46,
6464
],
6565
[
6666
'Parameter #2 $input of method DataProviderDataTest\BarTest::testWithAnnotation() expects string, false given.',
67-
55,
67+
46,
6868
],
6969
[
7070
'Parameter #2 $input of method DataProviderDataTest\YieldTest::myTestMethod() expects string, int given.',
71-
81,
71+
80,
7272
],
7373
[
7474
'Parameter #2 $input of method DataProviderDataTest\YieldTest::myTestMethod() expects string, false given.',
75-
87,
75+
86,
7676
],
7777
[
7878
'Parameter #2 $input of method DataProviderDataTest\YieldFromTest::myTestMethod() expects string, int given.',
79-
112,
79+
107,
8080
],
8181
[
8282
'Parameter #2 $input of method DataProviderDataTest\YieldFromTest::myTestMethod() expects string, false given.',
83-
116,
83+
107,
8484
],
8585
[
8686
'Method DataProviderDataTest\DifferentArgumentCount::testFoo() invoked with 3 parameters, 2 required.',
87-
141,
87+
136,
8888
],
8989
[
9090
'Method DataProviderDataTest\DifferentArgumentCount::testFoo() invoked with 1 parameter, 2 required.',
91-
146,
91+
136,
9292
],
9393
[
9494
'Method DataProviderDataTest\DifferentArgumentCountWithReusedDataprovider::testFoo() invoked with 3 parameters, 2 required.',
95-
177,
95+
172,
9696
],
9797
[
9898
'Method DataProviderDataTest\DifferentArgumentCountWithReusedDataprovider::testFoo() invoked with 1 parameter, 2 required.',
99-
182,
99+
172,
100+
],
101+
[
102+
'Parameter #2 $input of method DataProviderDataTest\UnionTypeReturnTest::testWithAnnotation() expects string, int given.',
103+
216,
100104
],
101105
]);
102106
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,33 @@ public function yieldProvider(): iterable
186186
}
187187
}
188188

189+
190+
class UnionTypeReturnTest extends TestCase
191+
{
192+
193+
/** @dataProvider aProvider */
194+
public function testWithAnnotation(string $expectedResult, string $input): void
195+
{
196+
}
197+
198+
public function aProvider(): array
199+
{
200+
$arr = [
201+
[
202+
'Hello World',
203+
" Hello World \n"
204+
]
205+
];
206+
207+
if (rand(0,1)) {
208+
$arr = [
209+
[
210+
'Hello World',
211+
123
212+
]
213+
];
214+
}
215+
216+
return $arr;
217+
}
218+
}

0 commit comments

Comments
 (0)