Skip to content
Permalink
Browse files

Fix #2339 - add support for null coalescing operator

  • Loading branch information
muglug committed Nov 27, 2019
1 parent d1c4c85 commit c75f7dc4d592a4a699bfe04ba099b67112cdb1bc
@@ -956,6 +956,38 @@ public static function analyzeAssignmentOperation(
}
}
if ($stmt instanceof PhpParser\Node\Expr\AssignOp\Coalesce) {
$old_data_provider = $statements_analyzer->node_data;
$statements_analyzer->node_data = clone $statements_analyzer->node_data;
$fake_coalesce_expr = new PhpParser\Node\Expr\BinaryOp\Coalesce(
$stmt->var,
$stmt->expr,
$stmt->getAttributes()
);
if (BinaryOpAnalyzer::analyze(
$statements_analyzer,
$fake_coalesce_expr,
$context
) === false) {
return false;
}
$fake_coalesce_type = $statements_analyzer->node_data->getType($fake_coalesce_expr);
$statements_analyzer->node_data = $old_data_provider;
if ($fake_coalesce_type) {
if ($array_var_id) {
$context->vars_in_scope[$array_var_id] = $fake_coalesce_type;
}
$statements_analyzer->node_data->setType($stmt, $fake_coalesce_type);
}
}
if (!$was_in_assignment) {
$context->inside_assignment = false;
}
@@ -222,6 +222,13 @@ function foo(string $s) : int {
'$a' => 'string',
],
],
'nullCoalescingAssignment' => [
'<?php
function foo(?string $s): string {
$s ??= "Hello";
return $s;
}'
],
];
}

0 comments on commit c75f7dc

Please sign in to comment.
You can’t perform that action at this time.