Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added dataflow to stringy bitwise op, bitwise not op (fix #5957) #6084

Merged
merged 2 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

namespace Psalm\Internal\Analyzer\Statements\Expression;

use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Codebase\VariableUseGraph;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\Issue\InvalidOperand;
use Psalm\Issue\PossiblyInvalidOperand;
use Psalm\IssueBuffer;
Expand All @@ -20,7 +23,7 @@ public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\BitwiseNot $stmt,
Context $context
) : bool {
): bool {
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
return false;
}
Expand Down Expand Up @@ -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);
}
}
}
}
}
22 changes: 22 additions & 0 deletions tests/UnusedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,28 @@ function foo(array $arr) : string {
echo $key . " " . $value;
}',
],
'intAndBitwiseNotOperator' => [
'<?php
function foo() : int
{
$bitmask = 0x1;
$bytes = 2;
$ret = $bytes | ~$bitmask;
return $ret;
}'
],
'stringAndBitwiseAndOperator' => [
'<?php
function randomBits() : string
{
$bitmask = \chr(0xFF >> 1);

$randomBytes = random_bytes(1);
$randomBytes[0] = $randomBytes[0] & $bitmask;

return $randomBytes;
}'
],
];
}

Expand Down