Skip to content

Commit 466e677

Browse files
authored
Refactor SprintfFunctionDynamicReturnTypeExtension
1 parent 36e60f4 commit 466e677

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Type\Php;
44

5+
use PhpParser\Node\Arg;
56
use PhpParser\Node\Expr\FuncCall;
67
use PHPStan\Analyser\Scope;
78
use PHPStan\Internal\CombinationsHelper;
@@ -108,6 +109,14 @@ public function getTypeFromFunctionCall(
108109
$returnType = new StringType();
109110
}
110111

112+
return $this->getConstantType($args, $returnType, $functionReflection, $scope);
113+
}
114+
115+
/**
116+
* @param Arg[] $args
117+
*/
118+
private function getConstantType(array $args, Type $fallbackReturnType, FunctionReflection $functionReflection, Scope $scope): Type
119+
{
111120
$values = [];
112121
$combinationsCount = 1;
113122
foreach ($args as $arg) {
@@ -123,23 +132,23 @@ public function getTypeFromFunctionCall(
123132
}
124133

125134
if (count($constantScalarValues) === 0) {
126-
return $returnType;
135+
return $fallbackReturnType;
127136
}
128137

129138
$values[] = $constantScalarValues;
130139
$combinationsCount *= count($constantScalarValues);
131140
}
132141

133142
if ($combinationsCount > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
134-
return $returnType;
143+
return $fallbackReturnType;
135144
}
136145

137146
$combinations = CombinationsHelper::combinations($values);
138147
$returnTypes = [];
139148
foreach ($combinations as $combination) {
140149
$format = array_shift($combination);
141150
if (!is_string($format)) {
142-
return $returnType;
151+
return $fallbackReturnType;
143152
}
144153

145154
try {
@@ -149,12 +158,12 @@ public function getTypeFromFunctionCall(
149158
$returnTypes[] = $scope->getTypeFromValue(@vsprintf($format, $combination));
150159
}
151160
} catch (Throwable) {
152-
return $returnType;
161+
return $fallbackReturnType;
153162
}
154163
}
155164

156165
if (count($returnTypes) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
157-
return $returnType;
166+
return $fallbackReturnType;
158167
}
159168

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

0 commit comments

Comments
 (0)