-
Notifications
You must be signed in to change notification settings - Fork 650
/
GetObjectVarsReturnTypeProvider.php
184 lines (163 loc) · 6.53 KB
/
GetObjectVarsReturnTypeProvider.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
<?php
namespace Psalm\Internal\Provider\ReturnTypeProvider;
use BackedEnum;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\ClassAnalyzer;
use Psalm\Internal\Analyzer\SourceAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\AtomicPropertyFetchAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TObjectWithProperties;
use Psalm\Type\Union;
use UnitEnum;
use stdClass;
use function is_int;
use function is_string;
use function reset;
use function strtolower;
/**
* @internal
*/
class GetObjectVarsReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* @return array<lowercase-string>
*/
public static function getFunctionIds(): array
{
return [
'get_object_vars',
];
}
private static ?TArray $fallback = null;
/**
* @return TArray|TKeyedArray
*/
public static function getGetObjectVarsReturnType(
Union $first_arg_type,
SourceAnalyzer $statements_source,
Context $context,
CodeLocation $location
): Atomic {
self::$fallback ??= new TArray([Type::getString(), Type::getMixed()]);
if ($first_arg_type->isSingle()) {
$atomics = $first_arg_type->getAtomicTypes();
$object_type = reset($atomics);
if ($object_type instanceof Atomic\TEnumCase) {
$properties = ['name' => new Union([new Atomic\TLiteralString($object_type->case_name)])];
$codebase = $statements_source->getCodebase();
$enum_classlike_storage = $codebase->classlike_storage_provider->get($object_type->value);
if ($enum_classlike_storage->enum_type === null) {
return new TKeyedArray($properties);
}
$enum_case_storage = $enum_classlike_storage->enum_cases[$object_type->case_name];
if (is_int($enum_case_storage->value)) {
$properties['value'] = new Union([new Atomic\TLiteralInt($enum_case_storage->value)]);
} elseif (is_string($enum_case_storage->value)) {
$properties['value'] = new Union([new Atomic\TLiteralString($enum_case_storage->value)]);
}
return new TKeyedArray($properties);
}
if ($object_type instanceof TObjectWithProperties) {
if ([] === $object_type->properties) {
return self::$fallback;
}
return new TKeyedArray($object_type->properties);
}
if ($object_type instanceof TNamedObject) {
if (strtolower($object_type->value) === strtolower(stdClass::class)) {
return self::$fallback;
}
$codebase = $statements_source->getCodebase();
$class_storage = $codebase->classlikes->getStorageFor($object_type->value);
if (null === $class_storage) {
return self::$fallback;
}
if ([] === $class_storage->appearing_property_ids) {
if ($class_storage->final) {
return Type::getEmptyArrayAtomic();
}
return self::$fallback;
}
$properties = [];
foreach ($class_storage->appearing_property_ids as $name => $property_id) {
if (ClassAnalyzer::checkPropertyVisibility(
$property_id,
$context,
$statements_source,
$location,
$statements_source->getSuppressedIssues(),
false,
) === true) {
$property_type = $codebase->properties->getPropertyType(
$property_id,
false,
$statements_source,
$context,
);
if (!$property_type) {
continue;
}
$property_type = $object_type instanceof TGenericObject
? AtomicPropertyFetchAnalyzer::localizePropertyType(
$codebase,
$property_type,
$object_type,
$class_storage,
$class_storage,
)
: $property_type
;
$properties[$name] = $property_type;
}
}
if ([] === $properties) {
if ($class_storage->final) {
return Type::getEmptyArrayAtomic();
}
return self::$fallback;
}
return new TKeyedArray(
$properties,
null,
$class_storage->final
|| $class_storage->name === UnitEnum::class
|| $class_storage->name === BackedEnum::class
|| $codebase->classImplements($class_storage->name, UnitEnum::class)
? null
: [Type::getString(), Type::getMixed()],
);
}
}
return self::$fallback;
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union
{
$statements_source = $event->getStatementsSource();
$call_args = $event->getCallArgs();
if (!$statements_source instanceof StatementsAnalyzer
|| !$call_args
) {
return Type::getMixed();
}
if (($first_arg_type = $statements_source->node_data->getType($call_args[0]->value))
&& $first_arg_type->isObjectType()
) {
return new Union([self::getGetObjectVarsReturnType(
$first_arg_type,
$statements_source,
$event->getContext(),
$event->getCodeLocation(),
)]);
}
return null;
}
}