|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Php; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\FuncCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\FunctionReflection; |
| 8 | +use PHPStan\Reflection\ParameterReflection; |
| 9 | +use PHPStan\Type\Accessory\AccessoryLowercaseStringType; |
| 10 | +use PHPStan\Type\Accessory\AccessoryUppercaseStringType; |
| 11 | +use PHPStan\Type\ArrayType; |
| 12 | +use PHPStan\Type\FunctionParameterOutTypeExtension; |
| 13 | +use PHPStan\Type\IntegerType; |
| 14 | +use PHPStan\Type\IntersectionType; |
| 15 | +use PHPStan\Type\MixedType; |
| 16 | +use PHPStan\Type\StringType; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use PHPStan\Type\UnionType; |
| 19 | + |
| 20 | +use function in_array; |
| 21 | +use function strtolower; |
| 22 | + |
| 23 | +final class ParseStrParameterOutTypeExtension implements FunctionParameterOutTypeExtension |
| 24 | +{ |
| 25 | + |
| 26 | + public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool |
| 27 | + { |
| 28 | + return in_array(strtolower($functionReflection->getName()), ['parse_str', 'mb_parse_str'], true) |
| 29 | + && $parameter->getName() === 'result'; |
| 30 | + } |
| 31 | + |
| 32 | + public function getParameterOutTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, ParameterReflection $parameter, Scope $scope): ?Type |
| 33 | + { |
| 34 | + $args = $funcCall->getArgs(); |
| 35 | + if (count($args) < 1) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + $stringType = $scope->getType($args[0]->value); |
| 40 | + $accessory = []; |
| 41 | + if ($stringType->isLowercaseString()->yes()) { |
| 42 | + $accessory[] = new AccessoryLowercaseStringType(); |
| 43 | + } |
| 44 | + if ($stringType->isUppercaseString()->yes()) { |
| 45 | + $accessory[] = new AccessoryUppercaseStringType(); |
| 46 | + } |
| 47 | + if (count($accessory) > 0) { |
| 48 | + $valueType = new IntersectionType([new StringType(), ...$accessory]); |
| 49 | + } else { |
| 50 | + $valueType = new StringType(); |
| 51 | + } |
| 52 | + |
| 53 | + return new ArrayType( |
| 54 | + new UnionType([new StringType(), new IntegerType()]), |
| 55 | + new UnionType([new ArrayType(new MixedType(), new MixedType()), $valueType]), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments