diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php index c09997e5f..35102f8e2 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Assignment/ArrayAssignmentAnalyzer.php @@ -222,12 +222,12 @@ class ArrayAssignmentAnalyzer $child_stmt->var->inferredType = clone $context->vars_in_scope[$parent_var_id]; } - $parent_var_id = $array_var_id; + $array_type = clone $child_stmt->var->inferredType; $child_stmt->inferredType = ArrayFetchAnalyzer::getArrayAccessTypeGivenOffset( $statements_analyzer, $child_stmt, - $child_stmt->var->inferredType, + $array_type, isset($child_stmt->dim->inferredType) ? $child_stmt->dim->inferredType : Type::getInt(), true, $array_var_id, @@ -236,6 +236,19 @@ class ArrayAssignmentAnalyzer $child_stmts ? null : $assignment_type ); + $child_stmt->var->inferredType = $array_type; + + if ($root_var_id) { + if (!$parent_var_id) { + $rooted_parent_id = $root_var_id; + $root_type = $array_type; + } else { + $rooted_parent_id = $parent_var_id; + } + + $context->vars_in_scope[$rooted_parent_id] = $array_type; + } + if (!$child_stmts) { $child_stmt->inferredType = $assignment_type; } @@ -243,6 +256,8 @@ class ArrayAssignmentAnalyzer $current_type = $child_stmt->inferredType; $current_dim = $child_stmt->dim; + $parent_var_id = $array_var_id; + if ($child_stmt->var->inferredType->hasMixed()) { $full_var_id = false; break; diff --git a/src/Psalm/Internal/Type/TypeCombination.php b/src/Psalm/Internal/Type/TypeCombination.php index 5bd808150..ec1d5a09a 100644 --- a/src/Psalm/Internal/Type/TypeCombination.php +++ b/src/Psalm/Internal/Type/TypeCombination.php @@ -765,7 +765,9 @@ class TypeCombination ); } - unset($possibly_undefined_entries[$candidate_property_name]); + if (!$type->had_mixed_value) { + unset($possibly_undefined_entries[$candidate_property_name]); + } } if ($combination->array_counts !== null) { diff --git a/tests/ArrayAssignmentTest.php b/tests/ArrayAssignmentTest.php index 7e5e145e0..812c1533f 100644 --- a/tests/ArrayAssignmentTest.php +++ b/tests/ArrayAssignmentTest.php @@ -1086,6 +1086,20 @@ class ArrayAssignmentTest extends TestCase foo($arr); }' ], + 'checkEmptinessAfterConditionalArrayAdjustment' => [ + 'arr["a"] = "hello"; + } + + if (!$this->arr) {} + } + }' + ], ]; }