2020-05-19 00:57:02 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Analyzer\Statements\Expression\BinaryOp;
|
|
|
|
|
|
|
|
use PhpParser;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Context;
|
2020-05-19 00:57:02 +02:00
|
|
|
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
|
|
|
|
use Psalm\Internal\Analyzer\StatementsAnalyzer;
|
2021-02-15 22:18:41 +01:00
|
|
|
use Psalm\Node\Expr\VirtualIsset;
|
|
|
|
use Psalm\Node\Expr\VirtualTernary;
|
|
|
|
use Psalm\Node\Expr\VirtualVariable;
|
2020-05-19 00:57:02 +02:00
|
|
|
use Psalm\Type;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2020-11-26 03:36:37 +01:00
|
|
|
use function substr;
|
2020-05-19 00:57:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
class CoalesceAnalyzer
|
|
|
|
{
|
|
|
|
public static function analyze(
|
|
|
|
StatementsAnalyzer $statements_analyzer,
|
2020-11-25 20:34:05 +01:00
|
|
|
PhpParser\Node\Expr\BinaryOp\Coalesce $stmt,
|
2020-05-19 00:57:02 +02:00
|
|
|
Context $context
|
|
|
|
) : bool {
|
2020-11-25 20:34:05 +01:00
|
|
|
$left_expr = $stmt->left;
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2020-12-02 08:15:17 +01:00
|
|
|
$root_expr = $left_expr;
|
|
|
|
|
|
|
|
while ($root_expr instanceof PhpParser\Node\Expr\ArrayDimFetch
|
|
|
|
|| $root_expr instanceof PhpParser\Node\Expr\PropertyFetch
|
|
|
|
) {
|
|
|
|
$root_expr = $root_expr->var;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($root_expr instanceof PhpParser\Node\Expr\FuncCall
|
|
|
|
|| $root_expr instanceof PhpParser\Node\Expr\MethodCall
|
|
|
|
|| $root_expr instanceof PhpParser\Node\Expr\StaticCall
|
|
|
|
|| $root_expr instanceof PhpParser\Node\Expr\Cast
|
2021-01-27 05:10:57 +01:00
|
|
|
|| $root_expr instanceof PhpParser\Node\Expr\NullsafePropertyFetch
|
|
|
|
|| $root_expr instanceof PhpParser\Node\Expr\NullsafeMethodCall
|
2021-11-04 22:14:01 +01:00
|
|
|
|| $root_expr instanceof PhpParser\Node\Expr\Ternary
|
2020-11-25 20:34:05 +01:00
|
|
|
) {
|
|
|
|
$left_var_id = '$<tmp coalesce var>' . (int) $left_expr->getAttribute('startFilePos');
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2020-12-02 08:25:15 +01:00
|
|
|
$cloned = clone $context;
|
|
|
|
$cloned->inside_isset = true;
|
|
|
|
|
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $left_expr, $cloned);
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2021-10-13 18:35:16 +02:00
|
|
|
$condition_type = $statements_analyzer->node_data->getType($left_expr) ?? Type::getMixed();
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2020-12-02 08:15:17 +01:00
|
|
|
if ($root_expr !== $left_expr) {
|
|
|
|
$condition_type->possibly_undefined = true;
|
|
|
|
}
|
|
|
|
|
2020-11-25 20:34:05 +01:00
|
|
|
$context->vars_in_scope[$left_var_id] = $condition_type;
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2021-02-15 22:18:41 +01:00
|
|
|
$left_expr = new VirtualVariable(
|
2020-11-25 20:34:05 +01:00
|
|
|
substr($left_var_id, 1),
|
|
|
|
$left_expr->getAttributes()
|
2020-05-19 00:57:02 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-15 22:18:41 +01:00
|
|
|
$ternary = new VirtualTernary(
|
|
|
|
new VirtualIsset(
|
2020-11-25 20:34:05 +01:00
|
|
|
[$left_expr],
|
|
|
|
$stmt->left->getAttributes()
|
|
|
|
),
|
|
|
|
$left_expr,
|
|
|
|
$stmt->right,
|
|
|
|
$stmt->getAttributes()
|
2020-05-19 00:57:02 +02:00
|
|
|
);
|
|
|
|
|
2020-11-25 20:34:05 +01:00
|
|
|
$old_node_data = $statements_analyzer->node_data;
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2020-11-25 20:34:05 +01:00
|
|
|
$statements_analyzer->node_data = clone $statements_analyzer->node_data;
|
2020-09-30 18:28:13 +02:00
|
|
|
|
2020-12-07 20:30:48 +01:00
|
|
|
ExpressionAnalyzer::analyze($statements_analyzer, $ternary, $context);
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2021-10-13 18:35:16 +02:00
|
|
|
$ternary_type = $statements_analyzer->node_data->getType($ternary) ?? Type::getMixed();
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2020-11-25 20:34:05 +01:00
|
|
|
$statements_analyzer->node_data = $old_node_data;
|
2020-05-19 00:57:02 +02:00
|
|
|
|
2020-11-25 20:34:05 +01:00
|
|
|
$statements_analyzer->node_data->setType($stmt, $ternary_type);
|
2020-05-19 00:57:02 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|