1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 06:58:41 +01:00
psalm/src/Psalm/Internal/Analyzer/Statements/Expression/UnaryPlusMinusAnalyzer.php

137 lines
5.1 KiB
PHP
Raw Normal View History

2020-05-18 21:13:27 +02:00
<?php
2020-05-18 21:13:27 +02:00
namespace Psalm\Internal\Analyzer\Statements\Expression;
use PhpParser;
use PhpParser\Node\Expr\UnaryMinus;
use Psalm\CodeLocation;
2021-06-08 04:55:21 +02:00
use Psalm\Context;
2020-05-18 21:13:27 +02:00
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Codebase\VariableUseGraph;
use Psalm\Internal\DataFlow\DataFlowNode;
2020-05-18 21:13:27 +02:00
use Psalm\Type;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIntRange;
2021-12-13 04:45:57 +01:00
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
2020-05-18 21:13:27 +02:00
use Psalm\Type\Atomic\TString;
2021-12-13 16:28:14 +01:00
use Psalm\Type\Union;
2022-10-03 13:58:01 +02:00
use RuntimeException;
2020-05-18 21:13:27 +02:00
2022-01-03 07:55:32 +01:00
/**
* @internal
*/
2020-05-18 21:13:27 +02:00
class UnaryPlusMinusAnalyzer
{
/**
* @param PhpParser\Node\Expr\UnaryMinus|PhpParser\Node\Expr\UnaryPlus $stmt
*/
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr $stmt,
Context $context
): bool {
2020-05-18 21:13:27 +02:00
if (ExpressionAnalyzer::analyze($statements_analyzer, $stmt->expr, $context) === false) {
return false;
}
if (!($stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr))) {
2021-12-13 16:28:14 +01:00
$statements_analyzer->node_data->setType($stmt, new Union([new TInt, new TFloat]));
2020-05-18 21:13:27 +02:00
} elseif ($stmt_expr_type->isMixed()) {
$statements_analyzer->node_data->setType($stmt, Type::getMixed());
} else {
$acceptable_types = [];
foreach ($stmt_expr_type->getAtomicTypes() as $type_part) {
if ($type_part instanceof TInt || $type_part instanceof TFloat) {
2022-10-03 13:58:01 +02:00
if (!$stmt instanceof PhpParser\Node\Expr\UnaryMinus) {
$acceptable_types []= $type_part;
continue;
2020-05-18 21:13:27 +02:00
}
2022-10-03 13:58:01 +02:00
if ($type_part instanceof TLiteralInt) {
$type_part = new TLiteralInt(-$type_part->value);
} elseif ($type_part instanceof TLiteralFloat) {
$type_part = new TLiteralFloat(-$type_part->value);
} elseif ($type_part instanceof TIntRange) {
//we'll have to inverse min and max bound and negate any literal
$old_min_bound = $type_part->min_bound;
$old_max_bound = $type_part->max_bound;
if ($old_min_bound === null) {
//min bound is null, max bound will be null
2022-10-03 13:58:01 +02:00
$new_max_bound = null;
} elseif ($old_min_bound === 0) {
2022-10-03 13:58:01 +02:00
$new_max_bound = 0;
} else {
2022-10-03 13:58:01 +02:00
$new_max_bound = -$old_min_bound;
}
if ($old_max_bound === null) {
//max bound is null, min bound will be null
2022-10-03 13:58:01 +02:00
$new_min_bound = null;
} elseif ($old_max_bound === 0) {
2022-10-03 13:58:01 +02:00
$new_min_bound = 0;
} else {
2022-10-03 13:58:01 +02:00
$new_min_bound = -$old_max_bound;
}
2022-10-03 13:58:01 +02:00
$type_part = new TIntRange($new_min_bound, $new_max_bound);
}
2020-05-18 21:13:27 +02:00
$acceptable_types[] = $type_part;
} elseif ($type_part instanceof TString) {
$acceptable_types[] = new TInt;
$acceptable_types[] = new TFloat;
} else {
$acceptable_types[] = new TInt;
}
}
2022-10-03 13:58:01 +02:00
if (!$acceptable_types) {
throw new RuntimeException("Impossible!");
}
2021-12-13 16:28:14 +01:00
$statements_analyzer->node_data->setType($stmt, new Union($acceptable_types));
2020-05-18 21:13:27 +02:00
}
self::addDataFlow(
$statements_analyzer,
$stmt,
$stmt->expr,
$stmt instanceof UnaryMinus ? 'unary-minus' : 'unary-plus'
);
2020-05-18 21:13:27 +02:00
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);
$statements_analyzer->node_data->setType(
$stmt,
$result_type->setParentNodes([
$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);
}
}
}
}
2020-05-18 21:13:27 +02:00
}