1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

allow ternary to override previous type when reassigning

This commit is contained in:
orklah 2021-09-04 20:22:06 +02:00
parent eb973ab2e1
commit 0825f220fe
2 changed files with 22 additions and 1 deletions

View File

@ -15,6 +15,7 @@ use Psalm\Type;
use Psalm\Type\Reconciler;
use function array_filter;
use function array_intersect;
use function array_keys;
use function array_map;
use function array_merge;
@ -232,6 +233,17 @@ class TernaryAnalyzer
}
}
$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,

View File

@ -2665,7 +2665,16 @@ class ConditionalTest extends \Psalm\Tests\TestCase
$takesValid($val3);
}
}'
]
],
'ternaryRedefineAllVars' => [
'<?php
$_a = null;
$b = rand(0,1) ? "" : "a";
$b === "a" ? $_a = "Y" : $_a = "N";',
'assertions' => [
'$_a===' => "'N'|'Y'",
]
],
];
}