44
55use PhpParser \Node ;
66use PHPStan \Analyser \Scope ;
7+ use PHPStan \BetterReflection \Reflection \ReflectionMethod ;
78use PHPStan \Node \Expr \TypeExpr ;
89use PHPStan \Rules \Rule ;
910use PHPStan \Type \ObjectType ;
@@ -63,19 +64,19 @@ public function processNode(Node $node, Scope $scope): array
6364 return [];
6465 }
6566
66- $ testsWithProvider = [];
67+ $ testWithProviderMethods = [];
6768 $ method = $ scope ->getFunction ();
6869 $ testMethods = $ this ->testMethodsHelper ->getTestMethods ($ classReflection , $ scope );
6970 foreach ($ testMethods as $ testMethod ) {
7071 foreach ($ this ->dataProviderHelper ->getDataProviderMethods ($ scope , $ testMethod , $ classReflection ) as [, $ providerMethodName ]) {
7172 if ($ providerMethodName === $ method ->getName ()) {
72- $ testsWithProvider [] = $ testMethod ;
73+ $ testWithProviderMethods [] = $ testMethod ;
7374 continue 2 ;
7475 }
7576 }
7677 }
7778
78- if (count ($ testsWithProvider ) === 0 ) {
79+ if (count ($ testWithProviderMethods ) === 0 ) {
7980 return [];
8081 }
8182
@@ -84,25 +85,10 @@ public function processNode(Node $node, Scope $scope): array
8485 return [];
8586 }
8687
87- $ maxNumberOfParameters = null ;
88- foreach ($ testsWithProvider as $ testMethod ) {
89- $ num = $ testMethod ->getNumberOfParameters ();
90- if ($ testMethod ->isVariadic ()) {
91- $ num = PHP_INT_MAX ;
92- }
93- if ($ maxNumberOfParameters === null ) {
94- $ maxNumberOfParameters = $ num ;
95- continue ;
96- }
88+ $ maxNumberOfParameters = $ this ->getMaxNumberOfParameters ($ testWithProviderMethods );
9789
98- $ maxNumberOfParameters = max ($ maxNumberOfParameters , $ num );
99- if ($ num === PHP_INT_MAX ) {
100- break ;
101- }
102- }
103-
104- foreach ($ testsWithProvider as $ testMethod ) {
105- $ numberOfParameters = $ testMethod ->getNumberOfParameters ();
90+ foreach ($ testWithProviderMethods as $ testWithProviderMethod ) {
91+ $ numberOfParameters = $ testWithProviderMethod ->getNumberOfParameters ();
10692
10793 foreach ($ arraysTypes as [$ startLine , $ arraysType ]) {
10894 $ args = $ this ->arrayItemsToArgs ($ arraysType , $ numberOfParameters );
@@ -111,15 +97,15 @@ public function processNode(Node $node, Scope $scope): array
11197 }
11298
11399 if (
114- !$ testMethod ->isVariadic ()
100+ !$ testWithProviderMethod ->isVariadic ()
115101 && $ numberOfParameters !== $ maxNumberOfParameters
116102 ) {
117103 $ args = array_slice ($ args , 0 , $ numberOfParameters );
118104 }
119105
120106 $ scope ->invokeNodeCallback (new Node \Expr \MethodCall (
121107 new TypeExpr (new ObjectType ($ classReflection ->getName ())),
122- $ testMethod ->getName (),
108+ $ testWithProviderMethod ->getName (),
123109 $ args ,
124110 ['startLine ' => $ startLine ],
125111 ));
@@ -129,6 +115,28 @@ public function processNode(Node $node, Scope $scope): array
129115 return [];
130116 }
131117
118+ /**
119+ * @param non-empty-array<ReflectionMethod> $testMethods
120+ */
121+ private function getMaxNumberOfParameters (array $ testMethods ): int
122+ {
123+ $ maxNumberOfParameters = null ;
124+ foreach ($ testMethods as $ testMethod ) {
125+ $ num = $ testMethod ->getNumberOfParameters ();
126+ if ($ testMethod ->isVariadic ()) {
127+ return PHP_INT_MAX ;
128+ }
129+ if ($ maxNumberOfParameters === null ) {
130+ $ maxNumberOfParameters = $ num ;
131+ continue ;
132+ }
133+
134+ $ maxNumberOfParameters = max ($ maxNumberOfParameters , $ num );
135+ }
136+
137+ return $ maxNumberOfParameters ;
138+ }
139+
132140 /**
133141 * @return array<Node\Arg>
134142 */
0 commit comments