-
Notifications
You must be signed in to change notification settings - Fork 651
/
IfConditionalAnalyzer.php
372 lines (312 loc) · 13.6 KB
/
IfConditionalAnalyzer.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
namespace Psalm\Internal\Analyzer\Statements\Block;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\Exception\ScopeAnalysisException;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Clause;
use Psalm\Internal\Scope\IfConditionalScope;
use Psalm\Internal\Scope\IfScope;
use Psalm\Issue\DocblockTypeContradiction;
use Psalm\Issue\RedundantCondition;
use Psalm\Issue\RedundantConditionGivenDocblockType;
use Psalm\Issue\TypeDoesNotContainType;
use Psalm\IssueBuffer;
use Psalm\Type\Reconciler;
use function array_diff_key;
use function array_filter;
use function array_keys;
use function array_merge;
use function array_values;
use function count;
/**
* @internal
*/
class IfConditionalAnalyzer
{
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr $cond,
Context $outer_context,
Codebase $codebase,
IfScope $if_scope,
int $branch_point
): IfConditionalScope {
$entry_clauses = [];
// used when evaluating elseifs
if ($if_scope->negated_clauses) {
$entry_clauses = array_merge($outer_context->clauses, $if_scope->negated_clauses);
$changed_var_ids = [];
if ($if_scope->negated_types) {
[$vars_reconciled, $references_reconciled] = Reconciler::reconcileKeyedTypes(
$if_scope->negated_types,
[],
$outer_context->vars_in_scope,
$outer_context->references_in_scope,
$changed_var_ids,
[],
$statements_analyzer,
[],
$outer_context->inside_loop,
new CodeLocation(
$statements_analyzer->getSource(),
$cond instanceof PhpParser\Node\Expr\BooleanNot
? $cond->expr
: $cond,
$outer_context->include_location,
false
)
);
if ($changed_var_ids) {
$outer_context = clone $outer_context;
$outer_context->vars_in_scope = $vars_reconciled;
$outer_context->references_in_scope = $references_reconciled;
$entry_clauses = array_values(
array_filter(
$entry_clauses,
static fn(Clause $c): bool => count($c->possibilities) > 1
|| $c->wedge
|| !isset($changed_var_ids[array_keys($c->possibilities)[0]])
)
);
}
}
}
// get the first expression in the if, which should be evaluated on its own
// this allows us to update the context of $matches in
// if (!preg_match('/a/', 'aa', $matches)) {
// exit
// }
// echo $matches[0];
$externally_applied_if_cond_expr = self::getDefinitelyEvaluatedExpressionAfterIf($cond);
$internally_applied_if_cond_expr = self::getDefinitelyEvaluatedExpressionInsideIf($cond);
$pre_condition_vars_in_scope = $outer_context->vars_in_scope;
$referenced_var_ids = $outer_context->cond_referenced_var_ids;
$outer_context->cond_referenced_var_ids = [];
$pre_assigned_var_ids = $outer_context->assigned_var_ids;
$outer_context->assigned_var_ids = [];
$if_context = null;
if ($internally_applied_if_cond_expr !== $externally_applied_if_cond_expr) {
$if_context = clone $outer_context;
}
$was_inside_conditional = $outer_context->inside_conditional;
$outer_context->inside_conditional = true;
if (ExpressionAnalyzer::analyze(
$statements_analyzer,
$externally_applied_if_cond_expr,
$outer_context
) === false) {
throw new ScopeAnalysisException();
}
$first_cond_assigned_var_ids = $outer_context->assigned_var_ids;
$outer_context->assigned_var_ids = array_merge(
$pre_assigned_var_ids,
$first_cond_assigned_var_ids
);
$first_cond_referenced_var_ids = $outer_context->cond_referenced_var_ids;
$outer_context->cond_referenced_var_ids = array_merge(
$referenced_var_ids,
$first_cond_referenced_var_ids
);
$outer_context->inside_conditional = $was_inside_conditional;
if (!$if_context) {
$if_context = clone $outer_context;
}
$if_conditional_context = clone $if_context;
// here we set up a context specifically for the statements in the first `if`, which can
// be affected by statements in the if condition
$if_conditional_context->if_body_context = $if_context;
if ($codebase->alter_code) {
$if_context->branch_point = $branch_point;
}
// we need to clone the current context so our ongoing updates
// to $outer_context don't mess with elseif/else blocks
$post_if_context = clone $outer_context;
if ($internally_applied_if_cond_expr !== $cond
|| $externally_applied_if_cond_expr !== $cond
) {
$assigned_var_ids = $first_cond_assigned_var_ids;
$if_conditional_context->assigned_var_ids = [];
$referenced_var_ids = $first_cond_referenced_var_ids;
$if_conditional_context->cond_referenced_var_ids = [];
$was_inside_conditional = $if_conditional_context->inside_conditional;
$if_conditional_context->inside_conditional = true;
if (ExpressionAnalyzer::analyze($statements_analyzer, $cond, $if_conditional_context) === false) {
throw new ScopeAnalysisException();
}
$if_conditional_context->inside_conditional = $was_inside_conditional;
/** @var array<string, bool> */
$more_cond_referenced_var_ids = $if_conditional_context->cond_referenced_var_ids;
$if_conditional_context->cond_referenced_var_ids = array_merge(
$more_cond_referenced_var_ids,
$referenced_var_ids
);
$cond_referenced_var_ids = array_merge(
$first_cond_referenced_var_ids,
$more_cond_referenced_var_ids
);
/** @var array<string, int> */
$more_cond_assigned_var_ids = $if_conditional_context->assigned_var_ids;
$if_conditional_context->assigned_var_ids = array_merge(
$more_cond_assigned_var_ids,
$assigned_var_ids
);
$assigned_in_conditional_var_ids = array_merge(
$first_cond_assigned_var_ids,
$more_cond_assigned_var_ids
);
} else {
$cond_referenced_var_ids = $first_cond_referenced_var_ids;
$assigned_in_conditional_var_ids = $first_cond_assigned_var_ids;
}
$newish_var_ids = [];
foreach (array_diff_key(
$if_conditional_context->vars_in_scope,
$pre_condition_vars_in_scope,
$cond_referenced_var_ids,
$assigned_in_conditional_var_ids
) as $name => $_value) {
$newish_var_ids[$name] = true;
}
self::handleParadoxicalCondition($statements_analyzer, $cond, true);
// get all the var ids that were referenced in the conditional, but not assigned in it
$cond_referenced_var_ids = array_diff_key($cond_referenced_var_ids, $assigned_in_conditional_var_ids);
$cond_referenced_var_ids = array_merge($newish_var_ids, $cond_referenced_var_ids);
return new IfConditionalScope(
$if_context,
$post_if_context,
$cond_referenced_var_ids,
$assigned_in_conditional_var_ids,
$entry_clauses
);
}
/**
* Returns statements that are definitely evaluated before any statements after the end of the
* if/elseif/else blocks
*/
private static function getDefinitelyEvaluatedExpressionAfterIf(PhpParser\Node\Expr $stmt): PhpParser\Node\Expr
{
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical
) {
if ($stmt->left instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->left->name->parts === ['true']
) {
return self::getDefinitelyEvaluatedExpressionAfterIf($stmt->right);
}
if ($stmt->right instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->right->name->parts === ['true']
) {
return self::getDefinitelyEvaluatedExpressionAfterIf($stmt->left);
}
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanAnd
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalAnd
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalXor
) {
return self::getDefinitelyEvaluatedExpressionAfterIf($stmt->left);
}
return $stmt;
}
if ($stmt instanceof PhpParser\Node\Expr\BooleanNot) {
$inner_stmt = self::getDefinitelyEvaluatedExpressionInsideIf($stmt->expr);
if ($inner_stmt !== $stmt->expr) {
return $inner_stmt;
}
}
return $stmt;
}
/**
* Returns statements that are definitely evaluated before any statements inside
* the if block
*/
private static function getDefinitelyEvaluatedExpressionInsideIf(PhpParser\Node\Expr $stmt): PhpParser\Node\Expr
{
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\Identical
) {
if ($stmt->left instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->left->name->parts === ['true']
) {
return self::getDefinitelyEvaluatedExpressionInsideIf($stmt->right);
}
if ($stmt->right instanceof PhpParser\Node\Expr\ConstFetch
&& $stmt->right->name->parts === ['true']
) {
return self::getDefinitelyEvaluatedExpressionInsideIf($stmt->left);
}
}
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp) {
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\BooleanOr
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalOr
|| $stmt instanceof PhpParser\Node\Expr\BinaryOp\LogicalXor
) {
return self::getDefinitelyEvaluatedExpressionInsideIf($stmt->left);
}
return $stmt;
}
if ($stmt instanceof PhpParser\Node\Expr\BooleanNot) {
$inner_stmt = self::getDefinitelyEvaluatedExpressionAfterIf($stmt->expr);
if ($inner_stmt !== $stmt->expr) {
return $inner_stmt;
}
}
return $stmt;
}
public static function handleParadoxicalCondition(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr $stmt,
bool $emit_redundant_with_assignation = false
): void {
$type = $statements_analyzer->node_data->getType($stmt);
if ($type !== null) {
if ($type->isAlwaysFalsy()) {
if ($type->from_docblock) {
IssueBuffer::maybeAdd(
new DocblockTypeContradiction(
'Operand of type ' . $type->getId() . ' is always falsy',
new CodeLocation($statements_analyzer, $stmt),
$type->getId() . ' falsy'
),
$statements_analyzer->getSuppressedIssues()
);
} else {
IssueBuffer::maybeAdd(
new TypeDoesNotContainType(
'Operand of type ' . $type->getId() . ' is always falsy',
new CodeLocation($statements_analyzer, $stmt),
$type->getId() . ' falsy'
),
$statements_analyzer->getSuppressedIssues()
);
}
} elseif ($type->isAlwaysTruthy() &&
(!$stmt instanceof PhpParser\Node\Expr\Assign || $emit_redundant_with_assignation)
) {
if ($type->from_docblock) {
IssueBuffer::maybeAdd(
new RedundantConditionGivenDocblockType(
'Operand of type ' . $type->getId() . ' is always truthy',
new CodeLocation($statements_analyzer, $stmt),
$type->getId() . ' falsy'
),
$statements_analyzer->getSuppressedIssues()
);
} else {
IssueBuffer::maybeAdd(
new RedundantCondition(
'Operand of type ' . $type->getId() . ' is always truthy',
new CodeLocation($statements_analyzer, $stmt),
$type->getId() . ' falsy'
),
$statements_analyzer->getSuppressedIssues()
);
}
}
}
}
}