1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #2307 - allow setting of array string offset

This commit is contained in:
Matthew Brown 2019-11-05 19:03:59 -05:00
parent b81a2d3852
commit cbaf050bd2
2 changed files with 24 additions and 1 deletions

View File

@ -318,6 +318,7 @@ class ArrayAssignmentAnalyzer
if ($key_value !== null) {
$has_matching_objectlike_property = false;
$has_matching_string = false;
foreach ($child_stmt->inferredType->getTypes() as $type) {
if ($type instanceof ObjectLike) {
@ -327,9 +328,23 @@ class ArrayAssignmentAnalyzer
$type->properties[$key_value] = clone $current_type;
}
}
if ($type instanceof Type\Atomic\TString) {
$has_matching_string = true;
if ($type instanceof Type\Atomic\TLiteralString
&& $current_type->isSingleStringLiteral()
) {
$new_char = $current_type->getSingleStringLiteral()->value;
if (strlen($new_char) === 1) {
$type->value[0] = $new_char;
}
}
}
}
if (!$has_matching_objectlike_property) {
if (!$has_matching_objectlike_property && !$has_matching_string) {
$array_assignment_type = new Type\Union([
new ObjectLike([$key_value => $current_type]),
]);

View File

@ -1250,6 +1250,14 @@ class ArrayAssignmentTest extends TestCase
}
}'
],
'assignStringFirstChar' => [
'<?php
/** @param non-empty-list<string> $arr */
function foo(array $arr) : string {
$arr[0][0] = "a";
return $arr[0];
}'
],
];
}