1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 09:37:59 +01:00
This commit is contained in:
Daniil Gentili 2023-10-21 14:45:30 +02:00
parent d0f832f5f6
commit a36d2fcb84
13 changed files with 113 additions and 28 deletions

View File

@ -799,7 +799,10 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
}
if ($this->return_vars_possibly_in_scope !== null) {
$context->vars_possibly_in_scope = [...$context->vars_possibly_in_scope, ...$this->return_vars_possibly_in_scope];
$context->vars_possibly_in_scope = [
...$context->vars_possibly_in_scope,
...$this->return_vars_possibly_in_scope,
];
}
foreach ($context->vars_in_scope as $var => $_) {
@ -1537,7 +1540,10 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
}
if ($this->return_vars_possibly_in_scope !== null) {
$this->return_vars_possibly_in_scope = [...$context->vars_possibly_in_scope, ...$this->return_vars_possibly_in_scope];
$this->return_vars_possibly_in_scope = [
...$context->vars_possibly_in_scope,
...$this->return_vars_possibly_in_scope,
];
} else {
$this->return_vars_possibly_in_scope = $context->vars_possibly_in_scope;
}

View File

@ -157,7 +157,10 @@ final class DoAnalyzer
$do_context->loop_scope = null;
$context->vars_possibly_in_scope = [...$context->vars_possibly_in_scope, ...$do_context->vars_possibly_in_scope];
$context->vars_possibly_in_scope = [
...$context->vars_possibly_in_scope,
...$do_context->vars_possibly_in_scope,
];
if ($context->collect_exceptions) {
$context->mergeExceptions($inner_loop_context);

View File

@ -385,7 +385,10 @@ final class ForeachAnalyzer
$foreach_context->loop_scope = null;
$context->vars_possibly_in_scope = [...$foreach_context->vars_possibly_in_scope, ...$context->vars_possibly_in_scope];
$context->vars_possibly_in_scope = [
...$foreach_context->vars_possibly_in_scope,
...$context->vars_possibly_in_scope,
];
if ($context->collect_exceptions) {
$context->mergeExceptions($foreach_context);

View File

@ -200,13 +200,22 @@ final class ElseAnalyzer
if ($has_leaving_statements) {
if ($else_context->loop_scope) {
if (!$has_continue_statement && !$has_break_statement) {
$if_scope->new_vars_possibly_in_scope = [...$vars_possibly_in_scope, ...$if_scope->new_vars_possibly_in_scope];
$if_scope->new_vars_possibly_in_scope = [
...$vars_possibly_in_scope,
...$if_scope->new_vars_possibly_in_scope,
];
}
$else_context->loop_scope->vars_possibly_in_scope = [...$vars_possibly_in_scope, ...$else_context->loop_scope->vars_possibly_in_scope];
$else_context->loop_scope->vars_possibly_in_scope = [
...$vars_possibly_in_scope,
...$else_context->loop_scope->vars_possibly_in_scope,
];
}
} else {
$if_scope->new_vars_possibly_in_scope = [...$vars_possibly_in_scope, ...$if_scope->new_vars_possibly_in_scope];
$if_scope->new_vars_possibly_in_scope = [
...$vars_possibly_in_scope,
...$if_scope->new_vars_possibly_in_scope,
];
$if_scope->possibly_assigned_var_ids = array_merge(
$possibly_assigned_var_ids,

View File

@ -373,16 +373,25 @@ final class ElseIfAnalyzer
if ($has_leaving_statements && $elseif_context->loop_scope) {
if (!$has_continue_statement && !$has_break_statement) {
$if_scope->new_vars_possibly_in_scope = [...$vars_possibly_in_scope, ...$if_scope->new_vars_possibly_in_scope];
$if_scope->new_vars_possibly_in_scope = [
...$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,
);
}
$elseif_context->loop_scope->vars_possibly_in_scope = [...$vars_possibly_in_scope, ...$elseif_context->loop_scope->vars_possibly_in_scope];
$elseif_context->loop_scope->vars_possibly_in_scope = [
...$vars_possibly_in_scope,
...$elseif_context->loop_scope->vars_possibly_in_scope,
];
} elseif (!$has_leaving_statements) {
$if_scope->new_vars_possibly_in_scope = [...$vars_possibly_in_scope, ...$if_scope->new_vars_possibly_in_scope];
$if_scope->new_vars_possibly_in_scope = [
...$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,

View File

@ -146,7 +146,10 @@ final class IfAnalyzer
$if_context->reconciled_expression_clauses = [];
$outer_context->vars_possibly_in_scope = [...$if_context->vars_possibly_in_scope, ...$outer_context->vars_possibly_in_scope];
$outer_context->vars_possibly_in_scope = [
...$if_context->vars_possibly_in_scope,
...$outer_context->vars_possibly_in_scope,
];
$old_if_context = clone $if_context;
@ -305,7 +308,10 @@ final class IfAnalyzer
$if_scope->new_vars_possibly_in_scope = $vars_possibly_in_scope;
}
$if_context->loop_scope->vars_possibly_in_scope = [...$vars_possibly_in_scope, ...$if_context->loop_scope->vars_possibly_in_scope];
$if_context->loop_scope->vars_possibly_in_scope = [
...$vars_possibly_in_scope,
...$if_context->loop_scope->vars_possibly_in_scope,
];
} elseif (!$has_leaving_statements) {
$if_scope->new_vars_possibly_in_scope = $vars_possibly_in_scope;
}

View File

@ -363,9 +363,15 @@ final class IfElseAnalyzer
);
}
$context->vars_possibly_in_scope = [...$context->vars_possibly_in_scope, ...$if_scope->new_vars_possibly_in_scope];
$context->vars_possibly_in_scope = [
...$context->vars_possibly_in_scope,
...$if_scope->new_vars_possibly_in_scope,
];
$context->possibly_assigned_var_ids = [...$context->possibly_assigned_var_ids, ...$if_scope->possibly_assigned_var_ids ?: []];
$context->possibly_assigned_var_ids = [
...$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(

View File

@ -139,7 +139,10 @@ final class LoopAnalyzer
}
}
$loop_parent_context->vars_possibly_in_scope = [...$continue_context->vars_possibly_in_scope, ...$loop_parent_context->vars_possibly_in_scope];
$loop_parent_context->vars_possibly_in_scope = [
...$continue_context->vars_possibly_in_scope,
...$loop_parent_context->vars_possibly_in_scope,
];
} else {
$original_parent_context = clone $loop_parent_context;
@ -267,7 +270,10 @@ final class LoopAnalyzer
$continue_context->has_returned = false;
$loop_parent_context->vars_possibly_in_scope = [...$continue_context->vars_possibly_in_scope, ...$loop_parent_context->vars_possibly_in_scope];
$loop_parent_context->vars_possibly_in_scope = [
...$continue_context->vars_possibly_in_scope,
...$loop_parent_context->vars_possibly_in_scope,
];
// if there are no changes to the types, no need to re-examine
if (!$has_changes) {
@ -547,7 +553,10 @@ final class LoopAnalyzer
}
// merge vars possibly in scope at the end of each loop
$loop_context->vars_possibly_in_scope = [...$loop_context->vars_possibly_in_scope, ...$loop_scope->vars_possibly_in_scope];
$loop_context->vars_possibly_in_scope = [
...$loop_context->vars_possibly_in_scope,
...$loop_scope->vars_possibly_in_scope,
];
}
/**

View File

@ -219,7 +219,10 @@ final class SwitchAnalyzer
$context->assigned_var_ids += $switch_scope->new_assigned_var_ids;
}
$context->vars_possibly_in_scope = [...$context->vars_possibly_in_scope, ...$switch_scope->new_vars_possibly_in_scope];
$context->vars_possibly_in_scope = [
...$context->vars_possibly_in_scope,
...$switch_scope->new_vars_possibly_in_scope,
];
//a switch can't return in all options without a default
$context->has_returned = $all_options_returned && $has_default;

View File

@ -103,7 +103,10 @@ final class WhileAnalyzer
$while_context->loop_scope = null;
if ($can_leave_loop) {
$context->vars_possibly_in_scope = [...$context->vars_possibly_in_scope, ...$while_context->vars_possibly_in_scope];
$context->vars_possibly_in_scope = [
...$context->vars_possibly_in_scope,
...$while_context->vars_possibly_in_scope,
];
} elseif ($pre_context) {
$context->vars_possibly_in_scope = $pre_context->vars_possibly_in_scope;
}

View File

@ -188,11 +188,20 @@ final class AndAnalyzer
if ($context->if_body_context && !$context->inside_negation) {
$if_body_context = $context->if_body_context;
$context->vars_in_scope = $right_context->vars_in_scope;
$if_body_context->vars_in_scope = [...$if_body_context->vars_in_scope, ...$context->vars_in_scope];
$if_body_context->vars_in_scope = [
...$if_body_context->vars_in_scope,
...$context->vars_in_scope,
];
$if_body_context->cond_referenced_var_ids = [...$if_body_context->cond_referenced_var_ids, ...$context->cond_referenced_var_ids];
$if_body_context->cond_referenced_var_ids = [
...$if_body_context->cond_referenced_var_ids,
...$context->cond_referenced_var_ids,
];
$if_body_context->assigned_var_ids = [...$if_body_context->assigned_var_ids, ...$context->assigned_var_ids];
$if_body_context->assigned_var_ids = [
...$if_body_context->assigned_var_ids,
...$context->assigned_var_ids,
];
$if_body_context->reconciled_expression_clauses = [
...$if_body_context->reconciled_expression_clauses,
@ -203,7 +212,10 @@ final class AndAnalyzer
),
];
$if_body_context->vars_possibly_in_scope = [...$if_body_context->vars_possibly_in_scope, ...$context->vars_possibly_in_scope];
$if_body_context->vars_possibly_in_scope = [
...$if_body_context->vars_possibly_in_scope,
...$context->vars_possibly_in_scope,
];
$if_body_context->updateChecks($context);
} else {

View File

@ -132,10 +132,16 @@ final class OrAnalyzer
}
$left_referenced_var_ids = $left_context->cond_referenced_var_ids;
$left_context->cond_referenced_var_ids = [...$pre_referenced_var_ids, ...$left_referenced_var_ids];
$left_context->cond_referenced_var_ids = [
...$pre_referenced_var_ids,
...$left_referenced_var_ids,
];
$left_assigned_var_ids = array_diff_key($left_context->assigned_var_ids, $pre_assigned_var_ids);
$left_context->assigned_var_ids = [...$pre_assigned_var_ids, ...$left_context->assigned_var_ids];
$left_context->assigned_var_ids = [
...$pre_assigned_var_ids,
...$left_context->assigned_var_ids,
];
$left_referenced_var_ids = array_diff_key($left_referenced_var_ids, $left_assigned_var_ids);
}

View File

@ -210,7 +210,10 @@ final class TernaryAnalyzer
return false;
}
$context->cond_referenced_var_ids = [...$context->cond_referenced_var_ids, ...$if_context->cond_referenced_var_ids];
$context->cond_referenced_var_ids = [
...$context->cond_referenced_var_ids,
...$if_context->cond_referenced_var_ids,
];
}
$t_else_context->clauses = Algebra::simplifyCNF(
@ -286,9 +289,16 @@ final class TernaryAnalyzer
}
}
$context->vars_possibly_in_scope = [...$context->vars_possibly_in_scope, ...$if_context->vars_possibly_in_scope, ...$t_else_context->vars_possibly_in_scope];
$context->vars_possibly_in_scope = [
...$context->vars_possibly_in_scope,
...$if_context->vars_possibly_in_scope,
...$t_else_context->vars_possibly_in_scope,
];
$context->cond_referenced_var_ids = [...$context->cond_referenced_var_ids, ...$t_else_context->cond_referenced_var_ids];
$context->cond_referenced_var_ids = [
...$context->cond_referenced_var_ids,
...$t_else_context->cond_referenced_var_ids,
];
$lhs_type = null;
$stmt_cond_type = $statements_analyzer->node_data->getType($stmt->cond);