1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Always keep track of referenced var ids

which we can use for analysis elsewhere
This commit is contained in:
Matthew Brown 2017-11-24 12:17:28 -05:00
parent 4312ef380b
commit c86e73ddb6
10 changed files with 64 additions and 86 deletions

View File

@ -597,7 +597,7 @@ abstract class FunctionLikeChecker extends SourceChecker implements StatementsSo
) {
$original_location = $statements_checker->getFirstAppearance($var_name);
if (!isset($context->referenced_vars[$var_name]) && $original_location) {
if (!isset($context->referenced_var_ids[$var_name]) && $original_location) {
if (!isset($storage->param_types[substr($var_name, 1)]) ||
!$storage instanceof MethodStorage ||
$storage->visibility === ClassLikeChecker::VISIBILITY_PRIVATE

View File

@ -73,12 +73,10 @@ class ForChecker
$context->vars_possibly_in_scope
);
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$for_context->referenced_vars,
$context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$for_context->referenced_var_ids,
$context->referenced_var_ids
);
return null;
}

View File

@ -310,12 +310,10 @@ class ForeachChecker
$context->vars_possibly_in_scope
);
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$foreach_context->referenced_vars,
$context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$foreach_context->referenced_var_ids,
$context->referenced_var_ids
);
return null;
}

View File

@ -142,12 +142,10 @@ class IfChecker
$context->vars_possibly_in_scope
);
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$if_context->referenced_vars,
$context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$if_context->referenced_var_ids,
$context->referenced_var_ids
);
$temp_else_context = clone $original_context;
@ -345,9 +343,9 @@ class IfChecker
}
if ($outer_context->collect_references) {
$outer_context->referenced_vars = array_merge(
$outer_context->referenced_vars,
$if_context->referenced_vars
$outer_context->referenced_var_ids = array_merge(
$outer_context->referenced_var_ids,
$if_context->referenced_var_ids
);
}
@ -777,9 +775,9 @@ class IfChecker
}
if ($outer_context->collect_references) {
$outer_context->referenced_vars = array_merge(
$outer_context->referenced_vars,
$elseif_context->referenced_vars
$outer_context->referenced_var_ids = array_merge(
$outer_context->referenced_var_ids,
$elseif_context->referenced_var_ids
);
}
@ -871,9 +869,9 @@ class IfChecker
}
if ($outer_context->collect_references) {
$outer_context->referenced_vars = array_merge(
$outer_context->referenced_vars,
$else_context->referenced_vars
$outer_context->referenced_var_ids = array_merge(
$outer_context->referenced_var_ids,
$else_context->referenced_var_ids
);
}

View File

@ -134,12 +134,10 @@ class SwitchChecker
$statements_checker->analyze($case_stmts, $case_context, $loop_context);
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$context->referenced_vars,
$case_context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$case_context->referenced_var_ids
);
if ($case_exit_type !== 'return_throw') {
$vars = array_diff_key(

View File

@ -90,12 +90,10 @@ class TryChecker
$statements_checker->analyze($catch->stmts, $catch_context, $loop_context);
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$catch_context->referenced_vars,
$context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$catch_context->referenced_var_ids,
$context->referenced_var_ids
);
if (!ScopeChecker::doesAlwaysReturnOrThrow($catch->stmts)) {
foreach ($catch_context->vars_in_scope as $catch_var => $type) {

View File

@ -123,12 +123,10 @@ class WhileChecker
$while_context->vars_possibly_in_scope
);
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$context->referenced_vars,
$while_context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$while_context->referenced_var_ids
);
return null;
}

View File

@ -843,9 +843,9 @@ class ExpressionChecker
return false;
}
$context->referenced_vars = array_merge(
$op_context->referenced_vars,
$context->referenced_vars
$context->referenced_var_ids = array_merge(
$op_context->referenced_var_ids,
$context->referenced_var_ids
);
foreach ($op_context->vars_in_scope as $var_id => $type) {
@ -948,9 +948,9 @@ class ExpressionChecker
$context->updateChecks($op_context);
}
$context->referenced_vars = array_merge(
$op_context->referenced_vars,
$context->referenced_vars
$context->referenced_var_ids = array_merge(
$op_context->referenced_var_ids,
$context->referenced_var_ids
);
$context->vars_possibly_in_scope = array_merge(
@ -1013,12 +1013,10 @@ class ExpressionChecker
}
}
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$context->referenced_vars,
$t_if_context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$t_if_context->referenced_var_ids
);
$t_else_context = clone $context;
@ -1043,12 +1041,10 @@ class ExpressionChecker
return false;
}
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$context->referenced_vars,
$t_else_context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$t_else_context->referenced_var_ids
);
$lhs_type = null;
@ -2037,12 +2033,10 @@ class ExpressionChecker
}
}
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$context->referenced_vars,
$t_if_context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$t_if_context->referenced_var_ids
);
}
if ($negated_if_types) {
@ -2072,12 +2066,10 @@ class ExpressionChecker
}
}
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$context->referenced_vars,
$t_else_context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$t_else_context->referenced_var_ids
);
$lhs_type = null;
@ -2236,7 +2228,7 @@ class ExpressionChecker
} elseif ($stmt instanceof PhpParser\Node\Expr\PropertyFetch) {
self::analyzeIssetVar($statements_checker, $stmt->var, $context);
} elseif ($stmt instanceof PhpParser\Node\Expr\Variable) {
if ($context->collect_references && is_string($stmt->name)) {
if (is_string($stmt->name)) {
$context->hasVariable('$' . $stmt->name);
}
}

View File

@ -773,12 +773,10 @@ class StatementsChecker extends SourceChecker implements StatementsSource
$do_context->vars_possibly_in_scope
);
if ($context->collect_references) {
$context->referenced_vars = array_merge(
$context->referenced_vars,
$do_context->referenced_vars
);
}
$context->referenced_var_ids = array_merge(
$context->referenced_var_ids,
$do_context->referenced_var_ids
);
return ExpressionChecker::analyze($this, $stmt->cond, $context);
}

View File

@ -124,7 +124,7 @@ class Context
*
* @var array<string, bool>
*/
public $referenced_vars = [];
public $referenced_var_ids = [];
/**
* A list of variables that have been passed by reference (where we know their type)
@ -313,7 +313,7 @@ class Context
public function remove($remove_var_id)
{
unset(
$this->referenced_vars[$remove_var_id],
$this->referenced_var_ids[$remove_var_id],
$this->vars_possibly_in_scope[$remove_var_id]
);
@ -547,7 +547,7 @@ class Context
$stripped_var = preg_replace('/(->|\[).*$/', '', $var_name);
if ($stripped_var[0] === '$' && $stripped_var !== '$this') {
$this->referenced_vars[$var_name] = true;
$this->referenced_var_ids[$var_name] = true;
}
return isset($this->vars_in_scope[$var_name]);