|
8 | 8 | use PHPStan\Rules\Rule; |
9 | 9 | use PHPStan\ShouldNotHappenException; |
10 | 10 | use PHPUnit\Framework\TestCase; |
| 11 | +use function array_slice; |
11 | 12 | use function count; |
| 13 | +use function max; |
| 14 | +use function min; |
12 | 15 |
|
13 | 16 | /** |
14 | 17 | * @implements Rule<Node> |
@@ -95,23 +98,35 @@ public function processNode(Node $node, Scope $scope): array |
95 | 98 | return []; |
96 | 99 | } |
97 | 100 |
|
98 | | - foreach ($arrayExprs as $arrayExpr) { |
99 | | - if (!$arrayExpr instanceof Node\Expr\Array_) { |
100 | | - throw new ShouldNotHappenException(); |
101 | | - } |
| 101 | + $maxNumberOfParameters = 0; |
| 102 | + $trimArgs = count($testsWithProvider) > 1; |
| 103 | + foreach ($testsWithProvider as $testMethod) { |
| 104 | + $maxNumberOfParameters = max($maxNumberOfParameters, $testMethod->getNumberOfParameters()); |
| 105 | + } |
102 | 106 |
|
103 | | - $args = $this->arrayItemsToArgs($arrayExpr); |
104 | | - if ($args === null) { |
105 | | - continue; |
106 | | - } |
| 107 | + foreach ($testsWithProvider as $testMethod) { |
| 108 | + foreach ($arrayExprs as $arrayExpr) { |
| 109 | + if (!$arrayExpr instanceof Node\Expr\Array_) { |
| 110 | + throw new ShouldNotHappenException(); |
| 111 | + } |
107 | 112 |
|
108 | | - $var = new Node\Expr\New_(new Node\Name($classReflection->getName())); |
109 | | - $scope->invokeNodeCallback(new Node\Expr\MethodCall( |
110 | | - $var, |
111 | | - $testsWithProvider[0]->getName(), |
112 | | - $args, |
113 | | - ['startLine' => $arrayExpr->getStartLine()], |
114 | | - )); |
| 113 | + $args = $this->arrayItemsToArgs($arrayExpr); |
| 114 | + if ($args === null) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + if ($trimArgs && $maxNumberOfParameters !== $testMethod->getNumberOfParameters()) { |
| 119 | + $args = array_slice($args, 0, min($testMethod->getNumberOfParameters(), $maxNumberOfParameters)); |
| 120 | + } |
| 121 | + |
| 122 | + $var = new Node\Expr\New_(new Node\Name($classReflection->getName())); |
| 123 | + $scope->invokeNodeCallback(new Node\Expr\MethodCall( |
| 124 | + $var, |
| 125 | + $testMethod->getName(), |
| 126 | + $args, |
| 127 | + ['startLine' => $arrayExpr->getStartLine()], |
| 128 | + )); |
| 129 | + } |
115 | 130 | } |
116 | 131 |
|
117 | 132 | return []; |
|
0 commit comments