1818use PHPStan \PhpDoc \ResolvedPhpDocBlock ;
1919use PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
2020use PHPStan \Reflection \ClassReflection ;
21+ use PHPStan \Reflection \ExtendedMethodReflection ;
2122use PHPStan \Reflection \MissingMethodFromReflectionException ;
2223use PHPStan \Reflection \ReflectionProvider ;
2324use PHPStan \Rules \IdentifierRuleError ;
@@ -149,13 +150,24 @@ public function processDataProvider(
149150 }
150151
151152 if (!$ dataProviderMethodReflection ->isPublic ()) {
152- $ errors [] = RuleErrorBuilder ::message (sprintf (
153+ $ errorBuilder = RuleErrorBuilder ::message (sprintf (
153154 '@dataProvider %s related method must be public. ' ,
154155 $ dataProviderValue ,
155156 ))
156157 ->line ($ lineNumber )
157- ->identifier ('phpunit.dataProviderPublic ' )
158- ->build ();
158+ ->identifier ('phpunit.dataProviderPublic ' );
159+
160+ $ methodNodeAndFile = $ this ->findMethodNode ($ dataProviderMethodReflection );
161+ if ($ methodNodeAndFile !== null ) {
162+ [$ methodNode , $ fileName ] = $ methodNodeAndFile ;
163+ $ errorBuilder ->fixNode ($ methodNode , static function (ClassMethod $ methodNode ) {
164+ $ methodNode ->flags &= ~Modifiers::VISIBILITY_MASK ;
165+ $ methodNode ->flags |= Modifiers::PUBLIC ;
166+
167+ return $ methodNode ;
168+ }, $ fileName );
169+ }
170+ $ errors [] = $ errorBuilder ->build ();
159171 }
160172
161173 if (
@@ -170,23 +182,14 @@ public function processDataProvider(
170182 ->line ($ lineNumber )
171183 ->identifier ('phpunit.dataProviderStatic ' );
172184
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- );
183- if ($ methodNode !== null ) {
184- $ errorBuilder ->fixNode ($ methodNode , static function (ClassMethod $ methodNode ) {
185- $ methodNode ->flags |= Modifiers::STATIC ;
186-
187- return $ methodNode ;
188- }, $ declaringClassFileName );
189- }
185+ $ methodNodeAndFile = $ this ->findMethodNode ($ dataProviderMethodReflection );
186+ if ($ methodNodeAndFile !== null ) {
187+ [$ methodNode , $ fileName ] = $ methodNodeAndFile ;
188+ $ errorBuilder ->fixNode ($ methodNode , static function (ClassMethod $ methodNode ) {
189+ $ methodNode ->flags |= Modifiers::STATIC ;
190+
191+ return $ methodNode ;
192+ }, $ fileName );
190193 }
191194 $ errors [] = $ errorBuilder ->build ();
192195 }
@@ -195,10 +198,19 @@ public function processDataProvider(
195198 }
196199
197200 /**
198- * @param Node[] $stmts
201+ * @return array{ClassMethod, string}|null
199202 */
200- private function findMethodNode (array $ stmts , string $ className , string $ methodName ): ?ClassMethod
203+ private function findMethodNode (ExtendedMethodReflection $ methodReflection ): ?array
201204 {
205+ // a method from a trait is declared in the trait, which can be in another file
206+ $ nativeMethod = $ methodReflection ->getDeclaringClass ()->getNativeReflection ()->getMethod ($ methodReflection ->getName ())->getBetterReflection ();
207+ $ fileName = $ nativeMethod ->getFileName ();
208+ if ($ fileName === null ) {
209+ return null ;
210+ }
211+
212+ $ stmts = $ this ->parser ->parseFile ($ fileName );
213+ $ className = $ nativeMethod ->getDeclaringClass ()->getName ();
202214 $ nodeFinder = new NodeFinder ();
203215
204216 /** @var ClassLike|null $classNode */
@@ -212,7 +224,12 @@ private function findMethodNode(array $stmts, string $className, string $methodN
212224 return null ;
213225 }
214226
215- return $ classNode ->getMethod ($ methodName );
227+ $ methodNode = $ classNode ->getMethod ($ methodReflection ->getName ());
228+ if ($ methodNode === null ) {
229+ return null ;
230+ }
231+
232+ return [$ methodNode , $ fileName ];
216233 }
217234
218235 private function getDataProviderAnnotationValue (PhpDocTagNode $ phpDocTag ): ?string
0 commit comments