diff --git a/src/Rules/PHPUnit/DataProviderHelper.php b/src/Rules/PHPUnit/DataProviderHelper.php index aa453a8..28376ca 100644 --- a/src/Rules/PHPUnit/DataProviderHelper.php +++ b/src/Rules/PHPUnit/DataProviderHelper.php @@ -4,10 +4,12 @@ use PhpParser\Comment\Doc; use PhpParser\Modifiers; +use PhpParser\Node; use PhpParser\Node\Attribute; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; +use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\NodeFinder; use PHPStan\Analyser\Scope; @@ -169,17 +171,20 @@ public function processDataProvider( ->identifier('phpunit.dataProviderStatic'); $dataProviderMethodReflectionDeclaringClass = $dataProviderMethodReflection->getDeclaringClass(); - if ($dataProviderMethodReflectionDeclaringClass->getFileName() !== null) { - $stmts = $this->parser->parseFile($dataProviderMethodReflectionDeclaringClass->getFileName()); - $nodeFinder = new NodeFinder(); - /** @var ClassMethod|null $methodNode */ - $methodNode = $nodeFinder->findFirst($stmts, static fn ($node) => $node instanceof ClassMethod && $node->name->toString() === $dataProviderMethodReflection->getName()); + $declaringClassFileName = $dataProviderMethodReflectionDeclaringClass->getFileName(); + if ($declaringClassFileName !== null) { + $stmts = $this->parser->parseFile($declaringClassFileName); + $methodNode = $this->findMethodNode( + $stmts, + $dataProviderMethodReflectionDeclaringClass->getName(), + $dataProviderMethodReflection->getName(), + ); if ($methodNode !== null) { $errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) { $methodNode->flags |= Modifiers::STATIC; return $methodNode; - }); + }, $declaringClassFileName); } } $errors[] = $errorBuilder->build(); @@ -188,6 +193,27 @@ public function processDataProvider( return $errors; } + /** + * @param Node[] $stmts + */ + private function findMethodNode(array $stmts, string $className, string $methodName): ?ClassMethod + { + $nodeFinder = new NodeFinder(); + + /** @var ClassLike|null $classNode */ + $classNode = $nodeFinder->findFirst( + $stmts, + static fn (Node $node): bool => $node instanceof ClassLike + && $node->namespacedName !== null + && $node->namespacedName->toString() === $className, + ); + if ($classNode === null) { + return null; + } + + return $classNode->getMethod($methodName); + } + private function getDataProviderAnnotationValue(PhpDocTagNode $phpDocTag): ?string { if (preg_match('/^[^ \t]+/', (string) $phpDocTag->value, $matches) !== 1) { diff --git a/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php b/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php index f63c9e6..ad0fbee 100644 --- a/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php +++ b/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php @@ -118,6 +118,31 @@ public function testFixDataProviderStatic(): void $this->fix(__DIR__ . '/data/data-provider-static-fix.php', __DIR__ . '/data/data-provider-static-fix.php.fixed'); } + public function testFixDataProviderStaticWithSameNamedMethodInOtherClass(): void + { + $this->phpunitVersion = 10; + + $this->fix(__DIR__ . '/data/data-provider-static-fix-same-name.php', __DIR__ . '/data/data-provider-static-fix-same-name.php.fixed'); + } + + public function testFixDataProviderStaticDoesNotChangeMethodAtSamePositionInTestFile(): void + { + $this->phpunitVersion = 10; + + $this->fix(__DIR__ . '/data/data-provider-static-fix-parent.php', __DIR__ . '/data/data-provider-static-fix-parent.php.fixed'); + } + + public function testFixDataProviderStaticDeclaredInParentClassFile(): void + { + $this->phpunitVersion = 10; + + $this->fixOtherFile( + __DIR__ . '/data/data-provider-static-fix-parent.php', + __DIR__ . '/data/data-provider-static-fix-parent-base.php', + __DIR__ . '/data/data-provider-static-fix-parent-base.php.fixed', + ); + } + /** * @return string[] */ diff --git a/tests/Rules/PHPUnit/data/data-provider-static-fix-parent-base.php b/tests/Rules/PHPUnit/data/data-provider-static-fix-parent-base.php new file mode 100644 index 0000000..eb30877 --- /dev/null +++ b/tests/Rules/PHPUnit/data/data-provider-static-fix-parent-base.php @@ -0,0 +1,13 @@ +