/
ParseUrlReturnTypeProvider.php
163 lines (135 loc) · 5.62 KB
/
ParseUrlReturnTypeProvider.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
156
157
158
159
160
161
162
163
<?php
namespace Psalm\Internal\Provider\ReturnTypeProvider;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Union;
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;
class ParseUrlReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* @return array<lowercase-string>
*/
public static function getFunctionIds(): array
{
return ['parse_url'];
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): Union
{
$statements_source = $event->getStatementsSource();
$call_args = $event->getCallArgs();
if (!$statements_source instanceof StatementsAnalyzer) {
return Type::getMixed();
}
if (isset($call_args[1])) {
$is_default_component = false;
if ($component_type = $statements_source->node_data->getType($call_args[1]->value)) {
if (!$component_type->hasMixed()) {
$codebase = $statements_source->getCodebase();
$acceptable_string_component_type = new Union([
new TLiteralInt(PHP_URL_SCHEME),
new TLiteralInt(PHP_URL_USER),
new TLiteralInt(PHP_URL_PASS),
new TLiteralInt(PHP_URL_HOST),
new TLiteralInt(PHP_URL_PATH),
new TLiteralInt(PHP_URL_QUERY),
new TLiteralInt(PHP_URL_FRAGMENT),
]);
$acceptable_int_component_type = new Union([
new TLiteralInt(PHP_URL_PORT),
]);
if (UnionTypeComparator::isContainedBy(
$codebase,
$component_type,
$acceptable_string_component_type
)) {
$nullable_falsable_string = new Union([
new TString,
new TFalse,
new TNull,
]);
$codebase = $statements_source->getCodebase();
if ($codebase->config->ignore_internal_nullable_issues) {
$nullable_falsable_string->ignore_nullable_issues = true;
}
if ($codebase->config->ignore_internal_falsable_issues) {
$nullable_falsable_string->ignore_falsable_issues = true;
}
return $nullable_falsable_string;
}
if (UnionTypeComparator::isContainedBy(
$codebase,
$component_type,
$acceptable_int_component_type
)) {
$nullable_falsable_int = new Union([
new TInt,
new TFalse,
new TNull,
]);
$codebase = $statements_source->getCodebase();
if ($codebase->config->ignore_internal_nullable_issues) {
$nullable_falsable_int->ignore_nullable_issues = true;
}
if ($codebase->config->ignore_internal_falsable_issues) {
$nullable_falsable_int->ignore_falsable_issues = true;
}
return $nullable_falsable_int;
}
if ($component_type->isSingleIntLiteral()) {
$component_type_type = $component_type->getSingleIntLiteral();
$is_default_component = $component_type_type->value <= -1;
}
}
}
if (!$is_default_component) {
$nullable_string_or_int = new Union([
new TString,
new TInt,
new TNull,
]);
$codebase = $statements_source->getCodebase();
if ($codebase->config->ignore_internal_nullable_issues) {
$nullable_string_or_int->ignore_nullable_issues = true;
}
return $nullable_string_or_int;
}
}
$component_types = [
'scheme' => Type::getString(),
'user' => Type::getString(),
'pass' => Type::getString(),
'host' => Type::getString(),
'port' => Type::getInt(),
'path' => Type::getString(),
'query' => Type::getString(),
'fragment' => Type::getString(),
];
foreach ($component_types as $component_type) {
$component_type->possibly_undefined = true;
}
$return_type = new Union([
new TKeyedArray($component_types),
new TFalse(),
]);
if ($statements_source->getCodebase()->config->ignore_internal_falsable_issues) {
$return_type->ignore_falsable_issues = true;
}
return $return_type;
}
}