Skip to content

Commit

Permalink
Remove nullability from properties that don’t need them
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 23, 2022
1 parent e0d3c3f commit 865a9f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
22 changes: 9 additions & 13 deletions src/Psalm/Internal/Analyzer/Statements/Block/LoopAnalyzer.php
Expand Up @@ -426,15 +426,13 @@ public static function analyze(
$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;
}
}

Expand Down Expand Up @@ -573,11 +571,9 @@ private static function updateLoopScopeContexts(
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) {
Expand Down
14 changes: 5 additions & 9 deletions src/Psalm/Internal/Analyzer/Statements/BreakAnalyzer.php
Expand Up @@ -38,15 +38,11 @@ public static function analyze(

$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) {
Expand Down
20 changes: 8 additions & 12 deletions src/Psalm/Internal/Analyzer/Statements/ContinueAnalyzer.php
Expand Up @@ -59,18 +59,14 @@ public static function analyze(

$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
);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Psalm/Internal/Scope/LoopScope.php
Expand Up @@ -26,7 +26,7 @@ class LoopScope
public $loop_parent_context;

/**
* @var array<string, Union>|null
* @var array<string, Union>
*/
public $redefined_loop_vars = [];

Expand All @@ -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>
Expand Down

0 comments on commit 865a9f8

Please sign in to comment.