mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
parent
9cb963f956
commit
052c7478cc
@ -2,9 +2,13 @@
|
||||
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\Node\Expr\UnaryMinus;
|
||||
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\Type;
|
||||
use Psalm\Type\Atomic\TFloat;
|
||||
use Psalm\Type\Atomic\TInt;
|
||||
@ -55,6 +59,39 @@ class UnaryPlusMinusAnalyzer
|
||||
$statements_analyzer->node_data->setType($stmt, new Type\Union($acceptable_types));
|
||||
}
|
||||
|
||||
self::addDataFlow(
|
||||
$statements_analyzer,
|
||||
$stmt,
|
||||
$stmt->expr,
|
||||
$stmt instanceof UnaryMinus ? 'unary-minus' : 'unary-plus'
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function addDataFlow(
|
||||
StatementsAnalyzer $statements_analyzer,
|
||||
PhpParser\Node\Expr $stmt,
|
||||
PhpParser\Node\Expr $value,
|
||||
string $type
|
||||
): 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1029,6 +1029,26 @@ class UnusedCodeTest extends TestCase
|
||||
throw A::foo();
|
||||
'
|
||||
],
|
||||
'variableUsedAsUnaryMinusOperand' => [
|
||||
'<?php
|
||||
function f(): int
|
||||
{
|
||||
$a = 1;
|
||||
$b = -$a;
|
||||
return $b;
|
||||
}
|
||||
',
|
||||
],
|
||||
'variableUsedAsUnaryPlusOperand' => [
|
||||
'<?php
|
||||
function f(): int
|
||||
{
|
||||
$a = 1;
|
||||
$b = +$a;
|
||||
return $b;
|
||||
}
|
||||
',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user