mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Fix #4839 - undefined possibly set in by-reference should be treated as such
This commit is contained in:
parent
d3e6379c0f
commit
cc06cb53f5
@ -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;
|
||||
|
@ -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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user