33namespace PHPStan \Rules \PHPUnit ;
44
55use PhpParser \Node ;
6+ use PHPStan \Analyser \NodeCallbackInvoker ;
67use PHPStan \Analyser \Scope ;
78use PHPStan \BetterReflection \Reflection \ReflectionMethod ;
89use PHPStan \Node \Expr \TypeExpr ;
10+ use PHPStan \Reflection \ClassReflection ;
911use PHPStan \Rules \MultipleNodeTypesRule ;
1012use PHPStan \Type \ObjectType ;
1113use PHPStan \Type \Type ;
@@ -57,20 +59,33 @@ public function processNode(Node $node, Scope $scope): array
5759 return [];
5860 }
5961
60- if ($ scope ->getFunction () === null ) {
62+ $ classReflection = $ scope ->getClassReflection ();
63+ if ($ classReflection === null ) {
6164 return [];
6265 }
63- if ($ scope ->isInAnonymousFunction ()) {
66+
67+ $ method = $ scope ->getFunction ();
68+ if ($ method === null ) {
69+ $ testMethodWithProviderClosure = $ this ->findTestMethodWithDataProviderClosure ($ node , $ scope , $ classReflection );
70+ if ($ testMethodWithProviderClosure === null ) {
71+ return [];
72+ }
73+
74+ [$ testMethod , $ validateArgumentCount ] = $ testMethodWithProviderClosure ;
75+ $ this ->checkDataProviderData (
76+ $ node ,
77+ $ scope ,
78+ $ classReflection ,
79+ [[$ testMethod , !$ validateArgumentCount && !$ testMethod ->isVariadic ()]],
80+ );
81+
6482 return [];
6583 }
66-
67- $ classReflection = $ scope ->getClassReflection ();
68- if ($ classReflection === null ) {
84+ if ($ scope ->isInAnonymousFunction ()) {
6985 return [];
7086 }
7187
7288 $ testWithProviderMethods = [];
73- $ method = $ scope ->getFunction ();
7489 $ testMethods = $ this ->testMethodsHelper ->getTestMethods ($ classReflection , $ scope );
7590 foreach ($ testMethods as $ testMethod ) {
7691 foreach ($ this ->dataProviderHelper ->getDataProviderMethods ($ scope , $ testMethod , $ classReflection ) as [, $ providerMethodName ]) {
@@ -85,39 +100,100 @@ public function processNode(Node $node, Scope $scope): array
85100 return [];
86101 }
87102
103+ $ maxNumberOfParameters = $ this ->getMaxNumberOfParameters ($ testWithProviderMethods );
104+
105+ $ testMethodsToCheck = [];
106+ foreach ($ testWithProviderMethods as $ testWithProviderMethod ) {
107+ $ testMethodsToCheck [] = [
108+ $ testWithProviderMethod ,
109+ !$ testWithProviderMethod ->isVariadic ()
110+ && $ testWithProviderMethod ->getNumberOfParameters () !== $ maxNumberOfParameters ,
111+ ];
112+ }
113+
114+ $ this ->checkDataProviderData ($ node , $ scope , $ classReflection , $ testMethodsToCheck );
115+
116+ return [];
117+ }
118+
119+ /**
120+ * @param Node\Stmt\Return_|Node\Expr\Yield_|Node\Expr\YieldFrom $node
121+ * @param Scope&NodeCallbackInvoker $scope
122+ * @param list<array{ReflectionMethod, bool}> $testMethods test methods with a flag whether to trim arguments to the number of parameters
123+ */
124+ private function checkDataProviderData (Node $ node , Scope $ scope , ClassReflection $ classReflection , array $ testMethods ): void
125+ {
88126 $ arraysTypes = $ this ->buildArrayTypesFromNode ($ node , $ scope );
89127 if ($ arraysTypes === []) {
90- return [] ;
128+ return ;
91129 }
92130
93- $ maxNumberOfParameters = $ this ->getMaxNumberOfParameters ($ testWithProviderMethods );
94-
95- foreach ($ testWithProviderMethods as $ testWithProviderMethod ) {
96- $ numberOfParameters = $ testWithProviderMethod ->getNumberOfParameters ();
131+ foreach ($ testMethods as [$ testMethod , $ trimArgs ]) {
132+ $ numberOfParameters = $ testMethod ->getNumberOfParameters ();
97133
98134 foreach ($ arraysTypes as [$ startLine , $ arraysType ]) {
99135 $ args = $ this ->arrayItemsToArgs ($ arraysType , $ numberOfParameters );
100136 if ($ args === null ) {
101137 continue ;
102138 }
103139
104- if (
105- !$ testWithProviderMethod ->isVariadic ()
106- && $ numberOfParameters !== $ maxNumberOfParameters
107- ) {
140+ if ($ trimArgs ) {
108141 $ args = array_slice ($ args , 0 , $ numberOfParameters );
109142 }
110143
111144 $ scope ->invokeNodeCallback (new Node \Expr \MethodCall (
112145 new TypeExpr (new ObjectType ($ classReflection ->getName ())),
113- $ testWithProviderMethod ->getName (),
146+ $ testMethod ->getName (),
114147 $ args ,
115148 ['startLine ' => $ startLine ],
116149 ));
117150 }
118151 }
152+ }
119153
120- return [];
154+ /**
155+ * Finds the test method whose #[DataProviderClosure] attribute contains the closure
156+ * the given node is directly declared in.
157+ *
158+ * @return array{ReflectionMethod, bool}|null test method and whether the argument count should be validated
159+ */
160+ private function findTestMethodWithDataProviderClosure (Node $ node , Scope $ scope , ClassReflection $ classReflection ): ?array
161+ {
162+ if (!$ scope ->isInAnonymousFunction ()) {
163+ return null ;
164+ }
165+
166+ // test methods declared in traits are not considered by TestMethodsHelper.
167+ // line numbers of a trait would also be compared with closures of the using class file.
168+ if ($ scope ->isInTrait ()) {
169+ return null ;
170+ }
171+
172+ // skip closures nested within the data provider closure
173+ $ parentScope = $ scope ->getParentScope ();
174+ if ($ parentScope === null || $ parentScope ->isInAnonymousFunction ()) {
175+ return null ;
176+ }
177+
178+ $ nodeStartPos = $ node ->getStartFilePos ();
179+ $ testMethods = $ this ->testMethodsHelper ->getTestMethods ($ classReflection , $ scope );
180+ foreach ($ testMethods as $ testMethod ) {
181+ foreach ($ this ->dataProviderHelper ->getDataProviderClosures ($ testMethod ) as [$ closure , $ validateArgumentCount ]) {
182+ if (
183+ $ nodeStartPos < $ closure ->getStartFilePos ()
184+ || $ nodeStartPos > $ closure ->getEndFilePos ()
185+ ) {
186+ continue ;
187+ }
188+
189+ return [
190+ $ testMethod ,
191+ $ validateArgumentCount === null || !$ scope ->getType ($ validateArgumentCount )->isFalse ()->yes (),
192+ ];
193+ }
194+ }
195+
196+ return null ;
121197 }
122198
123199 /**
0 commit comments