diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseIfAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseIfAnalyzer.php index 00c7895c2be..a5383894227 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseIfAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfElse/ElseIfAnalyzer.php @@ -92,6 +92,10 @@ public static function analyze( $codebase, ); + if (count($elseif_clauses) > 200) { + $elseif_clauses = []; + } + $elseif_clauses_handled = []; foreach ($elseif_clauses as $clause) { @@ -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; } } @@ -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; } } @@ -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 { diff --git a/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php index b4648cd59f2..8d6367f6549 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Block/IfElseAnalyzer.php @@ -94,6 +94,8 @@ public static function analyze( } } + $branch_point = $context->branch_point ?: (int) $stmt->getAttribute('startFilePos'); + try { $if_conditional_scope = IfConditionalAnalyzer::analyze( $statements_analyzer, @@ -101,7 +103,7 @@ public static function analyze( $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 @@ -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) { @@ -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, @@ -170,11 +171,9 @@ 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; @@ -182,7 +181,7 @@ public static function analyze( $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) ), ); diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php index 01150233a66..6361cc00ba5 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php @@ -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(); @@ -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( @@ -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) { diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php index 7524c523baa..be4dd5aab43 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/TernaryAnalyzer.php @@ -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, @@ -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) ), );