Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ElseIfAnalyzer inconsistency and remove dead code #10611

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public static function analyze(
$codebase,
);

if (count($elseif_clauses) > 200) {
$elseif_clauses = [];
}

$elseif_clauses_handled = [];

foreach ($elseif_clauses as $clause) {
Expand All @@ -101,7 +105,7 @@ public static function analyze(
foreach ($keys as $key) {
foreach ($mixed_var_ids as $mixed_var_id) {
if (preg_match('/^' . preg_quote($mixed_var_id, '/') . '(\[|-)/', $key)) {
$elseif_clauses_handled[] = new Clause([], $elseif_cond_id, $elseif_cond_id, true);
$clause = new Clause([], $elseif_cond_id, $elseif_cond_id, true);
break 2;
}
}
Expand All @@ -118,7 +122,7 @@ public static function analyze(
foreach ($c->possibilities as $key => $_value) {
foreach ($assigned_in_conditional_var_ids as $conditional_assigned_var_id => $_) {
if (preg_match('/^'.preg_quote($conditional_assigned_var_id, '/').'(\[|-|$)/', $key)) {
$entry_clauses[] = new Clause([], $elseif_cond_id, $elseif_cond_id, true);
$c = new Clause([], $elseif_cond_id, $elseif_cond_id, true);
break 2;
}
}
Expand All @@ -136,21 +140,23 @@ public static function analyze(
$assigned_in_conditional_var_ids,
);

$elseif_context_clauses = [...$entry_clauses, ...$elseif_clauses];
$elseif_clauses = Algebra::simplifyCNF($elseif_clauses);

$elseif_context->clauses = $entry_clauses
? Algebra::simplifyCNF([...$entry_clauses, ...$elseif_clauses])
: $elseif_clauses;

if ($elseif_context->reconciled_expression_clauses) {
$reconciled_expression_clauses = $elseif_context->reconciled_expression_clauses;

$elseif_context_clauses = array_values(
$elseif_context->clauses = array_values(
array_filter(
$elseif_context_clauses,
$elseif_context->clauses,
static fn(Clause $c): bool => !in_array($c->hash, $reconciled_expression_clauses, true),
),
);
}

$elseif_context->clauses = Algebra::simplifyCNF($elseif_context_clauses);

$active_elseif_types = [];

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ public static function analyze(
$outer_context->removeVarFromConflictingClauses($var_id);
}

$if_scope->if_actions = $final_actions = ScopeAnalyzer::getControlActions(
$final_actions = ScopeAnalyzer::getControlActions(
$stmt->stmts,
$statements_analyzer->node_data,
[],
);

// has a return/throw at end
$has_ending_statements = $final_actions === [ScopeAnalyzer::ACTION_END];

$has_leaving_statements = $has_ending_statements
Expand Down
15 changes: 7 additions & 8 deletions src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ public static function analyze(
}
}

$branch_point = $context->branch_point ?: (int) $stmt->getAttribute('startFilePos');

try {
$if_conditional_scope = IfConditionalAnalyzer::analyze(
$statements_analyzer,
$stmt->cond,
$context,
$codebase,
$if_scope,
$context->branch_point ?: (int) $stmt->getAttribute('startFilePos'),
$branch_point,
);

// this is the context for stuff that happens within the `if` block
Expand Down Expand Up @@ -140,7 +142,6 @@ public static function analyze(
$if_clauses_handled = [];
foreach ($if_clauses as $clause) {
$keys = array_keys($clause->possibilities);

$mixed_var_ids = array_diff($mixed_var_ids, $keys);

foreach ($keys as $key) {
Expand All @@ -161,7 +162,7 @@ public static function analyze(

// this will see whether any of the clauses in set A conflict with the clauses in set B
AlgebraAnalyzer::checkForParadox(
$context->clauses,
$entry_clauses,
$if_clauses,
$statements_analyzer,
$stmt->cond,
Expand All @@ -170,19 +171,17 @@ public static function analyze(

$if_clauses = Algebra::simplifyCNF($if_clauses);

$if_context_clauses = [...$entry_clauses, ...$if_clauses];

$if_context->clauses = $entry_clauses
? Algebra::simplifyCNF($if_context_clauses)
: $if_context_clauses;
? Algebra::simplifyCNF([...$entry_clauses, ...$if_clauses])
: $if_clauses;

if ($if_context->reconciled_expression_clauses) {
$reconciled_expression_clauses = $if_context->reconciled_expression_clauses;

$if_context->clauses = array_values(
array_filter(
$if_context->clauses,
static fn(Clause $c): bool => !in_array($c->hash, $reconciled_expression_clauses),
static fn(Clause $c): bool => !in_array($c->hash, $reconciled_expression_clauses, true),
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,6 @@ private static function updateArrayAssignmentChildType(

$from_countable_object_like = false;

$new_child_type = null;

$array_atomic_type = null;
if (!$current_dim && !$context->inside_loop) {
$atomic_root_types = $root_type->getAtomicTypes();
Expand Down Expand Up @@ -638,11 +636,6 @@ private static function updateArrayAssignmentChildType(
$array_atomic_type_array,
count($atomic_root_type_array->properties),
);
} elseif ($atomic_root_type_array->is_list) {
$array_atomic_type = $atomic_root_type_array;
$new_child_type = new Union([$array_atomic_type], [
'parent_nodes' => $root_type->parent_nodes,
]);
} else {
assert($array_atomic_type_list !== null);
$array_atomic_type = array_fill(
Expand Down Expand Up @@ -685,18 +678,16 @@ private static function updateArrayAssignmentChildType(

$array_assignment_type = new Union([$array_atomic_type]);

if (!$new_child_type) {
if ($templated_assignment) {
$new_child_type = $root_type;
} else {
$new_child_type = Type::combineUnionTypes(
$root_type,
$array_assignment_type,
$codebase,
true,
true,
);
}
if ($templated_assignment) {
$new_child_type = $root_type;
} else {
$new_child_type = Type::combineUnionTypes(
$root_type,
$array_assignment_type,
$codebase,
true,
true,
);
}

if ($from_countable_object_like) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static function (Clause $c) use ($mixed_var_ids, $cond_object_id): Clause {

// this will see whether any of the clauses in set A conflict with the clauses in set B
AlgebraAnalyzer::checkForParadox(
$context->clauses,
$entry_clauses,
$if_clauses,
$statements_analyzer,
$stmt->cond,
Expand All @@ -139,7 +139,7 @@ static function (Clause $c) use ($mixed_var_ids, $cond_object_id): Clause {
$ternary_context_clauses = array_values(
array_filter(
$ternary_context_clauses,
static fn(Clause $c): bool => !in_array($c->hash, $reconciled_expression_clauses),
static fn(Clause $c): bool => !in_array($c->hash, $reconciled_expression_clauses, true),
),
);

Expand Down
Loading