Skip to content
Permalink
Browse files

Improve handling of assignments in arrays

  • Loading branch information
muglug committed Dec 22, 2019
1 parent 8bd6860 commit 141bfd313f5501cfc4a3a3f3bb35e8be59d6c55a
@@ -291,6 +291,8 @@ public static function updateArrayType(
}

$context->vars_in_scope[$rooted_parent_id] = $array_type;
$context->assigned_var_ids[$rooted_parent_id] = true;
$context->possibly_assigned_var_ids[$rooted_parent_id] = true;
}

if (!$child_stmts) {
@@ -340,6 +342,9 @@ public static function updateArrayType(
}

$context->vars_in_scope[$array_var_id] = clone $assignment_type;

$context->assigned_var_ids[$array_var_id] = true;
$context->possibly_assigned_var_ids[$array_var_id] = true;
}

// only update as many child stmts are we were able to process above
@@ -452,6 +457,8 @@ public static function updateArrayType(
if ($root_var_id) {
$array_var_id = $root_var_id . implode('', $var_id_additions);
$context->vars_in_scope[$array_var_id] = clone $child_stmt_type;
$context->assigned_var_ids[$array_var_id] = true;
$context->possibly_assigned_var_ids[$array_var_id] = true;
}
}

@@ -214,6 +214,8 @@ public static function analyze(

if ($array_var_id) {
unset($context->referenced_var_ids[$array_var_id]);
$context->assigned_var_ids[$array_var_id] = true;
$context->possibly_assigned_var_ids[$array_var_id] = true;
}

if ($assign_value) {
@@ -401,8 +403,6 @@ public static function analyze(
if ($assign_var instanceof PhpParser\Node\Expr\Variable && is_string($assign_var->name) && $var_id) {
$context->vars_in_scope[$var_id] = $assign_value_type;
$context->vars_possibly_in_scope[$var_id] = true;
$context->assigned_var_ids[$var_id] = true;
$context->possibly_assigned_var_ids[$var_id] = true;

$location = new CodeLocation($statements_analyzer, $assign_var);

@@ -874,15 +874,18 @@ public static function analyzeAssignmentOperation(
}
}

if ($array_var_id && $context->collect_references && $stmt->var instanceof PhpParser\Node\Expr\Variable) {
$location = new CodeLocation($statements_analyzer, $stmt->var);
if ($array_var_id) {
$context->assigned_var_ids[$array_var_id] = true;
$context->possibly_assigned_var_ids[$array_var_id] = true;
$statements_analyzer->registerVariableAssignment(
$array_var_id,
$location
);
$context->unreferenced_vars[$array_var_id] = [$location->getHash() => $location];

if ($context->collect_references && $stmt->var instanceof PhpParser\Node\Expr\Variable) {
$location = new CodeLocation($statements_analyzer, $stmt->var);
$statements_analyzer->registerVariableAssignment(
$array_var_id,
$location
);
$context->unreferenced_vars[$array_var_id] = [$location->getHash() => $location];
}
}

$stmt_var_type = $statements_analyzer->node_data->getType($stmt->var);
@@ -702,9 +702,21 @@ function foo(array $options): void {
}
if (\is_array($options["b"])) {}
}
}'
],
'issetOnThing' => [
'<?php
function foo() : void {
$p = [false, false];
$i = rand(0, 1);
if (rand(0, 1) && isset($p[$i])) {
$p[$i] = true;
}
'
foreach ($p as $q) {
if ($q) {}
}
}',
],
];
}

0 comments on commit 141bfd3

Please sign in to comment.
You can’t perform that action at this time.