Skip to content

Commit 7c7ed4f

Browse files
Use type string resolver
1 parent a27f643 commit 7c7ed4f

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

src/Type/Symfony/ParameterDynamicReturnTypeExtension.php

+17-28
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;
@@ -24,6 +25,7 @@
2425
use PHPStan\Type\TypeCombinator;
2526
use PHPStan\Type\TypeTraverser;
2627
use PHPStan\Type\UnionType;
28+
use Symfony\Component\DependencyInjection\EnvVarProcessor;
2729
use function in_array;
2830

2931
final class ParameterDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -44,13 +46,24 @@ final class ParameterDynamicReturnTypeExtension implements DynamicMethodReturnTy
4446
/** @var \PHPStan\Symfony\ParameterMap */
4547
private $parameterMap;
4648

47-
public function __construct(string $className, ?string $methodGet, ?string $methodHas, bool $constantHassers, ParameterMap $symfonyParameterMap)
49+
/** @var \PHPStan\PhpDoc\TypeStringResolver */
50+
private $typeStringResolver;
51+
52+
public function __construct(
53+
string $className,
54+
?string $methodGet,
55+
?string $methodHas,
56+
bool $constantHassers,
57+
ParameterMap $symfonyParameterMap,
58+
TypeStringResolver $typeStringResolver
59+
)
4860
{
4961
$this->className = $className;
5062
$this->methodGet = $methodGet;
5163
$this->methodHas = $methodHas;
5264
$this->constantHassers = $constantHassers;
5365
$this->parameterMap = $symfonyParameterMap;
66+
$this->typeStringResolver = $typeStringResolver;
5467
}
5568

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

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

0 commit comments

Comments
 (0)