Skip to content

Commit

Permalink
Merge pull request #6420 from orklah/ternary-override-type
Browse files Browse the repository at this point in the history
Ternary override type when reassigning
  • Loading branch information
weirdan authored Sep 4, 2021
2 parents b710aab + e2b5948 commit 5dfd157
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psalm\Type\Reconciler;

use function array_filter;
use function array_intersect;
use function array_keys;
use function array_map;
use function array_merge;
Expand Down Expand Up @@ -232,6 +233,17 @@ function ($c) use ($reconciled_expression_clauses): bool {
}
}

$redef_var_ifs = array_keys($if_context->getRedefinedVars($context->vars_in_scope));
$redef_var_else = array_keys($t_else_context->getRedefinedVars($context->vars_in_scope));
$redef_all = array_intersect($redef_var_ifs, $redef_var_else);

foreach ($redef_all as $redef_var_id) {
$context->vars_in_scope[$redef_var_id] = Type::combineUnionTypes(
$if_context->vars_in_scope[$redef_var_id],
$t_else_context->vars_in_scope[$redef_var_id]
);
}

$context->vars_possibly_in_scope = array_merge(
$context->vars_possibly_in_scope,
$if_context->vars_possibly_in_scope,
Expand Down
11 changes: 10 additions & 1 deletion tests/TypeReconciliation/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2665,7 +2665,16 @@ function ($_valid): void {};
$takesValid($val3);
}
}'
]
],
'ternaryRedefineAllVars' => [
'<?php
$_a = null;
$b = rand(0,1) ? "" : "a";
$b === "a" ? $_a = "Y" : $_a = "N";',
'assertions' => [
'$_a===' => '"N"|"Y"',
]
],
];
}

Expand Down

0 comments on commit 5dfd157

Please sign in to comment.