From 8a46ab1b2953c86584577e284e38bdd0c07cc04c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 27 Nov 2026 15:19:40 +0100 Subject: [PATCH] Make `phpunit.dataProviderPublic` auto-fixable --- src/Rules/PHPUnit/DataProviderHelper.php | 63 ++++++++++++------- .../DataProviderDeclarationRuleTest.php | 7 +++ .../PHPUnit/data/data-provider-public-fix.php | 33 ++++++++++ .../data/data-provider-public-fix.php.fixed | 33 ++++++++++ 4 files changed, 113 insertions(+), 23 deletions(-) create mode 100644 tests/Rules/PHPUnit/data/data-provider-public-fix.php create mode 100644 tests/Rules/PHPUnit/data/data-provider-public-fix.php.fixed diff --git a/src/Rules/PHPUnit/DataProviderHelper.php b/src/Rules/PHPUnit/DataProviderHelper.php index 01e7cf8..a2d90bf 100644 --- a/src/Rules/PHPUnit/DataProviderHelper.php +++ b/src/Rules/PHPUnit/DataProviderHelper.php @@ -18,6 +18,7 @@ use PHPStan\PhpDoc\ResolvedPhpDocBlock; use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; use PHPStan\Reflection\ClassReflection; +use PHPStan\Reflection\ExtendedMethodReflection; use PHPStan\Reflection\MissingMethodFromReflectionException; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\IdentifierRuleError; @@ -149,13 +150,24 @@ public function processDataProvider( } if (!$dataProviderMethodReflection->isPublic()) { - $errors[] = RuleErrorBuilder::message(sprintf( + $errorBuilder = RuleErrorBuilder::message(sprintf( '@dataProvider %s related method must be public.', $dataProviderValue, )) ->line($lineNumber) - ->identifier('phpunit.dataProviderPublic') - ->build(); + ->identifier('phpunit.dataProviderPublic'); + + $methodNodeAndFile = $this->findMethodNode($dataProviderMethodReflection); + if ($methodNodeAndFile !== null) { + [$methodNode, $fileName] = $methodNodeAndFile; + $errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) { + $methodNode->flags &= ~Modifiers::VISIBILITY_MASK; + $methodNode->flags |= Modifiers::PUBLIC; + + return $methodNode; + }, $fileName); + } + $errors[] = $errorBuilder->build(); } if ( @@ -170,23 +182,14 @@ public function processDataProvider( ->line($lineNumber) ->identifier('phpunit.dataProviderStatic'); - // a method from a trait is declared in the trait, which can be in another file - $nativeMethod = $dataProviderMethodReflection->getDeclaringClass()->getNativeReflection()->getMethod($dataProviderMethodReflection->getName())->getBetterReflection(); - $declaringClassFileName = $nativeMethod->getFileName(); - if ($declaringClassFileName !== null) { - $stmts = $this->parser->parseFile($declaringClassFileName); - $methodNode = $this->findMethodNode( - $stmts, - $nativeMethod->getDeclaringClass()->getName(), - $dataProviderMethodReflection->getName(), - ); - if ($methodNode !== null) { - $errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) { - $methodNode->flags |= Modifiers::STATIC; - - return $methodNode; - }, $declaringClassFileName); - } + $methodNodeAndFile = $this->findMethodNode($dataProviderMethodReflection); + if ($methodNodeAndFile !== null) { + [$methodNode, $fileName] = $methodNodeAndFile; + $errorBuilder->fixNode($methodNode, static function (ClassMethod $methodNode) { + $methodNode->flags |= Modifiers::STATIC; + + return $methodNode; + }, $fileName); } $errors[] = $errorBuilder->build(); } @@ -195,10 +198,19 @@ public function processDataProvider( } /** - * @param Node[] $stmts + * @return array{ClassMethod, string}|null */ - private function findMethodNode(array $stmts, string $className, string $methodName): ?ClassMethod + private function findMethodNode(ExtendedMethodReflection $methodReflection): ?array { + // a method from a trait is declared in the trait, which can be in another file + $nativeMethod = $methodReflection->getDeclaringClass()->getNativeReflection()->getMethod($methodReflection->getName())->getBetterReflection(); + $fileName = $nativeMethod->getFileName(); + if ($fileName === null) { + return null; + } + + $stmts = $this->parser->parseFile($fileName); + $className = $nativeMethod->getDeclaringClass()->getName(); $nodeFinder = new NodeFinder(); /** @var ClassLike|null $classNode */ @@ -212,7 +224,12 @@ private function findMethodNode(array $stmts, string $className, string $methodN return null; } - return $classNode->getMethod($methodName); + $methodNode = $classNode->getMethod($methodReflection->getName()); + if ($methodNode === null) { + return null; + } + + return [$methodNode, $fileName]; } private function getDataProviderAnnotationValue(PhpDocTagNode $phpDocTag): ?string diff --git a/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php b/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php index 898ca0c..330e5e9 100644 --- a/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php +++ b/tests/Rules/PHPUnit/DataProviderDeclarationRuleTest.php @@ -161,6 +161,13 @@ public function testFixDataProviderStaticDeclaredInTraitInOtherFile(): void ); } + public function testFixDataProviderPublic(): void + { + $this->phpunitVersion = 10; + + $this->fix(__DIR__ . '/data/data-provider-public-fix.php', __DIR__ . '/data/data-provider-public-fix.php.fixed'); + } + /** * @return string[] */ diff --git a/tests/Rules/PHPUnit/data/data-provider-public-fix.php b/tests/Rules/PHPUnit/data/data-provider-public-fix.php new file mode 100644 index 0000000..2719a71 --- /dev/null +++ b/tests/Rules/PHPUnit/data/data-provider-public-fix.php @@ -0,0 +1,33 @@ +