1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Remove nullability from properties that don’t need them

This commit is contained in:
Matthew Brown 2022-01-23 14:18:53 -05:00
parent e0d3c3f217
commit 865a9f81bc
4 changed files with 25 additions and 37 deletions

View File

@ -426,15 +426,13 @@ class LoopAnalyzer
$does_always_break = $loop_scope->final_actions === [ScopeAnalyzer::ACTION_BREAK];
if ($does_sometimes_break) {
if ($loop_scope->possibly_redefined_loop_parent_vars !== null) {
foreach ($loop_scope->possibly_redefined_loop_parent_vars as $var => $type) {
$loop_scope->loop_parent_context->vars_in_scope[$var] = Type::combineUnionTypes(
$type,
$loop_scope->loop_parent_context->vars_in_scope[$var]
);
foreach ($loop_scope->possibly_redefined_loop_parent_vars as $var => $type) {
$loop_scope->loop_parent_context->vars_in_scope[$var] = Type::combineUnionTypes(
$type,
$loop_scope->loop_parent_context->vars_in_scope[$var]
);
$loop_scope->loop_parent_context->possibly_assigned_var_ids[$var] = true;
}
$loop_scope->loop_parent_context->possibly_assigned_var_ids[$var] = true;
}
}
@ -573,11 +571,9 @@ class LoopAnalyzer
if (!in_array(ScopeAnalyzer::ACTION_CONTINUE, $loop_scope->final_actions, true)) {
$loop_scope->loop_context->vars_in_scope = $pre_outer_context->vars_in_scope;
} else {
if ($loop_scope->redefined_loop_vars !== null) {
foreach ($loop_scope->redefined_loop_vars as $var => $type) {
$loop_scope->loop_context->vars_in_scope[$var] = $type;
$updated_loop_vars[$var] = true;
}
foreach ($loop_scope->redefined_loop_vars as $var => $type) {
$loop_scope->loop_context->vars_in_scope[$var] = $type;
$updated_loop_vars[$var] = true;
}
if ($loop_scope->possibly_redefined_loop_vars) {

View File

@ -38,15 +38,11 @@ class BreakAnalyzer
$redefined_vars = $context->getRedefinedVars($loop_scope->loop_parent_context->vars_in_scope);
if ($loop_scope->possibly_redefined_loop_parent_vars === null) {
$loop_scope->possibly_redefined_loop_parent_vars = $redefined_vars;
} else {
foreach ($redefined_vars as $var => $type) {
$loop_scope->possibly_redefined_loop_parent_vars[$var] = Type::combineUnionTypes(
$type,
$loop_scope->possibly_redefined_loop_parent_vars[$var] ?? null
);
}
foreach ($redefined_vars as $var => $type) {
$loop_scope->possibly_redefined_loop_parent_vars[$var] = Type::combineUnionTypes(
$type,
$loop_scope->possibly_redefined_loop_parent_vars[$var] ?? null
);
}
if ($loop_scope->iteration_count === 0) {

View File

@ -59,18 +59,14 @@ class ContinueAnalyzer
$redefined_vars = $context->getRedefinedVars($loop_scope->loop_parent_context->vars_in_scope);
if ($loop_scope->redefined_loop_vars === null) {
$loop_scope->redefined_loop_vars = $redefined_vars;
} else {
foreach ($loop_scope->redefined_loop_vars as $redefined_var => $type) {
if (!isset($redefined_vars[$redefined_var])) {
unset($loop_scope->redefined_loop_vars[$redefined_var]);
} else {
$loop_scope->redefined_loop_vars[$redefined_var] = Type::combineUnionTypes(
$redefined_vars[$redefined_var],
$type
);
}
foreach ($loop_scope->redefined_loop_vars as $redefined_var => $type) {
if (!isset($redefined_vars[$redefined_var])) {
unset($loop_scope->redefined_loop_vars[$redefined_var]);
} else {
$loop_scope->redefined_loop_vars[$redefined_var] = Type::combineUnionTypes(
$redefined_vars[$redefined_var],
$type
);
}
}

View File

@ -26,7 +26,7 @@ class LoopScope
public $loop_parent_context;
/**
* @var array<string, Union>|null
* @var array<string, Union>
*/
public $redefined_loop_vars = [];
@ -36,9 +36,9 @@ class LoopScope
public $possibly_redefined_loop_vars = [];
/**
* @var array<string, Union>|null
* @var array<string, Union>
*/
public $possibly_redefined_loop_parent_vars;
public $possibly_redefined_loop_parent_vars = [];
/**
* @var array<string, Union>