1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +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; $type->count = $property_count;
} else { } else {
$array_type->removeType($type_string); $array_type->removeType($type_string);
if (!$stmt->dim && $type->is_list) {
$type = new TList($generic_params);
} else {
$type = new TArray([ $type = new TArray([
$new_key_type, $new_key_type,
$generic_params, $generic_params,
]); ]);
}
$array_type->addType($type); $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;
}',
]
]; ];
} }