From 3b865f650901fa31ba363c496f9bce949f4433d0 Mon Sep 17 00:00:00 2001 From: Brown Date: Tue, 27 Aug 2019 10:18:58 -0400 Subject: [PATCH] Fix #2048 - allow mixed array to be assigned specific string keys --- .../Expression/Fetch/ArrayFetchAnalyzer.php | 8 +++++--- tests/ArrayAssignmentTest.php | 20 +++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) 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' => [