-
Notifications
You must be signed in to change notification settings - Fork 664
/
ClassLikeStorage.php
480 lines (379 loc) · 11.4 KB
/
ClassLikeStorage.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
declare(strict_types=1);
namespace Psalm\Storage;
use Psalm\Aliases;
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Config;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\MethodIdentifier;
use Psalm\Internal\Type\TypeAlias\ClassTypeAlias;
use Psalm\Issue\CodeIssue;
use Psalm\Issue\DeprecatedClass;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union;
use function array_values;
use function in_array;
final class ClassLikeStorage implements HasAttributesInterface
{
use CustomMetadataTrait;
use UnserializeMemoryUsageSuppressionTrait;
/**
* @var array<string, ClassConstantStorage>
*/
public array $constants = [];
/**
* Aliases to help Psalm understand constant refs
*/
public ?Aliases $aliases = null;
public bool $populated = false;
public bool $stubbed = false;
public bool $deprecated = false;
/**
* @var list<non-empty-string>
*/
public array $internal = [];
/**
* @var TTemplateParam[]
*/
public array $templatedMixins = [];
/**
* @var list<TNamedObject>
*/
public array $namedMixins = [];
public ?string $mixin_declaring_fqcln = null;
public ?bool $sealed_properties = null;
public ?bool $sealed_methods = null;
public bool $override_property_visibility = false;
public bool $override_method_visibility = false;
/**
* @var array<int, string>
*/
public array $suppressed_issues = [];
/**
* Is this class user-defined
*/
public bool $user_defined = false;
/**
* Interfaces this class implements directly
*
* @var array<lowercase-string, string>
*/
public array $direct_class_interfaces = [];
/**
* Interfaces this class implements explicitly and implicitly
*
* @var array<lowercase-string, string>
*/
public array $class_implements = [];
/**
* Parent interfaces listed explicitly
*
* @var array<lowercase-string, string>
*/
public array $direct_interface_parents = [];
/**
* Parent interfaces
*
* @var array<lowercase-string, string>
*/
public array $parent_interfaces = [];
/**
* There can only be one direct parent class
*/
public ?string $parent_class = null;
/**
* Parent classes
*
* @var array<lowercase-string, string>
*/
public array $parent_classes = [];
public ?CodeLocation $location = null;
public ?CodeLocation $stmt_location = null;
public ?CodeLocation $namespace_name_location = null;
public bool $abstract = false;
public bool $final = false;
public bool $final_from_docblock = false;
/**
* @var array<lowercase-string, string>
*/
public array $used_traits = [];
/**
* @var array<lowercase-string, lowercase-string>
*/
public array $trait_alias_map = [];
/**
* @var array<string, string>
*/
public array $trait_alias_map_cased = [];
/**
* @var array<lowercase-string, bool>
*/
public array $trait_final_map = [];
/**
* @var array<string, ClassLikeAnalyzer::VISIBILITY_*>
*/
public array $trait_visibility_map = [];
public bool $is_trait = false;
public bool $is_interface = false;
public bool $is_enum = false;
public bool $external_mutation_free = false;
public bool $mutation_free = false;
public bool $specialize_instance = false;
/**
* @var array<lowercase-string, MethodStorage>
*/
public array $methods = [];
/**
* @var array<lowercase-string, MethodStorage>
*/
public array $pseudo_methods = [];
/**
* @var array<lowercase-string, MethodStorage>
*/
public array $pseudo_static_methods = [];
/**
* Maps pseudo method names to the original declaring method identifier
* The key is the method name in lowercase, and the value is the original `MethodIdentifier` instance
*
* This property contains all pseudo methods declared on ancestors.
*
* @var array<lowercase-string, MethodIdentifier>
*/
public array $declaring_pseudo_method_ids = [];
/**
* @var array<lowercase-string, MethodIdentifier>
*/
public array $declaring_method_ids = [];
/**
* @var array<lowercase-string, MethodIdentifier>
*/
public array $appearing_method_ids = [];
/**
* Map from lowercase method name to list of declarations in order from parent, to grandparent, to
* great-grandparent, etc **including traits and interfaces**. Ancestors that don't have their own declaration are
* skipped.
*
* @var array<lowercase-string, array<string, MethodIdentifier>>
*/
public array $overridden_method_ids = [];
/**
* @var array<lowercase-string, MethodIdentifier>
*/
public array $documenting_method_ids = [];
/**
* @var array<lowercase-string, MethodIdentifier>
*/
public array $inheritable_method_ids = [];
/**
* @var array<lowercase-string, array<string, bool>>
*/
public array $potential_declaring_method_ids = [];
/**
* @var array<string, PropertyStorage>
*/
public array $properties = [];
/**
* @var array<string, Union>
*/
public array $pseudo_property_set_types = [];
/**
* @var array<string, Union>
*/
public array $pseudo_property_get_types = [];
/**
* @var array<string, string>
*/
public array $declaring_property_ids = [];
/**
* @var array<string, string>
*/
public array $appearing_property_ids = [];
public ?Union $inheritors = null;
/**
* @var array<string, string>
*/
public array $inheritable_property_ids = [];
/**
* @var array<string, array<string>>
*/
public array $overridden_property_ids = [];
/**
* 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 the defining class
* (i.e. the same as the class name). This allows operations with the same-named template defined
* across multiple classes to not run into trouble.
*
* @var array<string, non-empty-array<string, Union>>|null
*/
public ?array $template_types = null;
/**
* @var array<int, bool>|null
*/
public ?array $template_covariants = null;
/**
* A map of which generic classlikes are extended or implemented by this class or interface.
*
* This is only used in the populator, which poulates the $template_extended_params property below.
*
* @internal
* @var array<string, non-empty-array<int, Union>>|null
*/
public ?array $template_extended_offsets = null;
/**
* A map of which generic classlikes are extended or implemented by this class or interface.
*
* The annotation "@extends Traversable<SomeClass, SomeOtherClass>" would generate an entry of
*
* [
* "Traversable" => [
* "TKey" => new Union([new TNamedObject("SomeClass")]),
* "TValue" => new Union([new TNamedObject("SomeOtherClass")])
* ]
* ]
*
* @var array<string, array<string, Union>>|null
*/
public ?array $template_extended_params = null;
/**
* @var array<string, int>|null
*/
public ?array $template_type_extends_count = null;
/**
* @var array<string, int>|null
*/
public ?array $template_type_implements_count = null;
public ?Union $yield = null;
public ?string $declaring_yield_fqcn = null;
/**
* @var array<string, int>|null
*/
public ?array $template_type_uses_count = null;
/**
* @var array<string, bool>
*/
public array $initialized_properties = [];
/**
* @var array<string, true>
*/
public array $invalid_dependencies = [];
/**
* @var array<lowercase-string, bool>
*/
public array $dependent_classlikes = [];
/**
* A hash of the source file's name, contents, and this file's modified on date
*/
public string $hash = '';
public bool $has_visitor_issues = false;
/**
* @var list<CodeIssue>
*/
public array $docblock_issues = [];
/**
* @var array<string, ClassTypeAlias>
*/
public array $type_aliases = [];
public bool $preserve_constructor_signature = false;
public bool $enforce_template_inheritance = false;
public ?string $extension_requirement = null;
/**
* @var array<int, string>
*/
public array $implementation_requirements = [];
/**
* @var list<AttributeStorage>
*/
public array $attributes = [];
/**
* @var array<string, EnumCaseStorage>
*/
public array $enum_cases = [];
/**
* @var 'int'|'string'|null
*/
public ?string $enum_type = null;
public ?string $description = null;
public bool $public_api = false;
public bool $readonly = false;
public function __construct(public string $name)
{
}
/**
* @return list<AttributeStorage>
*/
public function getAttributeStorages(): array
{
return $this->attributes;
}
public function hasAttributeIncludingParents(
string $fq_class_name,
Codebase $codebase,
): bool {
if ($this->hasAttribute($fq_class_name)) {
return true;
}
foreach ($this->parent_classes as $parent_class) {
// skip missing dependencies
if (!$codebase->classlike_storage_provider->has($parent_class)) {
continue;
}
$parent_class_storage = $codebase->classlike_storage_provider->get($parent_class);
if ($parent_class_storage->hasAttribute($fq_class_name)) {
return true;
}
}
return false;
}
/**
* Get the template constraint types for the class.
*
* @return list<Union>
*/
public function getClassTemplateTypes(): array
{
$type_params = [];
foreach ($this->template_types ?? [] as $type_map) {
$type_params[] = array_values($type_map)[0];
}
return $type_params;
}
public function hasSealedProperties(Config $config): bool
{
return $this->sealed_properties ?? $config->seal_all_properties;
}
public function hasSealedMethods(Config $config): bool
{
return $this->sealed_methods ?? $config->seal_all_methods;
}
private function hasAttribute(string $fq_class_name): bool
{
foreach ($this->attributes as $attribute) {
if ($fq_class_name === $attribute->fq_class_name) {
return true;
}
}
return false;
}
/**
* @return array<int, string>
*/
public function getSuppressedIssuesForTemplateExtendParams(): array
{
$allowed_issue_types = [
DeprecatedClass::getIssueType(),
];
$suppressed_issues_for_template_extend_params = [];
foreach ($this->suppressed_issues as $offset => $suppressed_issue) {
if (!in_array($suppressed_issue, $allowed_issue_types, true)) {
continue;
}
$suppressed_issues_for_template_extend_params[$offset] = $suppressed_issue;
}
return $suppressed_issues_for_template_extend_params;
}
}