Skip to content

Commit b3cfcb3

Browse files
committed
infer constant types first
1 parent fd5b5c9 commit b3cfcb3

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ public function getTypeFromFunctionCall(
4848
return null;
4949
}
5050

51-
$formatType = $scope->getType($args[0]->value);
52-
if (count($args) === 1) {
53-
return $this->getConstantType($args, null, $functionReflection, $scope);
51+
$constantType = $this->getConstantType($args, $functionReflection, $scope);
52+
if ($constantType !== null) {
53+
return $constantType;
5454
}
5555

56+
$formatType = $scope->getType($args[0]->value);
5657
$formatStrings = $formatType->getConstantStrings();
5758
if (count($formatStrings) === 0) {
5859
return null;
@@ -118,19 +119,19 @@ public function getTypeFromFunctionCall(
118119
$returnType = new StringType();
119120
}
120121

121-
return $this->getConstantType($args, $returnType, $functionReflection, $scope);
122+
return $returnType;
122123
}
123124

124125
/**
125126
* @param Arg[] $args
126127
*/
127-
private function getConstantType(array $args, ?Type $fallbackReturnType, FunctionReflection $functionReflection, Scope $scope): ?Type
128+
private function getConstantType(array $args, FunctionReflection $functionReflection, Scope $scope): ?Type
128129
{
129130
$values = [];
130131
$combinationsCount = 1;
131132
foreach ($args as $arg) {
132133
if ($arg->unpack) {
133-
return $fallbackReturnType;
134+
return null;
134135
}
135136

136137
$argType = $scope->getType($arg->value);
@@ -145,23 +146,23 @@ private function getConstantType(array $args, ?Type $fallbackReturnType, Functio
145146
}
146147

147148
if (count($constantScalarValues) === 0) {
148-
return $fallbackReturnType;
149+
return null;
149150
}
150151

151152
$values[] = $constantScalarValues;
152153
$combinationsCount *= count($constantScalarValues);
153154
}
154155

155156
if ($combinationsCount > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
156-
return $fallbackReturnType;
157+
return null;
157158
}
158159

159160
$combinations = CombinationsHelper::combinations($values);
160161
$returnTypes = [];
161162
foreach ($combinations as $combination) {
162163
$format = array_shift($combination);
163164
if (!is_string($format)) {
164-
return $fallbackReturnType;
165+
return null;
165166
}
166167

167168
try {
@@ -171,12 +172,12 @@ private function getConstantType(array $args, ?Type $fallbackReturnType, Functio
171172
$returnTypes[] = $scope->getTypeFromValue(@vsprintf($format, $combination));
172173
}
173174
} catch (Throwable) {
174-
return $fallbackReturnType;
175+
return null;
175176
}
176177
}
177178

178179
if (count($returnTypes) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
179-
return $fallbackReturnType;
180+
return null;
180181
}
181182

182183
return TypeCombinator::union(...$returnTypes);

0 commit comments

Comments
 (0)