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

Don’t give up when faced with variadic type

This commit is contained in:
Matthew Brown 2017-01-17 00:07:58 -05:00
parent b9bad01dbd
commit e4769fd04a
2 changed files with 41 additions and 4 deletions

View File

@ -1082,13 +1082,29 @@ class CallChecker
$has_packed_var = $has_packed_var || $arg->unpack;
}
$last_param = $function_params
? $function_params[count($function_params) - 1]
: null;
foreach ($args as $argument_offset => $arg) {
if ($function_params !== null && isset($arg->value->inferredType)) {
if (count($function_params) > $argument_offset) {
$param_type = $function_params[$argument_offset]->type;
$function_param = count($function_params) > $argument_offset
? $function_params[$argument_offset]
: ($last_param && $last_param->is_variadic ? $last_param : null);
// for now stop when we encounter a variadic param pr a packed argument
if ($function_params[$argument_offset]->is_variadic || $arg->unpack) {
if ($function_param) {
$param_type = $function_param->type;
if ($function_param->is_variadic) {
if (!$param_type->hasArray() || !$param_type->types['array'] instanceof TArray) {
break;
}
$param_type = clone $param_type->types['array']->type_params[1];
}
// for now stop when we encounter a packed argument
if ($arg->unpack) {
break;
}

View File

@ -142,6 +142,27 @@ class Php56Test extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidScalarArgument
* @return void
*/
public function testVariadicArrayBadParam()
{
$stmts = self::$parser->parse('<?php
/**
* @return void
*/
function f(int ...$a_list) {
}
f(1, 2, "3");
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$context = new Context();
$file_checker->visitAndAnalyzeMethods($context);
}
/**
* @return void
*/