Skip to content

Commit c689efe

Browse files
committed
special case for providers only containing static data, so we get more precise error lines
1 parent 7f9dbf2 commit c689efe

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

src/Rules/PHPUnit/DataProviderDataRule.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function processNode(Node $node, Scope $scope): array
9393
foreach ($testsWithProvider as $testMethod) {
9494
$numberOfParameters = $testMethod->getNumberOfParameters();
9595

96-
foreach ($arraysTypes as $arraysType) {
96+
foreach ($arraysTypes as [$startLine, $arraysType]) {
9797
$args = $this->arrayItemsToArgs($arraysType, $numberOfParameters);
9898
if ($args === null) {
9999
continue;
@@ -107,7 +107,7 @@ public function processNode(Node $node, Scope $scope): array
107107
new TypeExpr(new ObjectType($classReflection->getName())),
108108
$testMethod->getName(),
109109
$args,
110-
['startLine' => $node->getStartLine()],
110+
['startLine' => $startLine],
111111
));
112112
}
113113
}
@@ -165,11 +165,38 @@ private function arrayItemsToArgs(Type $array, int $numberOfParameters): ?array
165165

166166
/**
167167
* @param Node\Stmt\Return_|Node\Expr\Yield_|Node\Expr\YieldFrom $node
168-
* @return array<Type>
168+
*
169+
* @return list<list{int, Type}>
169170
*/
170171
private function buildArrayTypesFromNode(Node $node, Scope $scope): array
171172
{
172173
$arraysTypes = [];
174+
175+
// special case for providers only containing static data, so we get more precise error lines
176+
if ($node instanceof Node\Stmt\Return_ && $node->expr instanceof Node\Expr\Array_) {
177+
foreach ($node->expr->items as $item) {
178+
if (!$item->value instanceof Node\Expr\Array_) {
179+
$arraysTypes = [];
180+
break;
181+
}
182+
183+
$constArrays = $scope->getType($item->value)->getConstantArrays();
184+
if ($constArrays === []) {
185+
$arraysTypes = [];
186+
break;
187+
}
188+
189+
foreach ($constArrays as $constArray) {
190+
$arraysTypes[] = [$item->value->getStartLine(), $constArray];
191+
}
192+
}
193+
194+
if ($arraysTypes !== []) {
195+
return $arraysTypes;
196+
}
197+
}
198+
199+
// general case with less precise error message lines
173200
if ($node instanceof Node\Stmt\Return_ || $node instanceof Node\Expr\YieldFrom) {
174201
if ($node->expr === null) {
175202
return [];
@@ -180,21 +207,25 @@ private function buildArrayTypesFromNode(Node $node, Scope $scope): array
180207
foreach ($exprConstArrays as $constArray) {
181208
foreach ($constArray->getValueTypes() as $valueType) {
182209
foreach ($valueType->getConstantArrays() as $constValueArray) {
183-
$arraysTypes[] = $constValueArray;
210+
$arraysTypes[] = [$node->getStartLine(), $constValueArray];
184211
}
185212
}
186213
}
187214

188215
if ($arraysTypes === []) {
189-
$arraysTypes = $exprType->getIterableValueType()->getArrays();
216+
foreach ($exprType->getIterableValueType()->getArrays() as $arrayType) {
217+
$arraysTypes[] = [$node->getStartLine(), $arrayType];
218+
}
190219
}
191220
} elseif ($node instanceof Node\Expr\Yield_) {
192221
if ($node->value === null) {
193222
return [];
194223
}
195224

196225
$exprType = $scope->getType($node->value);
197-
$arraysTypes = $exprType->getConstantArrays();
226+
foreach ($exprType->getConstantArrays() as $constValueArray) {
227+
$arraysTypes[] = [$node->getStartLine(), $constValueArray];
228+
}
198229
}
199230

200231
return $arraysTypes;

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ public function testRule(): void
4949
$this->analyse([__DIR__ . '/data/data-provider-data.php'], [
5050
[
5151
'Parameter #2 $input of method DataProviderDataTest\FooTest::testWithAttribute() expects string, int given.',
52-
19,
52+
24,
5353
],
5454
[
5555
'Parameter #2 $input of method DataProviderDataTest\FooTest::testWithAttribute() expects string, false given.',
56-
19,
56+
28,
5757
],
5858
[
5959
'Parameter #2 $input of method DataProviderDataTest\BarTest::testWithAnnotation() expects string, int given.',
60-
46,
60+
51,
6161
],
6262
[
6363
'Parameter #2 $input of method DataProviderDataTest\BarTest::testWithAnnotation() expects string, false given.',
64-
46,
64+
55,
6565
],
6666
[
6767
'Parameter #2 $input of method DataProviderDataTest\YieldTest::myTestMethod() expects string, int given.',
@@ -117,27 +117,27 @@ public function testRule(): void
117117
],
118118
[
119119
'Parameter #1 $si of method DataProviderDataTest\TestInvalidVariadic::testBar() expects int, string given.',
120-
333,
120+
334,
121121
],
122122
[
123123
'Parameter #1 $s of method DataProviderDataTest\TestInvalidVariadic::testFoo() expects string, int given.',
124-
333,
124+
335,
125125
],
126126
[
127127
'Parameter #1 $si of method DataProviderDataTest\TestInvalidVariadic2::testBar() expects int, string given.',
128-
355,
128+
356,
129129
],
130130
[
131131
'Parameter #2 ...$moreS of method DataProviderDataTest\TestInvalidVariadic2::testFoo() expects int, string given.',
132-
355,
132+
356,
133133
],
134134
[
135135
'Parameter #4 ...$moreS of method DataProviderDataTest\TestInvalidVariadic2::testFoo() expects int, string given.',
136-
355,
136+
356,
137137
],
138138
[
139139
'Parameter #1 $s of method DataProviderDataTest\TestInvalidVariadic2::testFoo() expects string, int given.',
140-
355,
140+
357,
141141
],
142142
[
143143
'Unknown parameter $foo in call to method DataProviderDataTest\TestArrayShapeIterable::testBar().',
@@ -165,11 +165,11 @@ public function testRule(): void
165165
],
166166
[
167167
'Parameter #2 $input of method DataProviderDataTest\AbstractBaseTest::testWithAttribute() expects string, int given.',
168-
461,
168+
466,
169169
],
170170
[
171171
'Parameter #2 $input of method DataProviderDataTest\AbstractBaseTest::testWithAttribute() expects string, false given.',
172-
461,
172+
470,
173173
],
174174
[
175175
'Parameter #2 $input of method DataProviderDataTest\ConstantArrayUnionTypeReturnTest::testFoo() expects string, int given.',

0 commit comments

Comments
 (0)