diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonComparisonOpAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonComparisonOpAnalyzer.php index 70f820737f5..b7007bf4a5a 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonComparisonOpAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/NonComparisonOpAnalyzer.php @@ -34,6 +34,13 @@ public static function analyze( $stmt_type = Type::getString(); $statements_analyzer->node_data->setType($stmt, $stmt_type); + BinaryOpAnalyzer::addDataFlow( + $statements_analyzer, + $stmt, + $stmt->left, + $stmt->right, + 'nondivop' + ); return; } diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BitwiseNotAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BitwiseNotAnalyzer.php index 8ad916ad7a7..74a2e04d389 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BitwiseNotAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BitwiseNotAnalyzer.php @@ -1,4 +1,5 @@ expr, $context) === false) { return false; } @@ -89,6 +92,34 @@ public static function analyze( } } + self::addDataFlow($statements_analyzer, $stmt, $stmt->expr); + return true; } + + private static function addDataFlow( + StatementsAnalyzer $statements_analyzer, + PhpParser\Node\Expr $stmt, + PhpParser\Node\Expr $value, + string $type = 'bitwisenot' + ): void { + $result_type = $statements_analyzer->node_data->getType($stmt); + if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph && $result_type) { + $var_location = new CodeLocation($statements_analyzer, $stmt); + + $stmt_value_type = $statements_analyzer->node_data->getType($value); + + $new_parent_node = DataFlowNode::getForAssignment($type, $var_location); + $statements_analyzer->data_flow_graph->addNode($new_parent_node); + $result_type->parent_nodes = [ + $new_parent_node->id => $new_parent_node, + ]; + + if ($stmt_value_type && $stmt_value_type->parent_nodes) { + foreach ($stmt_value_type->parent_nodes as $parent_node) { + $statements_analyzer->data_flow_graph->addPath($parent_node, $new_parent_node, $type); + } + } + } + } } diff --git a/tests/UnusedVariableTest.php b/tests/UnusedVariableTest.php index 555ea2ce113..6e4790a2b81 100644 --- a/tests/UnusedVariableTest.php +++ b/tests/UnusedVariableTest.php @@ -2402,6 +2402,28 @@ function foo(array $arr) : string { echo $key . " " . $value; }', ], + 'intAndBitwiseNotOperator' => [ + ' [ + '> 1); + + $randomBytes = random_bytes(1); + $randomBytes[0] = $randomBytes[0] & $bitmask; + + return $randomBytes; + }' + ], ]; }