-
Notifications
You must be signed in to change notification settings - Fork 672
/
Copy pathFunctionLikeStorage.php
359 lines (294 loc) · 7.69 KB
/
FunctionLikeStorage.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace Psalm\Storage;
use Psalm\CodeLocation;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Issue\CodeIssue;
use Psalm\Type\Union;
use function array_column;
use function array_fill_keys;
use function array_map;
use function count;
use function implode;
abstract class FunctionLikeStorage implements HasAttributesInterface
{
use CustomMetadataTrait;
use UnserializeMemoryUsageSuppressionTrait;
/**
* @var CodeLocation|null
*/
public $location;
/**
* @var CodeLocation|null
*/
public $stmt_location;
/**
* @psalm-readonly-allow-private-mutation
* @var list<FunctionLikeParameter>
*/
public $params = [];
/**
* @psalm-readonly-allow-private-mutation
* @var array<string, bool>
*/
public $param_lookup = [];
/**
* @var Union|null
*/
public $return_type;
/**
* @var CodeLocation|null
*/
public $return_type_location;
/**
* @var Union|null
*/
public $signature_return_type;
/**
* @var CodeLocation|null
*/
public $signature_return_type_location;
/**
* @var ?string
*/
public $cased_name;
/**
* @var array<int, string>
*/
public $suppressed_issues = [];
/**
* @var ?bool
*/
public $deprecated;
/**
* @var list<non-empty-string>
*/
public $internal = [];
/**
* @var bool
*/
public $variadic = false;
/**
* @var bool
*/
public $returns_by_ref = false;
/**
* @var ?int
*/
public $required_param_count;
/**
* @var array<string, Union>
*/
public $defined_constants = [];
/**
* @var array<string, bool>
*/
public $global_variables = [];
/**
* @var array<string, Union>
*/
public $global_types = [];
/**
* An array holding the class template "as" types.
*
* It's the de-facto list of all templates on a given class.
*
* The name of the template is the first key. The nested array is keyed by a unique
* function identifier. This allows operations with the same-named template defined
* across multiple classes and/or functions to not run into trouble.
*
* @var array<string, non-empty-array<string, Union>>|null
*/
public $template_types;
/**
* @var array<int, Possibilities>
*/
public $assertions = [];
/**
* @var array<int, Possibilities>
*/
public $if_true_assertions = [];
/**
* @var array<int, Possibilities>
*/
public $if_false_assertions = [];
/**
* @var bool
*/
public $has_visitor_issues = false;
/**
* @var list<CodeIssue>
*/
public $docblock_issues = [];
/**
* @var array<string, bool>
*/
public $throws = [];
/**
* @var array<string, CodeLocation>
*/
public $throw_locations = [];
/**
* @var bool
*/
public $has_yield = false;
/**
* @var bool
*/
public $mutation_free = false;
/**
* @var string|null
*/
public $return_type_description;
/**
* @psalm-suppress PossiblyUnusedProperty
* @var array<string, CodeLocation>|null
* @deprecated will be removed in Psalm 6. use {@see FunctionLikeStorage::$unused_docblock_parameters} instead
*/
public $unused_docblock_params;
/**
* @var array<string, CodeLocation>
*/
public array $unused_docblock_parameters = [];
public bool $has_undertyped_native_parameters = false;
/**
* @var bool
*/
public $pure = false;
/**
* Whether or not the function output is dependent solely on input - a function can be
* impure but still have this property (e.g. var_export). Useful for taint analysis.
*
* @var bool
*/
public $specialize_call = false;
/**
* @var array<string>
*/
public $taint_source_types = [];
/**
* @var array<string>
*/
public $added_taints = [];
/**
* @var array<string>
*/
public $removed_taints = [];
/**
* @var array<Union>
*/
public $conditionally_removed_taints = [];
/**
* @var array<int, string>
*/
public $return_source_params = [];
/**
* @var bool
*/
public $allow_named_arg_calls = true;
/**
* @var list<AttributeStorage>
*/
public $attributes = [];
/**
* @var list<array{fqn: string, params: array<int>, return: bool}>|null
*/
public $proxy_calls = [];
/**
* @var ?string
*/
public $description;
public bool $public_api = false;
/**
* Used in the Language Server
*/
public function getHoverMarkdown(): string
{
$params = count($this->params) > 0 ? "\n" . implode(
",\n",
array_map(
static function (FunctionLikeParameter $param): string {
$realType = $param->type ?: 'mixed';
return " {$realType} \${$param->name}";
},
$this->params,
),
) . "\n" : '';
$return_type = $this->return_type ?: 'mixed';
$symbol_text = "function {$this->cased_name}({$params}): {$return_type}";
if (!$this instanceof MethodStorage) {
return $symbol_text;
}
switch ($this->visibility) {
case ClassLikeAnalyzer::VISIBILITY_PRIVATE:
$visibility_text = 'private';
break;
case ClassLikeAnalyzer::VISIBILITY_PROTECTED:
$visibility_text = 'protected';
break;
default:
$visibility_text = 'public';
}
return $visibility_text . ' ' . $symbol_text;
}
public function getCompletionSignature(): string
{
$symbol_text = 'function ' . $this->cased_name . '(' . implode(
',',
array_map(
static fn(FunctionLikeParameter $param): string => ($param->type ?: 'mixed') . ' $' . $param->name,
$this->params,
),
) . ') : ' . ($this->return_type ?: 'mixed');
if (!$this instanceof MethodStorage) {
return $symbol_text;
}
switch ($this->visibility) {
case ClassLikeAnalyzer::VISIBILITY_PRIVATE:
$visibility_text = 'private';
break;
case ClassLikeAnalyzer::VISIBILITY_PROTECTED:
$visibility_text = 'protected';
break;
default:
$visibility_text = 'public';
}
return $visibility_text . ' ' . $symbol_text;
}
/**
* @internal
* @param list<FunctionLikeParameter> $params
*/
public function setParams(array $params): void
{
$this->params = $params;
$param_names = array_column($params, 'name');
$this->param_lookup = array_fill_keys($param_names, true);
}
/**
* @internal
*/
public function addParam(FunctionLikeParameter $param, ?bool $lookup_value = null): void
{
$this->params[] = $param;
$this->param_lookup[$param->name] = $lookup_value ?? true;
}
/**
* @return list<AttributeStorage>
*/
public function getAttributeStorages(): array
{
return $this->attributes;
}
public function __toString(): string
{
return $this->getCompletionSignature();
}
/**
* @deprecated will be removed in Psalm 6. use {@see FunctionLikeStorage::getCompletionSignature()} instead
* @psalm-suppress PossiblyUnusedParam, PossiblyUnusedMethod
*/
public function getSignature(bool $allow_newlines): string
{
return $this->getCompletionSignature();
}
}