diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php index f41844aa9..bb28daf96 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php @@ -444,12 +444,14 @@ class ArrayFetchAnalyzer if ($in_assignment && $type instanceof TArray - && $type->type_params[0]->isEmpty() + && (($type->type_params[0]->isEmpty() && $type->type_params[1]->isEmpty()) + || ($type->type_params[1]->isMixed() && \is_string($key_value))) && $key_value !== null ) { + $from_mixed_array = $type->type_params[1]->isMixed(); // ok, type becomes an ObjectLike $array_type->removeType($type_string); - $type = new ObjectLike([$key_value => new Type\Union([new TEmpty])]); + $type = new ObjectLike([$key_value => $from_mixed_array ? Type::getMixed() : Type::getEmpty()]); $array_type->addType($type); } @@ -616,7 +618,7 @@ class ArrayFetchAnalyzer $array_access_type = Type::getMixed(); } else { - if (!$context->inside_isset || $type->sealed) { + if ($type->sealed) { $object_like_keys = array_keys($type->properties); if (count($object_like_keys) === 1) { diff --git a/tests/ArrayAssignmentTest.php b/tests/ArrayAssignmentTest.php index 4fa9323d2..7e5e145e0 100644 --- a/tests/ArrayAssignmentTest.php +++ b/tests/ArrayAssignmentTest.php @@ -1072,6 +1072,20 @@ class ArrayAssignmentTest extends TestCase $c = new C(); $c[] = "hello";', ], + 'addToMixedArray' => [ + ' 'DuplicateArrayKey', ], - 'mixedArrayAssignment' => [ + 'mixedArrayAssignmentOnVariable' => [ ' 'MixedArrayAssignment', ], 'implementsArrayAccessPreventNullOffset' => [