Skip to content

Commit

Permalink
Rename referenced_var_ids to cond_referenced_var_ids
Browse files Browse the repository at this point in the history
Also reduce usage — this was a holdover of the old way of tracking unused variables
  • Loading branch information
muglug committed Mar 15, 2022
1 parent 83c5344 commit a9f4148
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 141 deletions.
8 changes: 4 additions & 4 deletions src/Psalm/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, bool>
*/
public $referenced_var_ids = [];
public $cond_referenced_var_ids = [];

/**
* A list of variables that have been passed by reference (where we know their type)
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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]);
Expand Down
6 changes: 0 additions & 6 deletions src/Psalm/Internal/Analyzer/Statements/Block/DoAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 0 additions & 6 deletions src/Psalm/Internal/Analyzer/Statements/Block/ForAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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;

Expand All @@ -174,8 +174,8 @@ public static function analyze(
$if_conditional_context->inside_conditional = $was_inside_conditional;

/** @var array<string, bool> */
$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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 4 additions & 17 deletions src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
68 changes: 20 additions & 48 deletions src/Psalm/Internal/Analyzer/Statements/Block/TryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [];

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Psalm\Type;
use UnexpectedValueException;

use function array_intersect_key;
use function array_merge;
use function in_array;

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit a9f4148

Please sign in to comment.