1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #4839 - undefined possibly set in by-reference should be treated as such

This commit is contained in:
Matt Brown 2020-12-14 16:57:48 -05:00 committed by Daniil Gentili
parent d3e6379c0f
commit cc06cb53f5
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 38 additions and 1 deletions

View File

@ -1186,6 +1186,8 @@ class AssignmentAnalyzer
if (!$context->hasVariable($var_id)) {
$context->vars_possibly_in_scope[$var_id] = true;
$location = new CodeLocation($statements_analyzer->getSource(), $stmt);
if (!$statements_analyzer->hasVariable($var_id)) {
if ($constrain_type
&& $prevent_null
@ -1197,7 +1199,7 @@ class AssignmentAnalyzer
if (IssueBuffer::accepts(
new \Psalm\Issue\NullReference(
'Not expecting null argument passed by reference',
new CodeLocation($statements_analyzer->getSource(), $stmt)
$location
),
$statements_analyzer->getSuppressedIssues()
)) {
@ -1205,6 +1207,24 @@ class AssignmentAnalyzer
}
}
if ($stmt instanceof PhpParser\Node\Expr\Variable) {
$statements_analyzer->registerVariable(
$var_id,
$location,
$context->branch_point
);
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
$byref_node = DataFlowNode::getForAssignment($var_id, $location);
$statements_analyzer->data_flow_graph->addPath(
$byref_node,
new DataFlowNode('variable-use', 'variable use', null),
'variable-use'
);
}
}
$context->hasVariable($var_id);
} else {
$var_not_in_scope = true;

View File

@ -451,6 +451,23 @@ class ArgTest extends TestCase
false,
'8.0'
],
'byrefVarSetsPossible' => [
'<?php
/**
* @param mixed $a
* @psalm-param-out int $a
*/
function takesByRef(&$a) : void {
$a = 5;
}
if (rand(0, 1)) {
takesByRef($b);
}
echo $b;',
'error_message' => 'PossiblyUndefinedGlobalVariable',
],
];
}
}