/
TKeyOf.php
106 lines (92 loc) · 2.77 KB
/
TKeyOf.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
<?php
namespace Psalm\Type\Atomic;
use Psalm\Type;
use Psalm\Type\Union;
use function array_merge;
use function array_values;
/**
* Represents an offset of an array.
*/
final class TKeyOf extends TArrayKey
{
/** @var Union */
public $type;
public function __construct(Union $type)
{
$this->type = $type;
}
public function getKey(bool $include_extra = true): string
{
return 'key-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
) {
return false;
}
}
return true;
}
public static function getArrayKeyType(
Union $type,
bool $keep_template_params = false
): ?Union {
$key_types = [];
foreach ($type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof TArray) {
$array_key_atomics = $atomic_type->type_params[0];
} elseif ($atomic_type instanceof TList) {
$array_key_atomics = Type::getInt();
} elseif ($atomic_type instanceof TKeyedArray) {
$array_key_atomics = $atomic_type->getGenericKeyType();
} elseif ($atomic_type instanceof TTemplateParam) {
if ($keep_template_params) {
$array_key_atomics = new Union([$atomic_type]);
} else {
$array_key_atomics = static::getArrayKeyType(
$atomic_type->as,
$keep_template_params
);
if ($array_key_atomics === null) {
continue;
}
}
} else {
continue;
}
$key_types = array_merge(
$key_types,
array_values($array_key_atomics->getAtomicTypes())
);
}
if ($key_types === []) {
return null;
}
return new Union($key_types);
}
}