Skip to content

Commit

Permalink
Fix #2423 - pass by ref variable status after byref assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Dec 5, 2019
1 parent 8ff33ee commit 19838fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Expand Up @@ -277,6 +277,10 @@ public static function analyze(
}

if ($array_var_id && isset($context->vars_in_scope[$array_var_id])) {
if ($context->vars_in_scope[$array_var_id]->by_ref) {
$assign_value_type->by_ref = true;
}

// removes dependennt vars from $context
$context->removeDescendents(
$array_var_id,
Expand Down Expand Up @@ -419,7 +423,7 @@ public static function analyze(
);
}

if (isset($context->byref_constraints[$var_id])) {
if (isset($context->byref_constraints[$var_id]) || $assign_value_type->by_ref) {
$statements_analyzer->registerVariableUses([$location->getHash() => $location]);
}
} elseif ($assign_var instanceof PhpParser\Node\Expr\List_
Expand Down Expand Up @@ -1025,6 +1029,8 @@ public static function analyzeAssignmentRef(
return false;
}

$assignment_type->by_ref = true;

$lhs_var_id = ExpressionAnalyzer::getVarId(
$stmt->var,
$statements_analyzer->getFQCLN(),
Expand Down
10 changes: 10 additions & 0 deletions tests/UnusedVariableTest.php
Expand Up @@ -1317,6 +1317,16 @@ function keys(): array {
echo gettype($k);
}'
],
'byRefVariableAfterAssignment' => [
'<?php
class A {
public string $value = "";
public function writeByRef(string $value): void {
$update =& $this->value;
$update = $value;
}
}'
],
];
}

Expand Down

0 comments on commit 19838fc

Please sign in to comment.