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

Mixed array access should mark vars as used just in case

This commit is contained in:
Matt Brown 2020-12-03 13:09:08 -05:00 committed by Daniil Gentili
parent dd20e838ce
commit 6f598464a9
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 44 additions and 1 deletions

View File

@ -7,6 +7,7 @@ use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Internal\Codebase\TaintFlowGraph;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\Internal\Type\TemplateInferredTypeReplacer;
use Psalm\CodeLocation;
use Psalm\Context;
@ -343,7 +344,7 @@ class ArrayFetchAnalyzer
$var_location = new CodeLocation($statements_analyzer->getSource(), $var);
$new_parent_node = \Psalm\Internal\DataFlow\DataFlowNode::getForAssignment(
$new_parent_node = DataFlowNode::getForAssignment(
$keyed_array_var_id ?: 'array-fetch',
$var_location
);
@ -940,6 +941,33 @@ class ArrayFetchAnalyzer
}
}
if (($data_flow_graph = $statements_analyzer->data_flow_graph)
&& $data_flow_graph instanceof \Psalm\Internal\Codebase\VariableUseGraph
&& ($stmt_var_type = $statements_analyzer->node_data->getType($stmt->var))
) {
if ($stmt_var_type->parent_nodes) {
$var_location = new CodeLocation($statements_analyzer->getSource(), $stmt->var);
$new_parent_node = DataFlowNode::getForAssignment('mixed-var-array-access', $var_location);
$data_flow_graph->addNode($new_parent_node);
foreach ($stmt_var_type->parent_nodes as $parent_node) {
$data_flow_graph->addPath($parent_node, $new_parent_node, '=');
$data_flow_graph->addPath(
$parent_node,
new DataFlowNode('variable-use', 'variable use', null),
'variable-use'
);
}
$stmt_var_type->parent_nodes = [
$new_parent_node->id => $new_parent_node
];
}
}
if (!$array_access_type) {
return Type::getMixed(
$type instanceof TEmpty

View File

@ -2275,6 +2275,21 @@ class UnusedVariableTest extends TestCase
$d += $l;
}',
],
'mixedArrayAccessMighBeObject' => [
'<?php
function takesResults(array $arr) : void {
/**
* @psalm-suppress MixedAssignment
*/
foreach ($arr as $item) {
/**
* @psalm-suppress MixedArrayAccess
* @psalm-suppress MixedArrayAssignment
*/
$item[0] = $item[1];
}
}'
],
];
}