From a9f4148db234af421cc69e04337bec645b008c0d Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 15 Mar 2022 18:40:31 -0400 Subject: [PATCH] Rename referenced_var_ids to cond_referenced_var_ids MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also reduce usage — this was a holdover of the old way of tracking unused variables --- src/Psalm/Context.php | 8 +-- .../Analyzer/Statements/Block/DoAnalyzer.php | 6 -- .../Analyzer/Statements/Block/ForAnalyzer.php | 6 -- .../Statements/Block/ForeachAnalyzer.php | 6 -- .../Block/IfConditionalAnalyzer.php | 14 ++-- .../Statements/Block/IfElse/IfAnalyzer.php | 5 -- .../Statements/Block/LoopAnalyzer.php | 21 ++---- .../Statements/Block/SwitchCaseAnalyzer.php | 5 -- .../Analyzer/Statements/Block/TryAnalyzer.php | 68 ++++++------------- .../Statements/Block/WhileAnalyzer.php | 6 -- .../Expression/AssignmentAnalyzer.php | 2 +- .../Expression/BinaryOp/AndAnalyzer.php | 20 +++--- .../Expression/BinaryOp/OrAnalyzer.php | 28 ++++---- .../Statements/Expression/TernaryAnalyzer.php | 12 ++-- 14 files changed, 66 insertions(+), 141 deletions(-) diff --git a/src/Psalm/Context.php b/src/Psalm/Context.php index e501a4eee95..7c31e352cf9 100644 --- a/src/Psalm/Context.php +++ b/src/Psalm/Context.php @@ -272,11 +272,11 @@ final class Context public $collect_exceptions = false; /** - * A list of variables that have been referenced + * A list of variables that have been referenced in conditionals * * @var array */ - public $referenced_var_ids = []; + public $cond_referenced_var_ids = []; /** * A list of variables that have been passed by reference (where we know their type) @@ -616,7 +616,7 @@ public function removePossibleReference(string $remove_var_id): void } unset( $this->vars_in_scope[$remove_var_id], - $this->referenced_var_ids[$remove_var_id], + $this->cond_referenced_var_ids[$remove_var_id], $this->referenced_counts[$remove_var_id], $this->references_in_scope[$remove_var_id], $this->references_to_external_scope[$remove_var_id], @@ -836,7 +836,7 @@ public function hasVariable(string $var_name): bool $stripped_var = preg_replace('/(->|\[).*$/', '', $var_name); if ($stripped_var !== '$this' || $var_name !== $stripped_var) { - $this->referenced_var_ids[$var_name] = true; + $this->cond_referenced_var_ids[$var_name] = true; } return isset($this->vars_in_scope[$var_name]); diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php index 2298b6cd41f..47226c94c06 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php @@ -17,7 +17,6 @@ use function array_diff; use function array_filter; -use function array_intersect_key; use function array_keys; use function array_merge; use function array_values; @@ -162,11 +161,6 @@ function (Clause $c) use ($mixed_var_ids): bool { $do_context->vars_possibly_in_scope ); - $context->referenced_var_ids = array_intersect_key( - $do_context->referenced_var_ids, - $context->referenced_var_ids - ); - if ($context->collect_exceptions) { $context->mergeExceptions($inner_loop_context); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/ForAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/ForAnalyzer.php index c45ca9b6d0f..c1293774b17 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/ForAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/ForAnalyzer.php @@ -11,7 +11,6 @@ use Psalm\Type; use UnexpectedValueException; -use function array_intersect_key; use function array_merge; use function count; use function in_array; @@ -176,11 +175,6 @@ public static function analyze( $context->vars_possibly_in_scope = $pre_context->vars_possibly_in_scope; } - $context->referenced_var_ids = array_intersect_key( - $for_context->referenced_var_ids, - $context->referenced_var_ids - ); - if ($context->collect_exceptions) { $context->mergeExceptions($for_context); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php index 098e03ce48a..7b950335d8e 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php @@ -60,7 +60,6 @@ use Psalm\Type\Union; use UnexpectedValueException; -use function array_intersect_key; use function array_keys; use function array_map; use function array_merge; @@ -378,11 +377,6 @@ public static function analyze( $context->vars_possibly_in_scope ); - $context->referenced_var_ids = array_intersect_key( - $foreach_context->referenced_var_ids, - $context->referenced_var_ids - ); - if ($context->collect_exceptions) { $context->mergeExceptions($foreach_context); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfConditionalAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfConditionalAnalyzer.php index 8601b0c261e..e1b8be725f1 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfConditionalAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfConditionalAnalyzer.php @@ -98,8 +98,8 @@ public static function analyze( $pre_condition_vars_in_scope = $outer_context->vars_in_scope; - $referenced_var_ids = $outer_context->referenced_var_ids; - $outer_context->referenced_var_ids = []; + $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 = []; @@ -128,8 +128,8 @@ public static function analyze( $first_cond_assigned_var_ids ); - $first_cond_referenced_var_ids = $outer_context->referenced_var_ids; - $outer_context->referenced_var_ids = array_merge( + $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 ); @@ -161,7 +161,7 @@ public static function analyze( $if_conditional_context->assigned_var_ids = []; $referenced_var_ids = $first_cond_referenced_var_ids; - $if_conditional_context->referenced_var_ids = []; + $if_conditional_context->cond_referenced_var_ids = []; $was_inside_conditional = $if_conditional_context->inside_conditional; @@ -174,8 +174,8 @@ public static function analyze( $if_conditional_context->inside_conditional = $was_inside_conditional; /** @var array */ - $more_cond_referenced_var_ids = $if_conditional_context->referenced_var_ids; - $if_conditional_context->referenced_var_ids = array_merge( + $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 ); diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php index 095b885209f..fccf832d839 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php @@ -152,11 +152,6 @@ public static function analyze( $outer_context->vars_possibly_in_scope ); - $outer_context->referenced_var_ids = array_merge( - $if_context->referenced_var_ids, - $outer_context->referenced_var_ids - ); - $old_if_context = clone $if_context; $codebase = $statements_analyzer->getCodebase(); diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php index e0eb239cf78..15be5e47d25 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php @@ -19,7 +19,6 @@ use Psalm\Type; use Psalm\Type\Reconciler; -use function array_intersect_key; use function array_keys; use function array_merge; use function array_unique; @@ -108,8 +107,6 @@ public static function analyze( $assignment_depth = self::getAssignmentMapDepth($first_var_id, $assignment_map); } - $pre_outer_context = $loop_parent_context; - if ($assignment_depth === 0 || $does_always_break) { $continue_context = clone $loop_context; @@ -389,8 +386,6 @@ public static function analyze( IssueBuffer::bubbleUp($recorded_issue); } } - - $pre_outer_context = $original_parent_context; } $does_sometimes_break = in_array(ScopeAnalyzer::ACTION_BREAK, $loop_scope->final_actions, true); @@ -499,14 +494,6 @@ public static function analyze( } } - $loop_context->referenced_var_ids = array_merge( - array_intersect_key( - $continue_context->referenced_var_ids, - $pre_outer_context->vars_in_scope - ), - $loop_context->referenced_var_ids - ); - if ($always_enters_loop) { foreach ($continue_context->vars_in_scope as $var_id => $type) { // if there are break statements in the loop it's not certain @@ -590,8 +577,8 @@ private static function applyPreConditionToLoopContext( Context $outer_context, bool $is_do ): array { - $pre_referenced_var_ids = $loop_context->referenced_var_ids; - $loop_context->referenced_var_ids = []; + $pre_referenced_var_ids = $loop_context->cond_referenced_var_ids; + $loop_context->cond_referenced_var_ids = []; $was_inside_conditional = $loop_context->inside_conditional; @@ -605,8 +592,8 @@ private static function applyPreConditionToLoopContext( $loop_context->inside_conditional = $was_inside_conditional; - $new_referenced_var_ids = $loop_context->referenced_var_ids; - $loop_context->referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids); + $new_referenced_var_ids = $loop_context->cond_referenced_var_ids; + $loop_context->cond_referenced_var_ids = array_merge($pre_referenced_var_ids, $new_referenced_var_ids); $always_assigned_before_loop_body_vars = Context::getNewOrUpdatedVarIds($outer_context, $loop_context); diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php index 3b0e86a3699..c9198328c0c 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/SwitchCaseAnalyzer.php @@ -478,11 +478,6 @@ public static function analyze( $statements_analyzer->node_data = $old_node_data; - $context->referenced_var_ids = array_merge( - $context->referenced_var_ids, - $case_context->referenced_var_ids - ); - if ($case_exit_type !== 'return_throw') { if (self::handleNonReturningCase( $statements_analyzer, diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php index 70c2db92b3c..940329162d3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php @@ -66,25 +66,19 @@ public static function analyze( $old_context = clone $context; - if ($all_catches_leave && !$stmt->finally) { - $try_context = $context; - } else { - $try_context = clone $context; + $try_context = clone $context; - if ($codebase->alter_code) { - $try_context->branch_point = $try_context->branch_point ?: (int) $stmt->getAttribute('startFilePos'); - } + if ($codebase->alter_code) { + $try_context->branch_point = $try_context->branch_point ?: (int) $stmt->getAttribute('startFilePos'); + } - if ($stmt->finally) { - $try_context->finally_scope = new FinallyScope($try_context->vars_in_scope); - } + if ($stmt->finally) { + $try_context->finally_scope = new FinallyScope($try_context->vars_in_scope); } $assigned_var_ids = $try_context->assigned_var_ids; $context->assigned_var_ids = []; - $old_referenced_var_ids = $try_context->referenced_var_ids; - $was_inside_try = $context->inside_try; $context->inside_try = true; if ($statements_analyzer->analyze($stmt->stmts, $context) === false) { @@ -118,35 +112,23 @@ public static function analyze( $newly_assigned_var_ids ); - $possibly_referenced_var_ids = array_merge( - $context->referenced_var_ids, - $old_referenced_var_ids - ); - - if ($try_context !== $context) { - foreach ($context->vars_in_scope as $var_id => $type) { - if (!isset($try_context->vars_in_scope[$var_id])) { - $try_context->vars_in_scope[$var_id] = clone $type; + foreach ($context->vars_in_scope as $var_id => $type) { + if (!isset($try_context->vars_in_scope[$var_id])) { + $try_context->vars_in_scope[$var_id] = clone $type; - $context->vars_in_scope[$var_id]->possibly_undefined = true; - $context->vars_in_scope[$var_id]->possibly_undefined_from_try = true; - } else { - $try_context->vars_in_scope[$var_id] = Type::combineUnionTypes( - $try_context->vars_in_scope[$var_id], - $type - ); - } + $context->vars_in_scope[$var_id]->possibly_undefined = true; + $context->vars_in_scope[$var_id]->possibly_undefined_from_try = true; + } else { + $try_context->vars_in_scope[$var_id] = Type::combineUnionTypes( + $try_context->vars_in_scope[$var_id], + $type + ); } - - $try_context->vars_possibly_in_scope = $context->vars_possibly_in_scope; - $try_context->possibly_thrown_exceptions = $context->possibly_thrown_exceptions; - - $context->referenced_var_ids = array_intersect_key( - $try_context->referenced_var_ids, - $context->referenced_var_ids - ); } + $try_context->vars_possibly_in_scope = $context->vars_possibly_in_scope; + $try_context->possibly_thrown_exceptions = $context->possibly_thrown_exceptions; + $try_leaves_loop = $context->loop_scope && $context->loop_scope->final_actions && !in_array(ScopeAnalyzer::ACTION_NONE, $context->loop_scope->final_actions, true); @@ -350,7 +332,7 @@ function (string $fq_catch_class) use ($codebase): TNamedObject { } } - $old_catch_assigned_var_ids = $catch_context->referenced_var_ids; + $old_catch_assigned_var_ids = $catch_context->assigned_var_ids; $catch_context->assigned_var_ids = []; @@ -374,16 +356,6 @@ function (string $fq_catch_class) use ($codebase): TNamedObject { $catch_context->assigned_var_ids += $old_catch_assigned_var_ids; - $context->referenced_var_ids = array_intersect_key( - $catch_context->referenced_var_ids, - $context->referenced_var_ids - ); - - $possibly_referenced_var_ids = array_merge( - $catch_context->referenced_var_ids, - $possibly_referenced_var_ids - ); - if ($catch_context->collect_exceptions) { $context->mergeExceptions($catch_context); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/WhileAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/WhileAnalyzer.php index a6ef17dfe4f..1d8716e7143 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/WhileAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/WhileAnalyzer.php @@ -10,7 +10,6 @@ use Psalm\Type; use UnexpectedValueException; -use function array_intersect_key; use function array_merge; use function in_array; @@ -109,11 +108,6 @@ public static function analyze( $context->vars_possibly_in_scope = $pre_context->vars_possibly_in_scope; } - $context->referenced_var_ids = array_intersect_key( - $while_context->referenced_var_ids, - $context->referenced_var_ids - ); - if ($context->collect_exceptions) { $context->mergeExceptions($while_context); } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index 8880176a061..9ecfcabbea7 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -210,7 +210,7 @@ public static function analyze( } if ($extended_var_id) { - unset($context->referenced_var_ids[$extended_var_id]); + unset($context->cond_referenced_var_ids[$extended_var_id]); $context->assigned_var_ids[$extended_var_id] = (int) $assign_var->getAttribute('startFilePos'); $context->possibly_assigned_var_ids[$extended_var_id] = true; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/AndAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/AndAnalyzer.php index 793a1ea162e..b52ca677626 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/AndAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/AndAnalyzer.php @@ -51,13 +51,13 @@ public static function analyze( return IfElseAnalyzer::analyze($statements_analyzer, $fake_if_stmt, $context) !== false; } - $pre_referenced_var_ids = $context->referenced_var_ids; + $pre_referenced_var_ids = $context->cond_referenced_var_ids; $pre_assigned_var_ids = $context->assigned_var_ids; $left_context = clone $context; - $left_context->referenced_var_ids = []; + $left_context->cond_referenced_var_ids = []; $left_context->assigned_var_ids = []; /** @var list $left_context->reconciled_expression_clauses */ @@ -89,8 +89,8 @@ public static function analyze( } /** @var array */ - $left_referenced_var_ids = $left_context->referenced_var_ids; - $context->referenced_var_ids = array_merge($pre_referenced_var_ids, $left_referenced_var_ids); + $left_referenced_var_ids = $left_context->cond_referenced_var_ids; + $context->cond_referenced_var_ids = array_merge($pre_referenced_var_ids, $left_referenced_var_ids); $left_assigned_var_ids = array_diff_key($left_context->assigned_var_ids, $pre_assigned_var_ids); @@ -161,9 +161,9 @@ public static function analyze( IfConditionalAnalyzer::handleParadoxicalCondition($statements_analyzer, $stmt->right); - $context->referenced_var_ids = array_merge( - $right_context->referenced_var_ids, - $left_context->referenced_var_ids + $context->cond_referenced_var_ids = array_merge( + $right_context->cond_referenced_var_ids, + $left_context->cond_referenced_var_ids ); if ($context->inside_conditional) { @@ -188,9 +188,9 @@ public static function analyze( $context->vars_in_scope ); - $if_body_context->referenced_var_ids = array_merge( - $if_body_context->referenced_var_ids, - $context->referenced_var_ids, + $if_body_context->cond_referenced_var_ids = array_merge( + $if_body_context->cond_referenced_var_ids, + $context->cond_referenced_var_ids, ); $if_body_context->assigned_var_ids = array_merge( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/OrAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/OrAnalyzer.php index 78fa57971a6..c4cb437a444 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/OrAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/OrAnalyzer.php @@ -94,8 +94,8 @@ public static function analyze( return false; } } else { - $pre_referenced_var_ids = $context->referenced_var_ids; - $context->referenced_var_ids = []; + $pre_referenced_var_ids = $context->cond_referenced_var_ids; + $context->cond_referenced_var_ids = []; $pre_assigned_var_ids = $context->assigned_var_ids; @@ -129,8 +129,8 @@ public static function analyze( } } - $left_referenced_var_ids = $left_context->referenced_var_ids; - $left_context->referenced_var_ids = array_merge($pre_referenced_var_ids, $left_referenced_var_ids); + $left_referenced_var_ids = $left_context->cond_referenced_var_ids; + $left_context->cond_referenced_var_ids = array_merge($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 = array_merge($pre_assigned_var_ids, $left_context->assigned_var_ids); @@ -263,8 +263,8 @@ public static function analyze( $right_context->if_body_context = null; - $pre_referenced_var_ids = $right_context->referenced_var_ids; - $right_context->referenced_var_ids = []; + $pre_referenced_var_ids = $right_context->cond_referenced_var_ids; + $right_context->cond_referenced_var_ids = []; $pre_assigned_var_ids = $right_context->assigned_var_ids; $right_context->assigned_var_ids = []; @@ -275,8 +275,8 @@ public static function analyze( IfConditionalAnalyzer::handleParadoxicalCondition($statements_analyzer, $stmt->right); - $right_referenced_var_ids = $right_context->referenced_var_ids; - $right_context->referenced_var_ids = array_merge($pre_referenced_var_ids, $right_referenced_var_ids); + $right_referenced_var_ids = $right_context->cond_referenced_var_ids; + $right_context->cond_referenced_var_ids = array_merge($pre_referenced_var_ids, $right_referenced_var_ids); $right_assigned_var_ids = $right_context->assigned_var_ids; $right_context->assigned_var_ids = array_merge($pre_assigned_var_ids, $right_assigned_var_ids); @@ -361,9 +361,9 @@ public static function analyze( $context->updateChecks($right_context); } - $context->referenced_var_ids = array_merge( - $right_context->referenced_var_ids, - $context->referenced_var_ids + $context->cond_referenced_var_ids = array_merge( + $right_context->cond_referenced_var_ids, + $context->cond_referenced_var_ids ); $context->assigned_var_ids = array_merge( @@ -390,9 +390,9 @@ public static function analyze( } } - $if_body_context->referenced_var_ids = array_merge( - $context->referenced_var_ids, - $if_body_context->referenced_var_ids + $if_body_context->cond_referenced_var_ids = array_merge( + $context->cond_referenced_var_ids, + $if_body_context->cond_referenced_var_ids ); $if_body_context->assigned_var_ids = array_merge( diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php index f81f80fbc23..ed6b042f998 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php @@ -212,9 +212,9 @@ function (Clause $c) use ($mixed_var_ids, $cond_object_id): Clause { return false; } - $context->referenced_var_ids = array_merge( - $context->referenced_var_ids, - $if_context->referenced_var_ids + $context->cond_referenced_var_ids = array_merge( + $context->cond_referenced_var_ids, + $if_context->cond_referenced_var_ids ); } @@ -302,9 +302,9 @@ function (Clause $c) use ($mixed_var_ids, $cond_object_id): Clause { $t_else_context->vars_possibly_in_scope ); - $context->referenced_var_ids = array_merge( - $context->referenced_var_ids, - $t_else_context->referenced_var_ids + $context->cond_referenced_var_ids = array_merge( + $context->cond_referenced_var_ids, + $t_else_context->cond_referenced_var_ids ); $lhs_type = null;