forked from vimeo/psalm
/
UnsetAnalyzer.php
154 lines (138 loc) · 6.67 KB
/
UnsetAnalyzer.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
<?php
namespace Psalm\Internal\Analyzer\Statements;
use PhpParser;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TArrayKey;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNever;
use Psalm\Type\Atomic\TNonEmptyArray;
use Psalm\Type\Atomic\TNonEmptyMixed;
use Psalm\Type\Union;
use function count;
/**
* @internal
*/
class UnsetAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Stmt\Unset_ $stmt,
Context $context
): void {
$context->inside_unset = true;
foreach ($stmt->vars as $var) {
$was_inside_general_use = $context->inside_general_use;
$context->inside_general_use = true;
ExpressionAnalyzer::analyze($statements_analyzer, $var, $context);
$context->inside_general_use = $was_inside_general_use;
$var_id = ExpressionIdentifier::getExtendedVarId(
$var,
$statements_analyzer->getFQCLN(),
$statements_analyzer
);
if ($var_id) {
$context->remove($var_id);
unset($context->references_possibly_from_confusing_scope[$var_id]);
}
if ($var instanceof PhpParser\Node\Expr\ArrayDimFetch && $var->dim) {
$root_var_id = ExpressionIdentifier::getExtendedVarId(
$var->var,
$statements_analyzer->getFQCLN(),
$statements_analyzer
);
$key_type = $statements_analyzer->node_data->getType($var->dim);
if ($root_var_id && isset($context->vars_in_scope[$root_var_id]) && $key_type) {
$root_types = [];
foreach ($context->vars_in_scope[$root_var_id]->getAtomicTypes() as $atomic_root_type) {
if ($atomic_root_type instanceof TKeyedArray) {
$key_value = null;
if ($key_type->isSingleIntLiteral()) {
$key_value = $key_type->getSingleIntLiteral()->value;
} elseif ($key_type->isSingleStringLiteral()) {
$key_value = $key_type->getSingleStringLiteral()->value;
}
if ($key_value !== null) {
$properties = $atomic_root_type->properties;
$is_list = $atomic_root_type->is_list;
if (isset($properties[$key_value])) {
if ($is_list
&& $key_value !== count($properties)-1
) {
$is_list = false;
}
unset($properties[$key_value]);
}
/** @psalm-suppress DocblockTypeContradiction https://github.com/vimeo/psalm/issues/8518 */
if (!$properties) {
if ($atomic_root_type->fallback_value_type) {
$root_types [] =
new TArray([
$atomic_root_type->fallback_key_type
? $atomic_root_type->fallback_key_type
: new Union([new TArrayKey]),
$atomic_root_type->fallback_value_type,
])
;
} else {
$root_types [] =
new TArray([
new Union([new TNever]),
new Union([new TNever]),
])
;
}
} else {
$root_types []= new TKeyedArray(
$properties,
null,
$atomic_root_type->fallback_key_type,
$atomic_root_type->fallback_value_type,
$is_list
);
}
} else {
$properties = [];
foreach ($atomic_root_type->properties as $key => $type) {
$properties[$key] = $type->setPossiblyUndefined(true);
}
$root_types []= new TKeyedArray(
$properties,
null,
$atomic_root_type->fallback_key_type,
$atomic_root_type->fallback_value_type,
false,
);
}
} elseif ($atomic_root_type instanceof TNonEmptyArray) {
$root_types []= new TArray($atomic_root_type->type_params);
} elseif ($atomic_root_type instanceof TNonEmptyMixed) {
$root_types []= new TMixed();
} elseif ($atomic_root_type instanceof TList) {
$root_types []=
new TArray([
Type::getInt(),
$atomic_root_type->type_param
])
;
} else {
$root_types []= $atomic_root_type;
}
}
$context->vars_in_scope[$root_var_id] = new Union($root_types);
$context->removeVarFromConflictingClauses(
$root_var_id,
$context->vars_in_scope[$root_var_id],
$statements_analyzer
);
}
}
}
$context->inside_unset = false;
}
}