/
ParseUrlFunctionDynamicReturnTypeExtension.php
155 lines (125 loc) · 4.35 KB
/
ParseUrlFunctionDynamicReturnTypeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use ValueError;
use function count;
use function parse_url;
use const PHP_URL_FRAGMENT;
use const PHP_URL_HOST;
use const PHP_URL_PASS;
use const PHP_URL_PATH;
use const PHP_URL_PORT;
use const PHP_URL_QUERY;
use const PHP_URL_SCHEME;
use const PHP_URL_USER;
final class ParseUrlFunctionDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
/** @var array<int,Type>|null */
private ?array $componentTypesPairedConstants = null;
/** @var array<string,Type>|null */
private ?array $componentTypesPairedStrings = null;
private ?Type $allComponentsTogetherType = null;
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'parse_url';
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if (count($functionCall->getArgs()) < 1) {
return ParametersAcceptorSelector::selectSingle(
$functionReflection->getVariants(),
)->getReturnType();
}
$this->cacheReturnTypes();
if (count($functionCall->getArgs()) > 1) {
$componentType = $scope->getType($functionCall->getArgs()[1]->value);
if (!$componentType instanceof ConstantType) {
return $this->createAllComponentsReturnType();
}
$componentType = $componentType->toInteger();
if (!$componentType instanceof ConstantIntegerType) {
throw new ShouldNotHappenException();
}
} else {
$componentType = new ConstantIntegerType(-1);
}
$urlType = $scope->getType($functionCall->getArgs()[0]->value);
if ($urlType instanceof ConstantStringType) {
try {
$result = @parse_url($urlType->getValue(), $componentType->getValue());
} catch (ValueError) {
return new ConstantBooleanType(false);
}
return $scope->getTypeFromValue($result);
}
if ($componentType->getValue() === -1) {
return $this->createAllComponentsReturnType();
}
return $this->componentTypesPairedConstants[$componentType->getValue()] ?? new ConstantBooleanType(false);
}
private function createAllComponentsReturnType(): Type
{
if ($this->allComponentsTogetherType === null) {
$returnTypes = [
new ConstantBooleanType(false),
];
$builder = ConstantArrayTypeBuilder::createEmpty();
if ($this->componentTypesPairedStrings === null) {
throw new ShouldNotHappenException();
}
foreach ($this->componentTypesPairedStrings as $componentName => $componentValueType) {
$builder->setOffsetValueType(new ConstantStringType($componentName), $componentValueType, true);
}
$returnTypes[] = $builder->getArray();
$this->allComponentsTogetherType = TypeCombinator::union(...$returnTypes);
}
return $this->allComponentsTogetherType;
}
private function cacheReturnTypes(): void
{
if ($this->componentTypesPairedConstants !== null) {
return;
}
$string = new StringType();
$port = IntegerRangeType::fromInterval(0, 65535);
$false = new ConstantBooleanType(false);
$null = new NullType();
$stringOrFalseOrNull = TypeCombinator::union($string, $false, $null);
$portOrFalseOrNull = TypeCombinator::union($port, $false, $null);
$this->componentTypesPairedConstants = [
PHP_URL_SCHEME => $stringOrFalseOrNull,
PHP_URL_HOST => $stringOrFalseOrNull,
PHP_URL_PORT => $portOrFalseOrNull,
PHP_URL_USER => $stringOrFalseOrNull,
PHP_URL_PASS => $stringOrFalseOrNull,
PHP_URL_PATH => $stringOrFalseOrNull,
PHP_URL_QUERY => $stringOrFalseOrNull,
PHP_URL_FRAGMENT => $stringOrFalseOrNull,
];
$this->componentTypesPairedStrings = [
'scheme' => $string,
'host' => $string,
'port' => $port,
'user' => $string,
'pass' => $string,
'path' => $string,
'query' => $string,
'fragment' => $string,
];
}
}