1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Improve unpacking

This commit is contained in:
Brown 2020-07-22 00:35:18 -04:00
parent 962265e98e
commit 983f233026
2 changed files with 30 additions and 4 deletions

View File

@ -217,6 +217,14 @@ class ArrayFunctionArgumentsAnalyzer
}
$array_type = $array_type->getGenericArrayType();
if ($objectlike_list) {
if ($array_type instanceof TNonEmptyArray) {
$array_type = new TNonEmptyList($array_type->type_params[1]);
} else {
$array_type = new TList($array_type->type_params[1]);
}
}
}
$by_ref_type = new Type\Union([clone $array_type]);
@ -242,9 +250,29 @@ class ArrayFunctionArgumentsAnalyzer
new Type\Union([new TArray([Type::getInt(), Type::getMixed()])])
);
} elseif ($arg->unpack) {
$arg_value_type = clone $arg_value_type;
foreach ($arg_value_type->getAtomicTypes() as $arg_value_atomic_type) {
if ($arg_value_atomic_type instanceof ObjectLike) {
$was_list = $arg_value_atomic_type->is_list;
$arg_value_atomic_type = $arg_value_atomic_type->getGenericArrayType();
if ($was_list) {
if ($arg_value_atomic_type instanceof TNonEmptyArray) {
$arg_value_atomic_type = new TNonEmptyList($arg_value_atomic_type->type_params[1]);
} else {
$arg_value_atomic_type = new TList($arg_value_atomic_type->type_params[1]);
}
}
$arg_value_type->addType($arg_value_atomic_type);
}
}
$by_ref_type = Type::combineUnionTypes(
$by_ref_type,
clone $arg_value_type
$arg_value_type
);
} else {
if ($objectlike_list) {

View File

@ -87,11 +87,9 @@ class Php56Test extends TestCase
],
'arrayPushArgumentUnpackingWithGoodArg' => [
'<?php
$a = [];
$a = ["foo"];
$b = ["foo", "bar"];
$a[] = "foo";
array_push($a, ...$b);',
'assertions' => [
'$a' => 'non-empty-list<string>',