1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #3928 - preserve list-ness when assigning with no offset

This commit is contained in:
Matthew Brown 2020-08-03 12:30:58 -04:00
parent 6ed7a81146
commit 9dfdcbef31
2 changed files with 25 additions and 4 deletions

View File

@ -1057,10 +1057,16 @@ class ArrayFetchAnalyzer
$type->count = $property_count;
} else {
$array_type->removeType($type_string);
$type = new TArray([
$new_key_type,
$generic_params,
]);
if (!$stmt->dim && $type->is_list) {
$type = new TList($generic_params);
} else {
$type = new TArray([
$new_key_type,
$generic_params,
]);
}
$array_type->addType($type);
}

View File

@ -437,6 +437,21 @@ class WhileTest extends \Psalm\Tests\TestCase
}
}'
],
'assignToObjectLikeListPreserveListness' => [
'<?php
/**
* @return non-empty-list<string>
*/
function foo(string $key): array {
$elements = [$key];
while (rand(0, 1)) {
$elements[] = $key;
}
return $elements;
}',
]
];
}