Skip to content

Commit b5a9373

Browse files
Use type string resolver
1 parent 5bf02f7 commit b5a9373

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

src/Type/Symfony/ParameterDynamicReturnTypeExtension.php

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node\Expr\MethodCall;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\PhpDoc\TypeStringResolver;
78
use PHPStan\Reflection\MethodReflection;
89
use PHPStan\Reflection\ParametersAcceptorSelector;
910
use PHPStan\ShouldNotHappenException;
@@ -25,6 +26,7 @@
2526
use PHPStan\Type\TypeCombinator;
2627
use PHPStan\Type\TypeTraverser;
2728
use PHPStan\Type\UnionType;
29+
use Symfony\Component\DependencyInjection\EnvVarProcessor;
2830
use function in_array;
2931

3032
final class ParameterDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -45,13 +47,24 @@ final class ParameterDynamicReturnTypeExtension implements DynamicMethodReturnTy
4547
/** @var \PHPStan\Symfony\ParameterMap */
4648
private $parameterMap;
4749

48-
public function __construct(string $className, ?string $methodGet, ?string $methodHas, Configuration $configuration, ParameterMap $symfonyParameterMap)
50+
/** @var \PHPStan\PhpDoc\TypeStringResolver */
51+
private $typeStringResolver;
52+
53+
public function __construct(
54+
string $className,
55+
?string $methodGet,
56+
?string $methodHas,
57+
Configuration $configuration,
58+
ParameterMap $symfonyParameterMap,
59+
TypeStringResolver $typeStringResolver
60+
)
4961
{
5062
$this->className = $className;
5163
$this->methodGet = $methodGet;
5264
$this->methodHas = $methodHas;
5365
$this->constantHassers = $configuration->hasConstantHassers();
5466
$this->parameterMap = $symfonyParameterMap;
67+
$this->typeStringResolver = $typeStringResolver;
5568
}
5669

5770
public function getClass(): string
@@ -134,33 +147,9 @@ private function generalizeTypeFromValue(Scope $scope, $value): Type
134147
&& preg_match('/%env\((.*)\:.*\)%/U', $value, $matches) === 1
135148
&& strlen($matches[0]) === strlen($value)
136149
) {
137-
switch ($matches[1]) {
138-
case 'base64':
139-
case 'file':
140-
case 'resolve':
141-
case 'string':
142-
case 'trim':
143-
return new StringType();
144-
case 'bool':
145-
return new BooleanType();
146-
case 'int':
147-
return new IntegerType();
148-
case 'float':
149-
return new FloatType();
150-
case 'csv':
151-
case 'json':
152-
case 'url':
153-
case 'query_string':
154-
return new ArrayType(new MixedType(), new MixedType());
155-
default:
156-
return new UnionType([
157-
new ArrayType(new MixedType(), new MixedType()),
158-
new BooleanType(),
159-
new FloatType(),
160-
new IntegerType(),
161-
new StringType(),
162-
]);
163-
}
150+
$providedTypes = EnvVarProcessor::getProvidedTypes();
151+
152+
return $this->typeStringResolver->resolve($providedTypes[$matches[1]] ?? 'bool|int|float|string|array');
164153
}
165154

166155
return $this->generalizeType($scope->getTypeFromValue($value));

0 commit comments

Comments
 (0)