|
5 | 5 | use PhpParser\Node\Expr\FuncCall;
|
6 | 6 | use PHPStan\Analyser\Scope;
|
7 | 7 | use PHPStan\Reflection\FunctionReflection;
|
| 8 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
8 | 9 | use PHPStan\Reflection\ParametersAcceptorSelector;
|
9 | 10 | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
|
10 | 11 | use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
|
11 | 12 | use PHPStan\Type\Accessory\AccessoryNumericStringType;
|
12 |
| -use PHPStan\Type\ConstantScalarType; |
13 | 13 | use PHPStan\Type\DynamicFunctionReturnTypeExtension;
|
14 | 14 | use PHPStan\Type\IntersectionType;
|
15 | 15 | use PHPStan\Type\StringType;
|
16 | 16 | use PHPStan\Type\Type;
|
| 17 | +use PHPStan\Type\TypeCombinator; |
17 | 18 | use Throwable;
|
18 | 19 | use function array_key_exists;
|
19 | 20 | use function array_shift;
|
@@ -86,29 +87,58 @@ public function getTypeFromFunctionCall(
|
86 | 87 | $values = [];
|
87 | 88 | foreach ($args as $arg) {
|
88 | 89 | $argType = $scope->getType($arg->value);
|
89 |
| - if (!$argType instanceof ConstantScalarType) { |
| 90 | + if (count($argType->getConstantScalarValues()) === 0) { |
90 | 91 | return $returnType;
|
91 | 92 | }
|
92 | 93 |
|
93 |
| - $values[] = $argType->getValue(); |
| 94 | + $values[] = $argType->getConstantScalarValues(); |
94 | 95 | }
|
95 | 96 |
|
96 |
| - $format = array_shift($values); |
97 |
| - if (!is_string($format)) { |
98 |
| - return $returnType; |
99 |
| - } |
| 97 | + $combinations = $this->combinations($values); |
| 98 | + $returnTypes = []; |
| 99 | + foreach ($combinations as $combination) { |
| 100 | + $format = array_shift($combination); |
| 101 | + if (!is_string($format)) { |
| 102 | + return $returnType; |
| 103 | + } |
100 | 104 |
|
101 |
| - try { |
102 |
| - if ($functionReflection->getName() === 'sprintf') { |
103 |
| - $value = @sprintf($format, ...$values); |
104 |
| - } else { |
105 |
| - $value = @vsprintf($format, $values); |
| 105 | + try { |
| 106 | + if ($functionReflection->getName() === 'sprintf') { |
| 107 | + $returnTypes[] = $scope->getTypeFromValue(@sprintf($format, ...$combination)); |
| 108 | + } else { |
| 109 | + $returnTypes[] = $scope->getTypeFromValue(@vsprintf($format, $combination)); |
| 110 | + } |
| 111 | + } catch (Throwable) { |
| 112 | + return $returnType; |
106 | 113 | }
|
107 |
| - } catch (Throwable) { |
| 114 | + } |
| 115 | + |
| 116 | + if (count($returnTypes) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { |
108 | 117 | return $returnType;
|
109 | 118 | }
|
110 | 119 |
|
111 |
| - return $scope->getTypeFromValue($value); |
| 120 | + return TypeCombinator::union(...$returnTypes); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param array<mixed> $arrays |
| 125 | + * @return iterable<mixed> |
| 126 | + */ |
| 127 | + private function combinations(array $arrays): iterable |
| 128 | + { |
| 129 | + // from https://stackoverflow.com/a/70800936/565782 by Arnaud Le Blanc |
| 130 | + if ($arrays === []) { |
| 131 | + yield []; |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + $head = array_shift($arrays); |
| 136 | + |
| 137 | + foreach ($head as $elem) { |
| 138 | + foreach ($this->combinations($arrays) as $combination) { |
| 139 | + yield [$elem, ...$combination]; |
| 140 | + } |
| 141 | + } |
112 | 142 | }
|
113 | 143 |
|
114 | 144 | }
|
0 commit comments