Skip to content

Commit a3436e6

Browse files
authored
Fix DataProviderDeclaration rule might fix wrong same named method in file (#367)
* Fix DataProviderDeclaration rule might fix wrong same named file * Fix DataProviderDeclaration rule might fix wrong same named file * Fix DataProviderDeclaration rule fixing a dataprovider from a different file * Fix DataProviderDeclaration rule fixing a dataprovider from a trait
1 parent 8c16263 commit a3436e6

13 files changed

Lines changed: 304 additions & 7 deletions

‎src/Rules/PHPUnit/DataProviderHelper.php‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use PhpParser\Comment\Doc;
66
use PhpParser\Modifiers;
7+
use PhpParser\Node;
78
use PhpParser\Node\Attribute;
89
use PhpParser\Node\Expr\ClassConstFetch;
910
use PhpParser\Node\Name;
1011
use PhpParser\Node\Scalar\String_;
12+
use PhpParser\Node\Stmt\ClassLike;
1113
use PhpParser\Node\Stmt\ClassMethod;
1214
use PhpParser\NodeFinder;
1315
use PHPStan\Analyser\Scope;
@@ -168,18 +170,22 @@ public function processDataProvider(
168170
->line($lineNumber)
169171
->identifier('phpunit.dataProviderStatic');
170172

171-
$dataProviderMethodReflectionDeclaringClass = $dataProviderMethodReflection->getDeclaringClass();
172-
if ($dataProviderMethodReflectionDeclaringClass->getFileName() !== null) {
173-
$stmts = $this->parser->parseFile($dataProviderMethodReflectionDeclaringClass->getFileName());
174-
$nodeFinder = new NodeFinder();
175-
/** @var ClassMethod|null $methodNode */
176-
$methodNode = $nodeFinder->findFirst($stmts, static fn ($node) => $node instanceof ClassMethod && $node->name->toString() === $dataProviderMethodReflection->getName());
173+
// a method from a trait is declared in the trait, which can be in another file
174+
$nativeMethod = $dataProviderMethodReflection->getDeclaringClass()->getNativeReflection()->getMethod($dataProviderMethodReflection->getName())->getBetterReflection();
175+
$declaringClassFileName = $nativeMethod->getFileName();
176+
if ($declaringClassFileName !== null) {
177+
$stmts = $this->parser->parseFile($declaringClassFileName);
178+
$methodNode = $this->findMethodNode(
179+
$stmts,
180+
$nativeMethod->getDeclaringClass()->getName(),
181+
$dataProviderMethodReflection->getName(),
182+
);
177183
if ($methodNode !== null) {
178184
$errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) {
179185
$methodNode->flags |= Modifiers::STATIC;
180186

181187
return $methodNode;
182-
});
188+
}, $declaringClassFileName);
183189
}
184190
}
185191
$errors[] = $errorBuilder->build();
@@ -188,6 +194,27 @@ public function processDataProvider(
188194
return $errors;
189195
}
190196

