/
ElseAnalyzer.php
240 lines (205 loc) · 8.37 KB
/
ElseAnalyzer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Psalm\Internal\Analyzer\Statements\Block\IfElse;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Algebra;
use Psalm\Internal\Analyzer\ScopeAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Scope\IfScope;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Issue\ConflictingReferenceConstraint;
use Psalm\IssueBuffer;
use Psalm\Type\Reconciler;
use function array_diff_key;
use function array_key_exists;
use function array_keys;
use function array_merge;
use function count;
use function in_array;
use function preg_match;
use function preg_quote;
/**
* @internal
*/
class ElseAnalyzer
{
/**
* @return false|null
*/
public static function analyze(
StatementsAnalyzer $statements_analyzer,
?PhpParser\Node\Stmt\Else_ $else,
IfScope $if_scope,
Context $else_context,
Context $outer_context
): ?bool {
$codebase = $statements_analyzer->getCodebase();
if (!$else && !$if_scope->negated_clauses && !$else_context->clauses) {
$if_scope->final_actions = array_merge([ScopeAnalyzer::ACTION_NONE], $if_scope->final_actions);
$if_scope->assigned_var_ids = [];
$if_scope->new_vars = [];
$if_scope->redefined_vars = [];
$if_scope->reasonable_clauses = [];
return null;
}
$else_context->clauses = Algebra::simplifyCNF(
array_merge(
$else_context->clauses,
$if_scope->negated_clauses
)
);
$else_types = Algebra::getTruthsFromFormula($else_context->clauses);
$original_context = clone $else_context;
if ($else_types) {
$changed_var_ids = [];
[$else_context->vars_in_scope, $else_context->references_in_scope] = Reconciler::reconcileKeyedTypes(
$else_types,
[],
$else_context->vars_in_scope,
$else_context->references_in_scope,
$changed_var_ids,
[],
$statements_analyzer,
$statements_analyzer->getTemplateTypeMap() ?: [],
$else_context->inside_loop,
$else
? new CodeLocation($statements_analyzer->getSource(), $else, $outer_context->include_location)
: null
);
$else_context->clauses = Context::removeReconciledClauses($else_context->clauses, $changed_var_ids)[0];
foreach ($changed_var_ids as $changed_var_id => $_) {
foreach ($else_context->vars_in_scope as $var_id => $_) {
if (preg_match('/' . preg_quote($changed_var_id, '/') . '[\]\[\-]/', $var_id)
&& !array_key_exists($var_id, $changed_var_ids)
) {
$else_context->removePossibleReference($var_id);
}
}
}
}
$old_else_context = clone $else_context;
$pre_stmts_assigned_var_ids = $else_context->assigned_var_ids;
$else_context->assigned_var_ids = [];
$pre_possibly_assigned_var_ids = $else_context->possibly_assigned_var_ids;
$else_context->possibly_assigned_var_ids = [];
if ($else) {
if ($statements_analyzer->analyze(
$else->stmts,
$else_context
) === false
) {
return false;
}
}
foreach ($else_context->parent_remove_vars as $var_id => $_) {
$outer_context->removeVarFromConflictingClauses($var_id);
}
/** @var array<string, int> */
$new_assigned_var_ids = $else_context->assigned_var_ids;
$else_context->assigned_var_ids += $pre_stmts_assigned_var_ids;
/** @var array<string, bool> */
$new_possibly_assigned_var_ids = $else_context->possibly_assigned_var_ids;
$else_context->possibly_assigned_var_ids += $pre_possibly_assigned_var_ids;
if ($else) {
foreach ($else_context->byref_constraints as $var_id => $byref_constraint) {
if (isset($outer_context->byref_constraints[$var_id])
&& ($outer_constraint_type = $outer_context->byref_constraints[$var_id]->type)
&& $byref_constraint->type
&& !UnionTypeComparator::isContainedBy(
$codebase,
$byref_constraint->type,
$outer_constraint_type
)
) {
IssueBuffer::maybeAdd(
new ConflictingReferenceConstraint(
'There is more than one pass-by-reference constraint on ' . $var_id,
new CodeLocation($statements_analyzer, $else, $outer_context->include_location, true)
),
$statements_analyzer->getSuppressedIssues()
);
} else {
$outer_context->byref_constraints[$var_id] = $byref_constraint;
}
}
}
$final_actions = $else
? ScopeAnalyzer::getControlActions(
$else->stmts,
$statements_analyzer->node_data,
[]
)
: [ScopeAnalyzer::ACTION_NONE];
// has a return/throw at end
$has_ending_statements = $final_actions === [ScopeAnalyzer::ACTION_END];
$has_leaving_statements = $has_ending_statements
|| (count($final_actions) && !in_array(ScopeAnalyzer::ACTION_NONE, $final_actions, true));
$has_break_statement = $final_actions === [ScopeAnalyzer::ACTION_BREAK];
$has_continue_statement = $final_actions === [ScopeAnalyzer::ACTION_CONTINUE];
$if_scope->final_actions = array_merge($final_actions, $if_scope->final_actions);
// if it doesn't end in a return
if (!$has_leaving_statements) {
IfAnalyzer::updateIfScope(
$codebase,
$if_scope,
$else_context,
$original_context,
$new_assigned_var_ids,
$new_possibly_assigned_var_ids,
[],
true
);
$if_scope->reasonable_clauses = [];
}
// update the parent context as necessary
if ($if_scope->negatable_if_types) {
$outer_context->update(
$old_else_context,
$else_context,
$has_leaving_statements,
array_keys($if_scope->negatable_if_types),
$if_scope->updated_vars
);
}
if (!$has_ending_statements) {
$vars_possibly_in_scope = array_diff_key(
$else_context->vars_possibly_in_scope,
$outer_context->vars_possibly_in_scope
);
$possibly_assigned_var_ids = $new_possibly_assigned_var_ids;
if ($has_leaving_statements) {
if ($else_context->loop_scope) {
if (!$has_continue_statement && !$has_break_statement) {
$if_scope->new_vars_possibly_in_scope = array_merge(
$vars_possibly_in_scope,
$if_scope->new_vars_possibly_in_scope
);
}
$else_context->loop_scope->vars_possibly_in_scope = array_merge(
$vars_possibly_in_scope,
$else_context->loop_scope->vars_possibly_in_scope
);
}
} else {
$if_scope->new_vars_possibly_in_scope = array_merge(
$vars_possibly_in_scope,
$if_scope->new_vars_possibly_in_scope
);
$if_scope->possibly_assigned_var_ids = array_merge(
$possibly_assigned_var_ids,
$if_scope->possibly_assigned_var_ids
);
}
}
if ($outer_context->collect_exceptions) {
$outer_context->mergeExceptions($else_context);
}
// Track references set in the else to make sure they aren't reused later
$outer_context->updateReferencesPossiblyFromConfusingScope(
$else_context,
$statements_analyzer
);
return null;
}
}