Skip to content
Permalink
Browse files

Fix #2048 - allow mixed array to be assigned specific string keys

  • Loading branch information...
muglug committed Aug 27, 2019
1 parent 853e92e commit 3b865f650901fa31ba363c496f9bce949f4433d0
@@ -444,12 +444,14 @@ public static function getArrayAccessTypeGivenOffset(
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 @@ public static function getArrayAccessTypeGivenOffset(
$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 @@ public function offsetUnset(int $offset) : void { }
$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 @@ public function bar() {
];',
'error_message' => 'DuplicateArrayKey',
],
'mixedArrayAssignment' => [
'mixedArrayAssignmentOnVariable' => [
'<?php
$_GET["foo"][0] = "5";',
function foo(array $arr) : void {
$arr["foo"][0] = "5";
}',
'error_message' => 'MixedArrayAssignment',
],
'implementsArrayAccessPreventNullOffset' => [

0 comments on commit 3b865f6

Please sign in to comment.
You can’t perform that action at this time.