1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #4467 - variables are only the same if they were set in the same location

This commit is contained in:
Matt Brown 2020-11-01 11:26:42 -05:00 committed by Daniil Gentili
parent badf0855ce
commit c07378a713
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
14 changed files with 37 additions and 21 deletions

View File

@ -236,7 +236,7 @@ class Context
/**
* A list of vars that have been assigned to
*
* @var array<string, bool>
* @var array<string, int>
*/
public $assigned_var_ids = [];
@ -483,6 +483,8 @@ class Context
foreach ($new_context->vars_in_scope as $var_id => $context_type) {
if (!isset($original_context->vars_in_scope[$var_id])
|| ($original_context->assigned_var_ids[$var_id] ?? 0)
!== ($new_context->assigned_var_ids[$var_id] ?? 0)
|| !$original_context->vars_in_scope[$var_id]->equals($context_type)
) {
$redefined_var_ids[] = $var_id;

View File

@ -27,7 +27,7 @@ class AlgebraAnalyzer
*
* @param list<Clause> $formula_1
* @param list<Clause> $formula_2
* @param array<string, bool> $new_assigned_var_ids
* @param array<string, mixed> $new_assigned_var_ids
*/
public static function checkForParadox(
array $formula_1,

View File

@ -658,7 +658,7 @@ class IfAnalyzer
$more_cond_referenced_var_ids
);
/** @var array<string, bool> */
/** @var array<string, int> */
$more_cond_assigned_var_ids = $if_conditional_context->assigned_var_ids;
$if_conditional_context->assigned_var_ids = array_merge(
$more_cond_assigned_var_ids,
@ -809,7 +809,7 @@ class IfAnalyzer
$if_scope->final_actions = $final_actions;
/** @var array<string, bool> */
/** @var array<string, int> */
$new_assigned_var_ids = $if_context->assigned_var_ids;
/** @var array<string, bool> */
$new_possibly_assigned_var_ids = $if_context->possibly_assigned_var_ids;
@ -1268,7 +1268,7 @@ class IfAnalyzer
return false;
}
/** @var array<string, bool> */
/** @var array<string, int> */
$new_stmts_assigned_var_ids = $elseif_context->assigned_var_ids;
$elseif_context->assigned_var_ids = $pre_stmts_assigned_var_ids + $new_stmts_assigned_var_ids;
@ -1573,7 +1573,7 @@ class IfAnalyzer
}
}
/** @var array<string, bool> */
/** @var array<string, int> */
$new_assigned_var_ids = $else_context->assigned_var_ids;
$else_context->assigned_var_ids = $pre_stmts_assigned_var_ids;
@ -1847,7 +1847,7 @@ class IfAnalyzer
}
/**
* @param array<string, bool> $cond_assigned_var_ids
* @param array<string, int> $cond_assigned_var_ids
*/
public static function addConditionallyAssignedVarsToContext(
StatementsAnalyzer $statements_analyzer,

View File

@ -433,7 +433,7 @@ class SwitchCaseAnalyzer
$statements_analyzer->node_data = $old_node_data;
/** @var array<string, bool> */
/** @var array<string, int> */
$new_case_assigned_var_ids = $case_context->assigned_var_ids;
$case_context->assigned_var_ids = $pre_assigned_var_ids + $new_case_assigned_var_ids;

View File

@ -108,7 +108,7 @@ class TryAnalyzer
$context->break_types
);
/** @var array<string, bool> */
/** @var array<string, int> */
$newly_assigned_var_ids = $context->assigned_var_ids;
$context->assigned_var_ids = array_merge(

View File

@ -142,7 +142,7 @@ class InstancePropertyAssignmentAnalyzer
);
if ($var_id) {
$context->assigned_var_ids[$var_id] = true;
$context->assigned_var_ids[$var_id] = (int) $stmt->var->getAttribute('startFilePos');
if ($direct_assignment && isset($context->protected_var_ids[$var_id])) {
if (IssueBuffer::accepts(

View File

@ -158,7 +158,7 @@ class AssignmentAnalyzer
if ($array_var_id) {
unset($context->referenced_var_ids[$array_var_id]);
$context->assigned_var_ids[$array_var_id] = true;
$context->assigned_var_ids[$array_var_id] = (int) $assign_var->getAttribute('startFilePos');
$context->possibly_assigned_var_ids[$array_var_id] = true;
}
@ -725,7 +725,7 @@ class AssignmentAnalyzer
if ($list_var_id) {
$context->vars_possibly_in_scope[$list_var_id] = true;
$context->assigned_var_ids[$list_var_id] = true;
$context->assigned_var_ids[$list_var_id] = (int) $var->getAttribute('startFilePos');
$context->possibly_assigned_var_ids[$list_var_id] = true;
$already_in_scope = isset($context->vars_in_scope[$list_var_id]);
@ -1362,7 +1362,7 @@ class AssignmentAnalyzer
$codebase = $statements_analyzer->getCodebase();
if ($array_var_id) {
$context->assigned_var_ids[$array_var_id] = true;
$context->assigned_var_ids[$array_var_id] = (int) $stmt->var->getAttribute('startFilePos');;
$context->possibly_assigned_var_ids[$array_var_id] = true;
if ($codebase->find_unused_variables && $stmt->var instanceof PhpParser\Node\Expr\Variable) {
@ -1750,7 +1750,7 @@ class AssignmentAnalyzer
}
}
$context->assigned_var_ids[$var_id] = true;
$context->assigned_var_ids[$var_id] = (int) $stmt->getAttribute('startFilePos');;
$context->vars_in_scope[$var_id] = $by_ref_out_type;

View File

@ -1181,7 +1181,7 @@ class ArgumentAnalyzer
}
if ($context->inside_conditional && !isset($context->assigned_var_ids[$var_id])) {
$context->assigned_var_ids[$var_id] = false;
$context->assigned_var_ids[$var_id] = (int) $input_expr->getAttribute('startFilePos');;
}
if ($was_cloned) {

View File

@ -1745,7 +1745,7 @@ class FunctionCallAnalyzer extends CallAnalyzer
$mixed_type->parent_nodes = $context->vars_in_scope[$var_id]->parent_nodes;
$context->vars_in_scope[$var_id] = $mixed_type;
$context->assigned_var_ids[$var_id] = true;
$context->assigned_var_ids[$var_id] = (int) $stmt->getAttribute('startFilePos');
$context->possibly_assigned_var_ids[$var_id] = true;
}
} elseif ($function_name->parts === ['compact']) {

View File

@ -80,7 +80,7 @@ class IncDecExpressionAnalyzer
$context->vars_in_scope[$var_id] = $stmt_type;
if ($codebase->find_unused_variables && $stmt->var instanceof PhpParser\Node\Expr\Variable) {
$context->assigned_var_ids[$var_id] = true;
$context->assigned_var_ids[$var_id] = (int) $stmt->var->getAttribute('startFilePos');
$context->possibly_assigned_var_ids[$var_id] = true;
}

View File

@ -173,7 +173,7 @@ class StaticAnalyzer
if ($context->check_variables) {
$context->vars_in_scope[$var_id] = $comment_type ? clone $comment_type : Type::getMixed();
$context->vars_possibly_in_scope[$var_id] = true;
$context->assigned_var_ids[$var_id] = true;
$context->assigned_var_ids[$var_id] = (int) $stmt->getAttribute('startFilePos');
$statements_analyzer->byref_uses[$var_id] = true;
$location = new CodeLocation($statements_analyzer, $var);

View File

@ -18,7 +18,7 @@ class IfConditionalScope
public $cond_referenced_var_ids;
/**
* @var array<string, bool>
* @var array<string, int>
*/
public $cond_assigned_var_ids;
@ -27,7 +27,7 @@ class IfConditionalScope
/**
* @param array<string, bool> $cond_referenced_var_ids
* @param array<string, bool> $cond_assigned_var_ids
* @param array<string, int> $cond_assigned_var_ids
* @param list<\Psalm\Internal\Clause> $entry_clauses
*/
public function __construct(

View File

@ -25,7 +25,7 @@ class IfScope
public $redefined_vars = null;
/**
* @var array<string, bool>|null
* @var array<string, int>|null
*/
public $assigned_var_ids = null;

View File

@ -1108,6 +1108,20 @@ class ForeachTest extends \Psalm\Tests\TestCase
if (is_int($id)) {}
}'
],
'loopVarRedefinedAtLoopStart' => [
'<?php
/**
* @param non-empty-array<string, string> $files
*/
function foo(array $files): void
{
$file = reset($files);
foreach ($files as $file) {
strlen($file);
$file = 0;
}
}'
],
];
}