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

Fix #830 - unpack template param args appropriately

This commit is contained in:
Matt Brown 2018-06-20 10:40:50 -04:00
parent 4688cf7a92
commit 740279352e
3 changed files with 54 additions and 1 deletions

View File

@ -829,11 +829,28 @@ class CallChecker
$generic_params = [];
}
$arg_type = $arg->value->inferredType;
if ($arg->unpack) {
if ($arg->value->inferredType->hasArray()) {
/** @var Type\Atomic\TArray|Type\Atomic\ObjectLike */
$array_atomic_type = $arg->value->inferredType->getTypes()['array'];
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
$array_atomic_type = $array_atomic_type->getGenericArrayType();
}
$arg_type = $array_atomic_type->type_params[1];
} else {
$arg_type = Type::getMixed();
}
}
$param_type->replaceTemplateTypesWithStandins(
$template_types,
$generic_params,
$codebase,
$arg->value->inferredType
$arg_type
);
}
}

View File

@ -701,6 +701,18 @@ class FunctionCallTest extends TestCase
["foo" => "bar"]
);'
],
'splatArrayIntersect' => [
'<?php
$foo = [
[1, 2, 3],
[1, 2],
];
$bar = array_intersect(... $foo);',
'assertions' => [
'$bar' => 'array<int, int>',
],
],
];
}

View File

@ -684,6 +684,30 @@ class TemplateTest extends TestCase
function(Collection $elt): bool { return true; }
);',
],
'splatTemplateParam' => [
'<?php
/**
* @template TKey
* @template TValue
*
* @param array<TKey, TValue> $arr
* @param array $arr2
* @return array<TKey, TValue>
*/
function splat_proof(array $arr, array $arr2) {
return $arr;
}
$foo = [
[1, 2, 3],
[1, 2],
];
$a = splat_proof(... $foo);',
'assertions' => [
'$a' => 'array<int, int>',
],
],
];
}