forked from vimeo/psalm
/
TValueOf.php
131 lines (116 loc) · 3.91 KB
/
TValueOf.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
<?php
namespace Psalm\Type\Atomic;
use Psalm\Codebase;
use Psalm\Internal\Codebase\ConstantTypeResolver;
use Psalm\Storage\EnumCaseStorage;
use Psalm\Type\Atomic;
use Psalm\Type\Union;
use function array_map;
use function array_values;
use function assert;
use function count;
/**
* Represents a value of an array or enum.
*/
final class TValueOf extends Atomic
{
/** @var Union */
public $type;
public function __construct(Union $type)
{
$this->type = $type;
}
public function getKey(bool $include_extra = true): string
{
return 'value-of<' . $this->type . '>';
}
/**
* @param array<lowercase-string, string> $aliased_classes
*/
public function toPhpString(
?string $namespace,
array $aliased_classes,
?string $this_class,
int $analysis_php_version_id
): ?string {
return null;
}
public function canBeFullyExpressedInPhp(int $analysis_php_version_id): bool
{
return false;
}
public function getAssertionString(): string
{
return 'mixed';
}
public static function isViableTemplateType(Union $template_type): bool
{
foreach ($template_type->getAtomicTypes() as $type) {
if (!$type instanceof TArray
&& !$type instanceof TClassConstant
&& !$type instanceof TKeyedArray
&& !$type instanceof TList
&& !$type instanceof TPropertiesOf
&& !$type instanceof TNamedObject
) {
return false;
}
}
return true;
}
public static function getValueType(
Union $type,
Codebase $codebase,
bool $keep_template_params = false,
): ?Union {
$value_types = [];
foreach ($type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof TArray) {
$value_atomics = $atomic_type->type_params[1];
} elseif ($atomic_type instanceof TList) {
$value_atomics = $atomic_type->type_param;
} elseif ($atomic_type instanceof TKeyedArray) {
$value_atomics = $atomic_type->getGenericValueType();
} elseif ($atomic_type instanceof TTemplateParam) {
if ($keep_template_params) {
$value_atomics = new Union([$atomic_type]);
} else {
$value_atomics = static::getValueType(
$atomic_type->as,
$codebase,
$keep_template_params
);
if ($value_atomics === null) {
continue;
}
}
} elseif ($atomic_type instanceof TNamedObject
&& $codebase->classlike_storage_provider->has($atomic_type->value)
) {
$class_storage = $codebase->classlike_storage_provider->get($atomic_type->value);
$cases = $class_storage->enum_cases;
if (!$class_storage->is_enum
|| $class_storage->enum_type === null
|| count($cases) === 0
) {
// Invalid value-of, skip
continue;
}
$value_atomics = new Union(array_map(
function (EnumCaseStorage $case): Atomic {
assert($case->value !== null); // Backed enum must have a value
return ConstantTypeResolver::getLiteralTypeFromScalarValue($case->value);
},
array_values($cases),
));
} else {
continue;
}
$value_types = [...$value_types, ...array_values($value_atomics->getAtomicTypes())];
}
if ($value_types === []) {
return null;
}
return new Union($value_types);
}
}