mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix #2048 - allow mixed array to be assigned specific string keys
This commit is contained in:
parent
853e92e7fc
commit
3b865f6509
@ -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) {
|
||||
|
@ -1072,6 +1072,20 @@ class ArrayAssignmentTest extends TestCase
|
||||
$c = new C();
|
||||
$c[] = "hello";',
|
||||
],
|
||||
'addToMixedArray' => [
|
||||
'<?php
|
||||
/**
|
||||
* @param array{key: string} $a
|
||||
*/
|
||||
function foo(array $a): void {
|
||||
echo $a["key"];
|
||||
}
|
||||
|
||||
function bar(array $arr) : void {
|
||||
$arr["key"] = "qqq";
|
||||
foo($arr);
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -1207,9 +1221,11 @@ class ArrayAssignmentTest extends TestCase
|
||||
];',
|
||||
'error_message' => 'DuplicateArrayKey',
|
||||
],
|
||||
'mixedArrayAssignment' => [
|
||||
'mixedArrayAssignmentOnVariable' => [
|
||||
'<?php
|
||||
$_GET["foo"][0] = "5";',
|
||||
function foo(array $arr) : void {
|
||||
$arr["foo"][0] = "5";
|
||||
}',
|
||||
'error_message' => 'MixedArrayAssignment',
|
||||
],
|
||||
'implementsArrayAccessPreventNullOffset' => [
|
||||
|
Loading…
Reference in New Issue
Block a user