2020-05-18 21:13:27 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression;
|
|
|
|
|
|
|
|
use PhpParser;
|
2021-07-21 00:15:51 +02:00
|
|
|
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;
|
2021-07-21 00:15:51 +02:00
|
|
|
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;
|
2021-09-09 22:42:23 +02:00
|
|
|
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
|
2021-12-05 18:51:26 +01:00
|
|
|
): 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) {
|
2021-09-09 22:42:23 +02:00
|
|
|
//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;
|
2021-09-09 22:42:23 +02:00
|
|
|
} elseif ($old_min_bound === 0) {
|
2022-10-03 13:58:01 +02:00
|
|
|
$new_max_bound = 0;
|
2021-09-09 22:42:23 +02:00
|
|
|
} else {
|
2022-10-03 13:58:01 +02:00
|
|
|
$new_max_bound = -$old_min_bound;
|
2021-09-09 22:42:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2021-09-09 22:42:23 +02:00
|
|
|
} elseif ($old_max_bound === 0) {
|
2022-10-03 13:58:01 +02:00
|
|
|
$new_min_bound = 0;
|
2021-09-09 22:42:23 +02:00
|
|
|
} else {
|
2022-10-03 13:58:01 +02:00
|
|
|
$new_min_bound = -$old_max_bound;
|
2021-09-09 22:42:23 +02:00
|
|
|
}
|
2022-10-03 13:58:01 +02:00
|
|
|
|
|
|
|
$type_part = new TIntRange($new_min_bound, $new_max_bound);
|
2021-09-09 22:42:23 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-21 00:15:51 +02:00
|
|
|
self::addDataFlow(
|
|
|
|
$statements_analyzer,
|
|
|
|
$stmt,
|
|
|
|
$stmt->expr,
|
2022-12-18 17:15:15 +01:00
|
|
|
$stmt instanceof UnaryMinus ? 'unary-minus' : 'unary-plus',
|
2021-07-21 00:15:51 +02:00
|
|
|
);
|
|
|
|
|
2020-05-18 21:13:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-07-21 00:15:51 +02:00
|
|
|
|
|
|
|
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);
|
2022-11-04 19:04:23 +01:00
|
|
|
$statements_analyzer->node_data->setType(
|
|
|
|
$stmt,
|
|
|
|
$result_type->setParentNodes([
|
|
|
|
$new_parent_node->id => $new_parent_node,
|
2022-12-18 17:15:15 +01:00
|
|
|
]),
|
2022-11-04 19:04:23 +01:00
|
|
|
);
|
2021-07-21 00:15:51 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|