1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-07 05:28:37 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php

509 lines
17 KiB
PHP
Raw Normal View History

2016-10-22 19:23:18 +02:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Analyzer\Statements\Block;
2016-10-22 19:23:18 +02:00
2016-11-02 07:29:00 +01:00
use PhpParser;
2021-06-08 04:55:21 +02:00
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Algebra;
2020-11-03 22:15:44 +01:00
use Psalm\Internal\Algebra\FormulaGenerator;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\AlgebraAnalyzer;
use Psalm\Internal\Analyzer\ScopeAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Clause;
use Psalm\Internal\Scope\IfScope;
use Psalm\Node\Expr\VirtualBooleanNot;
2016-10-22 19:23:18 +02:00
use Psalm\Type;
use Psalm\Type\Reconciler;
2021-06-08 04:55:21 +02:00
use function array_combine;
use function array_diff_key;
use function array_filter;
2021-06-08 04:55:21 +02:00
use function array_intersect_key;
use function array_keys;
2021-06-08 04:55:21 +02:00
use function array_map;
use function array_merge;
use function array_reduce;
use function array_unique;
2021-06-08 04:55:21 +02:00
use function array_values;
use function count;
use function in_array;
2021-06-08 04:55:21 +02:00
use function preg_match;
use function preg_quote;
2016-10-22 19:23:18 +02:00
/**
* @internal
*/
2020-11-07 02:51:14 +01:00
class IfElseAnalyzer
2016-10-22 19:23:18 +02:00
{
/**
* System of type substitution and deletion
*
* for example
*
* x: A|null
*
* if (x)
* (x: A)
* x = B -- effects: remove A from the type of x, add B
* else
* (x: null)
* x = C -- effects: remove null from the type of x, add C
*
*
* x: A|null
*
* if (!x)
* (x: null)
* throw new Exception -- effects: remove null from the type of x
*
2017-05-27 02:16:18 +02:00
*
2016-10-22 19:23:18 +02:00
* @return null|false
*/
public static function analyze(
2018-11-11 18:01:14 +01:00
StatementsAnalyzer $statements_analyzer,
2016-11-02 07:29:00 +01:00
PhpParser\Node\Stmt\If_ $stmt,
Context $context
): ?bool {
2018-11-11 18:01:14 +01:00
$codebase = $statements_analyzer->getCodebase();
2018-11-06 03:57:36 +01:00
$if_scope = new IfScope();
// We need to clone the original context for later use if we're exiting in this if conditional
if ($stmt->cond instanceof PhpParser\Node\Expr\BinaryOp
|| ($stmt->cond instanceof PhpParser\Node\Expr\BooleanNot
&& $stmt->cond->expr instanceof PhpParser\Node\Expr\BinaryOp)
) {
$final_actions = ScopeAnalyzer::getControlActions(
$stmt->stmts,
null,
$codebase->config->exit_functions,
2021-05-17 14:27:24 +02:00
[]
);
$has_leaving_statements = $final_actions === [ScopeAnalyzer::ACTION_END]
|| (count($final_actions) && !in_array(ScopeAnalyzer::ACTION_NONE, $final_actions, true));
if ($has_leaving_statements) {
$if_scope->post_leaving_if_context = clone $context;
}
}
2019-08-27 04:16:06 +02:00
try {
2020-11-07 03:01:17 +01:00
$if_conditional_scope = IfConditionalAnalyzer::analyze(
2019-08-27 04:16:06 +02:00
$statements_analyzer,
$stmt->cond,
2019-08-27 04:16:06 +02:00
$context,
$codebase,
$if_scope,
$context->branch_point ?: (int) $stmt->getAttribute('startFilePos')
2018-06-07 21:04:16 +02:00
);
2019-08-27 04:16:06 +02:00
$if_context = $if_conditional_scope->if_context;
2019-12-08 06:49:34 +01:00
$post_if_context = $if_conditional_scope->post_if_context;
2019-08-27 04:16:06 +02:00
$cond_referenced_var_ids = $if_conditional_scope->cond_referenced_var_ids;
2020-11-07 06:58:20 +01:00
$assigned_in_conditional_var_ids = $if_conditional_scope->assigned_in_conditional_var_ids;
2019-08-27 04:16:06 +02:00
} catch (\Psalm\Exception\ScopeAnalysisException $e) {
return false;
2016-10-22 19:23:18 +02:00
}
$mixed_var_ids = [];
foreach ($if_context->vars_in_scope as $var_id => $type) {
if ($type->isMixed() && isset($context->vars_in_scope[$var_id])) {
$mixed_var_ids[] = $var_id;
}
}
2020-08-26 21:35:29 +02:00
$cond_object_id = \spl_object_id($stmt->cond);
2020-11-03 22:15:44 +01:00
$if_clauses = FormulaGenerator::getFormula(
2020-08-26 21:35:29 +02:00
$cond_object_id,
$cond_object_id,
$stmt->cond,
2017-01-07 20:35:07 +01:00
$context->self,
2018-11-11 18:01:14 +01:00
$statements_analyzer,
2018-11-06 03:57:36 +01:00
$codebase
);
2016-10-22 19:23:18 +02:00
if (count($if_clauses) > 200) {
$if_clauses = [];
}
$if_clauses = array_values(
array_map(
/**
* @return Clause
*/
function (Clause $c) use ($mixed_var_ids, $cond_object_id): Clause {
$keys = array_keys($c->possibilities);
2019-11-12 16:21:27 +01:00
$mixed_var_ids = \array_diff($mixed_var_ids, $keys);
foreach ($keys as $key) {
foreach ($mixed_var_ids as $mixed_var_id) {
if (preg_match('/^' . preg_quote($mixed_var_id, '/') . '(\[|-)/', $key)) {
2020-08-26 21:35:29 +02:00
return new Clause([], $cond_object_id, $cond_object_id, true);
}
}
}
return $c;
},
$if_clauses
)
);
2019-12-08 06:49:34 +01:00
$entry_clauses = $context->clauses;
2017-04-02 21:26:10 +02:00
// this will see whether any of the clauses in set A conflict with the clauses in set B
2018-11-06 03:57:36 +01:00
AlgebraAnalyzer::checkForParadox(
$context->clauses,
$if_clauses,
2018-11-11 18:01:14 +01:00
$statements_analyzer,
$stmt->cond,
2020-11-07 06:58:20 +01:00
$assigned_in_conditional_var_ids
);
// if we have assignments in the if, we may have duplicate clauses
2020-11-07 06:58:20 +01:00
if ($assigned_in_conditional_var_ids) {
2018-05-07 07:26:06 +02:00
$if_clauses = Algebra::simplifyCNF($if_clauses);
}
2017-04-02 21:26:10 +02:00
2019-12-08 06:49:34 +01:00
$if_context_clauses = array_merge($entry_clauses, $if_clauses);
$if_context->clauses = Algebra::simplifyCNF($if_context_clauses);
if ($if_context->reconciled_expression_clauses) {
$reconciled_expression_clauses = $if_context->reconciled_expression_clauses;
$if_context->clauses = array_values(
array_filter(
$if_context->clauses,
function ($c) use ($reconciled_expression_clauses): bool {
return !in_array($c->hash, $reconciled_expression_clauses);
2019-12-08 06:49:34 +01:00
}
)
);
if (count($if_context->clauses) === 1
&& $if_context->clauses[0]->wedge
&& !$if_context->clauses[0]->possibilities
) {
$if_context->clauses = [];
$if_context->reconciled_expression_clauses = [];
}
2019-12-08 06:49:34 +01:00
}
2016-10-22 19:23:18 +02:00
2021-07-29 20:59:52 +02:00
// define this before we alter local clauses after reconciliation
$if_scope->reasonable_clauses = $if_context->clauses;
2019-01-08 15:57:14 +01:00
try {
$if_scope->negated_clauses = Algebra::negateFormula($if_clauses);
} catch (\Psalm\Exception\ComplicatedExpressionException $e) {
try {
2020-11-03 22:15:44 +01:00
$if_scope->negated_clauses = FormulaGenerator::getFormula(
$cond_object_id,
$cond_object_id,
new VirtualBooleanNot($stmt->cond),
$context->self,
$statements_analyzer,
$codebase,
false
);
} catch (\Psalm\Exception\ComplicatedExpressionException $e) {
$if_scope->negated_clauses = [];
}
2019-01-08 15:57:14 +01:00
}
2018-05-07 07:26:06 +02:00
$if_scope->negated_types = Algebra::getTruthsFromFormula(
Algebra::simplifyCNF(
2018-05-06 02:52:10 +02:00
array_merge($context->clauses, $if_scope->negated_clauses)
)
);
2016-10-22 19:23:18 +02:00
$active_if_types = [];
$reconcilable_if_types = Algebra::getTruthsFromFormula(
$if_context->clauses,
\spl_object_id($stmt->cond),
$cond_referenced_var_ids,
$active_if_types
);
2016-10-22 19:23:18 +02:00
if (array_filter(
$context->clauses,
function ($clause): bool {
2021-09-26 22:39:01 +02:00
return (bool)$clause->possibilities;
}
)) {
$omit_keys = array_reduce(
$context->clauses,
/**
* @param array<string> $carry
* @return array<string>
*/
function (array $carry, Clause $clause): array {
return array_merge($carry, array_keys($clause->possibilities));
},
[]
);
$omit_keys = array_combine($omit_keys, $omit_keys);
$omit_keys = array_diff_key($omit_keys, Algebra::getTruthsFromFormula($context->clauses));
$cond_referenced_var_ids = array_diff_key(
$cond_referenced_var_ids,
$omit_keys
);
}
2016-10-22 19:23:18 +02:00
// if the if has an || in the conditional, we cannot easily reason about it
if ($reconcilable_if_types) {
$changed_var_ids = [];
2016-10-22 19:23:18 +02:00
$if_vars_in_scope_reconciled =
Reconciler::reconcileKeyedTypes(
2016-10-22 19:23:18 +02:00
$reconcilable_if_types,
$active_if_types,
2016-10-22 19:23:18 +02:00
$if_context->vars_in_scope,
$changed_var_ids,
$cond_referenced_var_ids,
2018-11-11 18:01:14 +01:00
$statements_analyzer,
$statements_analyzer->getTemplateTypeMap() ?: [],
$if_context->inside_loop,
$context->check_variables
? new CodeLocation(
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSource(),
$stmt->cond instanceof PhpParser\Node\Expr\BooleanNot
? $stmt->cond->expr
: $stmt->cond,
$context->include_location
) : null
2016-10-22 19:23:18 +02:00
);
$if_context->vars_in_scope = $if_vars_in_scope_reconciled;
foreach ($reconcilable_if_types as $var_id => $_) {
2017-12-02 19:32:20 +01:00
$if_context->vars_possibly_in_scope[$var_id] = true;
}
if ($changed_var_ids) {
2019-12-08 06:49:34 +01:00
$if_context->clauses = Context::removeReconciledClauses($if_context->clauses, $changed_var_ids)[0];
2021-02-09 05:31:49 +01:00
foreach ($changed_var_ids as $changed_var_id => $_) {
foreach ($if_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)
2021-02-09 06:53:09 +01:00
&& !\array_key_exists($var_id, $cond_referenced_var_ids)
2021-02-09 05:31:49 +01:00
) {
unset($if_context->vars_in_scope[$var_id]);
}
}
}
}
2018-01-20 17:48:16 +01:00
$if_scope->if_cond_changed_var_ids = $changed_var_ids;
2016-10-22 19:23:18 +02:00
}
2020-11-03 22:44:24 +01:00
$if_context->reconciled_expression_clauses = [];
2016-10-22 19:23:18 +02:00
$old_if_context = clone $if_context;
2016-11-02 07:29:00 +01:00
$context->vars_possibly_in_scope = array_merge(
$if_context->vars_possibly_in_scope,
$context->vars_possibly_in_scope
);
2016-10-22 19:23:18 +02:00
$context->referenced_var_ids = array_merge(
$if_context->referenced_var_ids,
$context->referenced_var_ids
);
$temp_else_context = clone $post_if_context;
2016-10-22 19:23:18 +02:00
$changed_var_ids = [];
if ($if_scope->negated_types) {
$else_vars_reconciled = Reconciler::reconcileKeyedTypes(
$if_scope->negated_types,
[],
$temp_else_context->vars_in_scope,
$changed_var_ids,
[],
2018-11-11 18:01:14 +01:00
$statements_analyzer,
$statements_analyzer->getTemplateTypeMap() ?: [],
$context->inside_loop,
$context->check_variables
? new CodeLocation(
2018-11-11 18:01:14 +01:00
$statements_analyzer->getSource(),
$stmt->cond instanceof PhpParser\Node\Expr\BooleanNot
? $stmt->cond->expr
: $stmt->cond,
$context->include_location
) : null
2016-10-22 19:23:18 +02:00
);
$temp_else_context->vars_in_scope = $else_vars_reconciled;
2016-10-22 19:23:18 +02:00
}
// we calculate the vars redefined in a hypothetical else statement to determine
// which vars of the if we can safely change
$pre_assignment_else_redefined_vars = array_intersect_key(
$temp_else_context->getRedefinedVars($context->vars_in_scope, true),
$changed_var_ids
);
// check the if
2020-11-07 03:01:17 +01:00
if (IfElse\IfAnalyzer::analyze(
2018-11-11 18:01:14 +01:00
$statements_analyzer,
$stmt,
$if_scope,
$if_conditional_scope,
$if_context,
$old_if_context,
$context,
$pre_assignment_else_redefined_vars
) === false) {
return false;
}
2016-10-22 19:23:18 +02:00
2021-04-19 05:28:34 +02:00
// this has to go on a separate line because the phar compactor messes with precedence
$scope_to_clone = $if_scope->post_leaving_if_context ?? $post_if_context;
$else_context = clone $scope_to_clone;
2019-08-27 04:16:06 +02:00
// check the elseifs
foreach ($stmt->elseifs as $elseif) {
2020-11-07 03:01:17 +01:00
if (IfElse\ElseIfAnalyzer::analyze(
2018-11-11 18:01:14 +01:00
$statements_analyzer,
$elseif,
$if_scope,
2019-08-27 04:16:06 +02:00
$else_context,
2018-11-06 03:57:36 +01:00
$context,
2019-08-27 04:16:06 +02:00
$codebase,
$else_context->branch_point ?: (int) $stmt->getAttribute('startFilePos')
) === false) {
return false;
}
2016-10-22 19:23:18 +02:00
}
if ($stmt->else) {
2018-11-06 03:57:36 +01:00
if ($codebase->alter_code) {
$else_context->branch_point =
$else_context->branch_point ?: (int) $stmt->getAttribute('startFilePos');
}
}
2020-11-07 03:01:17 +01:00
if (IfElse\ElseAnalyzer::analyze(
2018-11-11 18:01:14 +01:00
$statements_analyzer,
$stmt->else,
$if_scope,
$else_context,
$context
) === false) {
return false;
}
if ($context->loop_scope) {
$context->loop_scope->final_actions = array_unique(
2017-12-03 00:28:18 +01:00
array_merge(
$context->loop_scope->final_actions,
2017-12-03 00:28:18 +01:00
$if_scope->final_actions
)
);
}
2016-10-22 19:23:18 +02:00
$context->vars_possibly_in_scope = array_merge(
$context->vars_possibly_in_scope,
$if_scope->new_vars_possibly_in_scope
);
2016-10-22 19:23:18 +02:00
$context->possibly_assigned_var_ids = array_merge(
$context->possibly_assigned_var_ids,
$if_scope->possibly_assigned_var_ids ?: []
);
// vars can only be defined/redefined if there was an else (defined in every block)
$context->assigned_var_ids = array_merge(
$context->assigned_var_ids,
$if_scope->assigned_var_ids ?: []
);
if ($if_scope->new_vars) {
foreach ($if_scope->new_vars as $var_id => $type) {
if (isset($context->vars_possibly_in_scope[$var_id])
&& $statements_analyzer->data_flow_graph
) {
$type->parent_nodes += $statements_analyzer->getParentNodesForPossiblyUndefinedVariable($var_id);
}
$context->vars_in_scope[$var_id] = $type;
}
}
2016-10-22 19:23:18 +02:00
if ($if_scope->redefined_vars) {
foreach ($if_scope->redefined_vars as $var_id => $type) {
$context->vars_in_scope[$var_id] = $type;
$if_scope->updated_vars[$var_id] = true;
2016-10-22 19:23:18 +02:00
if ($if_scope->reasonable_clauses) {
$if_scope->reasonable_clauses = Context::filterClauses(
$var_id,
$if_scope->reasonable_clauses,
2021-09-26 22:06:24 +02:00
$context->vars_in_scope[$var_id] ?? null,
2018-11-11 18:01:14 +01:00
$statements_analyzer
);
}
}
}
2018-05-14 22:29:51 +02:00
if ($if_scope->possible_param_types) {
foreach ($if_scope->possible_param_types as $var => $type) {
$context->possible_param_types[$var] = $type;
2017-12-03 00:28:18 +01:00
}
}
2016-10-22 19:23:18 +02:00
if ($if_scope->reasonable_clauses
&& (count($if_scope->reasonable_clauses) > 1 || !$if_scope->reasonable_clauses[0]->wedge)
) {
$context->clauses = Algebra::simplifyCNF(
array_merge(
$if_scope->reasonable_clauses,
$context->clauses
)
);
}
if ($if_scope->possibly_redefined_vars) {
foreach ($if_scope->possibly_redefined_vars as $var_id => $type) {
if (isset($context->vars_in_scope[$var_id])) {
if (!$type->failed_reconciliation
&& !isset($if_scope->updated_vars[$var_id])
) {
$combined_type = Type::combineUnionTypes(
$context->vars_in_scope[$var_id],
$type,
$codebase
);
2018-05-14 22:29:51 +02:00
if (!$combined_type->equals($context->vars_in_scope[$var_id])) {
$context->removeDescendents($var_id, $combined_type);
}
2016-10-22 19:23:18 +02:00
$context->vars_in_scope[$var_id] = $combined_type;
2018-06-17 02:01:33 +02:00
} else {
$context->vars_in_scope[$var_id]->parent_nodes += $type->parent_nodes;
2018-06-17 02:01:33 +02:00
}
}
}
}
$context->possibly_assigned_var_ids += $if_scope->possibly_assigned_var_ids;
if (!in_array(ScopeAnalyzer::ACTION_NONE, $if_scope->final_actions, true)) {
$context->has_returned = true;
}
return null;
}
2016-10-22 19:23:18 +02:00
}