From 6f598464a974d2d7d8c00649edb73a1413d9c28e Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Thu, 3 Dec 2020 13:09:08 -0500 Subject: [PATCH] Mixed array access should mark vars as used just in case --- .../Expression/Fetch/ArrayFetchAnalyzer.php | 30 ++++++++++++++++++- tests/UnusedVariableTest.php | 15 ++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php index 52972ea20..350f01df4 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php @@ -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 diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index e91be3c4a..bff38743c 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -2275,6 +2275,21 @@ class UnusedVariableTest extends TestCase $d += $l; }', ], + 'mixedArrayAccessMighBeObject' => [ + '