forked from vimeo/psalm
/
ArrayCombineReturnTypeProvider.php
132 lines (115 loc) · 3.85 KB
/
ArrayCombineReturnTypeProvider.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
<?php
namespace Psalm\Internal\Provider\ReturnTypeProvider;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Issue\InvalidArgument;
use Psalm\IssueBuffer;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Union;
use function array_combine;
use function assert;
use function count;
/**
* @internal
*/
class ArrayCombineReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* @return array<lowercase-string>
*/
public static function getFunctionIds(): array
{
return ['array_combine'];
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Union
{
$statements_source = $event->getStatementsSource();
$call_args = $event->getCallArgs();
if (!$statements_source instanceof StatementsAnalyzer
|| count($call_args) < 2
) {
return Type::getNever();
}
if (!$keys_type = $statements_source->node_data->getType($call_args[0]->value)) {
return null;
}
if (!$keys_type->isArray()) {
return null;
}
$keys = $keys_type->getArray();
if ($keys instanceof TArray && $keys->isEmptyArray()) {
$keys = [];
} elseif (!$keys instanceof TKeyedArray || $keys->fallback_params) {
return null;
} else {
$keys = $keys->properties;
}
if (!$values_type = $statements_source->node_data->getType($call_args[1]->value)) {
return null;
}
if (!$values_type->isArray()) {
return null;
}
$values = $values_type->getArray();
if ($values instanceof TArray && $values->isEmptyArray()) {
$values = [];
} elseif (!$values instanceof TKeyedArray || $values->fallback_params) {
return null;
} else {
$values = $values->properties;
}
$keys_array = [];
$is_list = true;
$prev_key = -1;
foreach ($keys as $key) {
if ($key->possibly_undefined) {
return null;
}
if ($key->isSingleIntLiteral()) {
$key = $key->getSingleIntLiteral()->value;
$keys_array []= $key;
if ($is_list && $key-1 !== $prev_key) {
$is_list = false;
}
$prev_key = $key;
} elseif ($key->isSingleStringLiteral()) {
$keys_array []= $key->getSingleStringLiteral()->value;
$is_list = false;
} else {
return null;
}
}
foreach ($values as $value) {
if ($value->possibly_undefined) {
return null;
}
}
if (count($keys_array) !== count($values)) {
IssueBuffer::maybeAdd(
new InvalidArgument(
'The keys array ' . $keys_type->getId() . ' must have exactly the same '
. 'number of elements as the values array '
. $values_type->getId(),
$event->getCodeLocation(),
'array_combine'
),
$statements_source->getSuppressedIssues()
);
return $statements_source->getCodebase()->analysis_php_version_id >= 8_00_00
? Type::getNever()
: Type::getFalse();
}
$result = array_combine(
$keys_array,
$values
);
assert($result !== false);
if (!$result) {
return Type::getEmptyArray();
}
return new Union([new TKeyedArray($result, null, null, $is_list)]);
}
}