197+
/**
198+
* @param Node[] $stmts
199+
*/
200+
private function findMethodNode(array $stmts, string $className, string $methodName): ?ClassMethod
201+
{
202+
$nodeFinder = new NodeFinder();
203+
204+
/** @var ClassLike|null $classNode */
205+
$classNode = $nodeFinder->findFirst(
206+
$stmts,
207+
static fn (Node $node): bool => $node instanceof ClassLike
208+
&& $node->namespacedName !== null
209+
&& $node->namespacedName->toString() === $className,
210+
);
211+
if ($classNode === null) {
212+
return null;
213+
}
214+
215+
return $classNode->getMethod($methodName);
216+
}
217+
191218
private function getDataProviderAnnotationValue(PhpDocTagNode $phpDocTag): ?string
192219
{
193220
if (preg_match('/^[^ \t]+/', (string) $phpDocTag->value, $matches) !== 1) {

‎tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,49 @@ public function testFixDataProviderStatic(): void
118118
$this->fix(__DIR__ . '/data/data-provider-static-fix.php', __DIR__ . '/data/data-provider-static-fix.php.fixed');
119119
}
120120

121+
public function testFixDataProviderStaticWithSameNamedMethodInOtherClass(): void
122+
{
123+
$this->phpunitVersion = 10;
124+
125+
$this->fix(__DIR__ . '/data/data-provider-static-fix-same-name.php', __DIR__ . '/data/data-provider-static-fix-same-name.php.fixed');
126+
}
127+
128+
public function testFixDataProviderStaticDoesNotChangeMethodAtSamePositionInTestFile(): void
129+
{
130+
$this->phpunitVersion = 10;
131+
132+
$this->fix(__DIR__ . '/data/data-provider-static-fix-parent.php', __DIR__ . '/data/data-provider-static-fix-parent.php.fixed');
133+
}
134+
135+
public function testFixDataProviderStaticDeclaredInParentClassFile(): void
136+
{
137+
$this->phpunitVersion = 10;
138+
139+
$this->fixOtherFile(
140+
__DIR__ . '/data/data-provider-static-fix-parent.php',
141+
__DIR__ . '/data/data-provider-static-fix-parent-base.php',
142+
__DIR__ . '/data/data-provider-static-fix-parent-base.php.fixed',
143+
);
144+
}
145+
146+
public function testFixDataProviderStaticDeclaredInTraitInSameFile(): void
147+
{
148+
$this->phpunitVersion = 10;
149+
150+
$this->fix(__DIR__ . '/data/data-provider-static-fix-trait.php', __DIR__ . '/data/data-provider-static-fix-trait.php.fixed');
151+
}
152+
153+
public function testFixDataProviderStaticDeclaredInTraitInOtherFile(): void
154+
{
155+
$this->phpunitVersion = 10;
156+
157+
$this->fixOtherFile(
158+
__DIR__ . '/data/data-provider-static-fix-trait-other-file.php',
159+
__DIR__ . '/data/data-provider-static-fix-trait-other-file-trait.php',
160+
__DIR__ . '/data/data-provider-static-fix-trait-other-file-trait.php.fixed',
161+
);
162+
}
163+
121164
/**
122165
* @return string[]
123166
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderStaticFixParent;
4+
5+
abstract class BaseTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
public function provide(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderStaticFixParent;
4+
5+
abstract class BaseTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
public static function provide(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderStaticFixParent;
4+
5+
class ChildTestCaseUsingParentProviderTest extends BaseTestCase
6+
{
7+
8+
public function helpers(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
/**
14+
* @dataProvider provide
15+
*/
16+
public function testFoo(): void
17+
{
18+
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderStaticFixParent;
4+
5+
class ChildTestCaseUsingParentProviderTest extends BaseTestCase
6+
{
7+
8+
public function helpers(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
/**
14+
* @dataProvider provide
15+
*/
16+
public function testFoo(): void
17+
{
18+
19+
}
20+
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderStaticFixSameName;
4+
5+
class Unrelated
6+
{
7+
8+
public function dataProvide(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
}
14+
15+
class FixTestCase extends \PHPUnit\Framework\TestCase
16+
{
17+
18+
public function dataProvide(): iterable
19+
{
20+
yield [];
21+
}
22+
23+
/**
24+
* @dataProvider dataProvide
25+
*/
26+
public function testFoo(): void
27+
{
28+
29+
}
30+
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderStaticFixSameName;
4+
5+
class Unrelated
6+
{
7+
8+
public function dataProvide(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
}
14+
15+
class FixTestCase extends \PHPUnit\Framework\TestCase
16+
{
17+
18+
public static function dataProvide(): iterable
19+
{
20+
yield [];
21+
}
22+
23+
/**
24+
* @dataProvider dataProvide
25+
*/
26+
public function testFoo(): void
27+
{
28+
29+
}
30+
31+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderStaticFixTraitOtherFile;
4+
5+
trait ProvidesData
6+
{
7+
8+
public function provide(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DataProviderStaticFixTraitOtherFile;
4+
5+
trait ProvidesData
6+
{
7+
8+
public static function provide(): iterable
9+
{
10+
yield [];
11+
}
12+
13+
}

0 commit comments

Comments
 (0)