Skip to content

Commit 35c1bf4

Browse files
authored
[TypeDeclaration] Handle with inner function on AddParamTypeBasedOnPHPUnitDataProviderRector (#5786)
1 parent d869306 commit 35c1bf4

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;
4+
5+
use Iterator;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class WithInnerFunction extends TestCase
9+
{
10+
/**
11+
* @dataProvider provideData()
12+
*/
13+
public function test($name)
14+
{
15+
}
16+
17+
public function provideData()
18+
{
19+
function () {
20+
return [['some']];
21+
};
22+
23+
yield [1];
24+
}
25+
}
26+
27+
?>
28+
-----
29+
<?php
30+
31+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture;
32+
33+
use Iterator;
34+
use PHPUnit\Framework\TestCase;
35+
36+
final class WithInnerFunction extends TestCase
37+
{
38+
/**
39+
* @dataProvider provideData()
40+
*/
41+
public function test(int $name)
42+
{
43+
}
44+
45+
public function provideData()
46+
{
47+
function () {
48+
return [['some']];
49+
};
50+
51+
yield [1];
52+
}
53+
}
54+
55+
?>

rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ private function inferParam(
161161
}
162162

163163
/** @var Return_[] $returns */
164-
$returns = $this->betterNodeFinder->findInstanceOf((array) $dataProviderClassMethod->stmts, Return_::class);
164+
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($dataProviderClassMethod, Return_::class);
165165
if ($returns !== []) {
166166
return $this->resolveReturnStaticArrayTypeByParameterPosition($returns, $parameterPosition);
167167
}
168168

169169
/** @var Yield_[] $yields */
170-
$yields = $this->betterNodeFinder->findInstanceOf((array) $dataProviderClassMethod->stmts, Yield_::class);
170+
$yields = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($dataProviderClassMethod, Yield_::class);
171171
return $this->resolveYieldStaticArrayTypeByParameterPosition($yields, $parameterPosition);
172172
}
173173

0 commit comments

Comments
 (0)