1616use PHPStan \PhpDoc \ResolvedPhpDocBlock ;
1717use PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
1818use PHPStan \Reflection \ClassReflection ;
19+ use PHPStan \Reflection \ExtendedMethodReflection ;
1920use PHPStan \Reflection \MissingMethodFromReflectionException ;
2021use PHPStan \Reflection \ReflectionProvider ;
2122use PHPStan \Rules \IdentifierRuleError ;
@@ -147,13 +148,23 @@ public function processDataProvider(
147148 }
148149
149150 if (!$ dataProviderMethodReflection ->isPublic ()) {
150- $ errors [] = RuleErrorBuilder ::message (sprintf (
151+ $ errorBuilder = RuleErrorBuilder ::message (sprintf (
151152 '@dataProvider %s related method must be public. ' ,
152153 $ dataProviderValue ,
153154 ))
154155 ->line ($ lineNumber )
155- ->identifier ('phpunit.dataProviderPublic ' )
156- ->build ();
156+ ->identifier ('phpunit.dataProviderPublic ' );
157+
158+ $ methodNode = $ this ->findMethodNode ($ dataProviderMethodReflection );
159+ if ($ methodNode !== null ) {
160+ $ errorBuilder ->fixNode ($ methodNode , static function (ClassMethod $ methodNode ) {
161+ $ methodNode ->flags &= ~Modifiers::VISIBILITY_MASK ;
162+ $ methodNode ->flags |= Modifiers::PUBLIC ;
163+
164+ return $ methodNode ;
165+ });
166+ }
167+ $ errors [] = $ errorBuilder ->build ();
157168 }
158169
159170 if (
@@ -168,26 +179,37 @@ public function processDataProvider(
168179 ->line ($ lineNumber )
169180 ->identifier ('phpunit.dataProviderStatic ' );
170181
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 ());
177- if ($ methodNode !== null ) {
178- $ errorBuilder ->fixNode ($ methodNode , static function (ClassMethod $ methodNode ) {
179- $ methodNode ->flags |= Modifiers::STATIC ;
180-
181- return $ methodNode ;
182- });
183- }
182+ $ methodNode = $ this ->findMethodNode ($ dataProviderMethodReflection );
183+ if ($ methodNode !== null ) {
184+ $ errorBuilder ->fixNode ($ methodNode , static function (ClassMethod $ methodNode ) {
185+ $ methodNode ->flags |= Modifiers::STATIC ;
186+
187+ return $ methodNode ;
188+ });
184189 }
185190 $ errors [] = $ errorBuilder ->build ();
186191 }
187192
188193 return $ errors ;
189194 }
190195
196+ private function findMethodNode (ExtendedMethodReflection $ methodReflection ): ?ClassMethod
197+ {
198+ $ fileName = $ methodReflection ->getDeclaringClass ()->getFileName ();
199+ if ($ fileName === null ) {
200+ return null ;
201+ }
202+
203+ $ stmts = $ this ->parser ->parseFile ($ fileName );
204+ $ nodeFinder = new NodeFinder ();
205+ $ methodName = $ methodReflection ->getName ();
206+
207+ /** @var ClassMethod|null $methodNode */
208+ $ methodNode = $ nodeFinder ->findFirst ($ stmts , static fn ($ node ) => $ node instanceof ClassMethod && $ node ->name ->toString () === $ methodName );
209+
210+ return $ methodNode ;
211+ }
212+
191213 private function getDataProviderAnnotationValue (PhpDocTagNode $ phpDocTag ): ?string
192214 {
193215 if (preg_match ('/^[^ \t]+/ ' , (string ) $ phpDocTag ->value , $ matches ) !== 1 ) {
0 commit comments