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

Migrate min/max function calls back to CallMap

This commit is contained in:
Brown 2020-05-26 12:28:56 -04:00
parent a266d4748b
commit ecb179c784
3 changed files with 33 additions and 43 deletions

View File

@ -172,6 +172,39 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
$int->from_calculation = true; $int->from_calculation = true;
return $int; return $int;
case 'min':
case 'max':
if (isset($call_args[0])) {
$first_arg = $call_args[0]->value;
if ($first_arg_type = $statements_analyzer->node_data->getType($first_arg)) {
if ($first_arg_type->hasArray()) {
/** @psalm-suppress PossiblyUndefinedStringArrayOffset */
$array_type = $first_arg_type->getAtomicTypes()['array'];
if ($array_type instanceof Type\Atomic\ObjectLike) {
return $array_type->getGenericValueType();
}
if ($array_type instanceof Type\Atomic\TArray) {
return clone $array_type->type_params[1];
}
if ($array_type instanceof Type\Atomic\TList) {
return clone $array_type->type_param;
}
} elseif ($first_arg_type->hasScalarType()
&& isset($call_args[1])
&& ($second_arg = $call_args[1]->value)
&& ($second_arg_type = $statements_analyzer->node_data->getType($second_arg))
&& $second_arg_type->hasScalarType()
) {
return Type::combineUnionTypes($first_arg_type, $second_arg_type);
}
}
}
break;
case 'get_parent_class': case 'get_parent_class':
// this is unreliable, as it's hard to know exactly what's wanted - attempted this in // this is unreliable, as it's hard to know exactly what's wanted - attempted this in
// https://github.com/vimeo/psalm/commit/355ed831e1c69c96bbf9bf2654ef64786cbe9fd7 // https://github.com/vimeo/psalm/commit/355ed831e1c69c96bbf9bf2654ef64786cbe9fd7

View File

@ -1412,23 +1412,6 @@ class FunctionCallAnalyzer extends CallAnalyzer
} }
} }
} }
} elseif (($function_id === 'min' || $function_id === 'max')
&& count($stmt->args) === 1
&& $first_arg
) {
$first_arg_type = $statements_analyzer->node_data->getType($first_arg->value);
if ($first_arg_type && $first_arg_type->hasScalarType()) {
if (IssueBuffer::accepts(
new \Psalm\Issue\TooFewArguments(
$function_id . ' must have more than one argument unless an array is passed',
new CodeLocation($statements_analyzer, $function_name)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
}
} }
} }
} }

View File

@ -455,32 +455,6 @@ function sscanf(string $str, string $format, &...$vars) {}
*/ */
function pathinfo(string $path, int $options = \PATHINFO_DIRNAME) {} function pathinfo(string $path, int $options = \PATHINFO_DIRNAME) {}
/**
* @psalm-pure
*
* @template T as string|int|float
* @template TFirstArg as array<T>|T
*
* @param TFirstArg $arg_1
* @param T $arg_2
*
* @return T
*/
function min($arg_1, ...$arg_2) {}
/**
* @psalm-pure
*
* @template T as string|int|float
* @template TFirstArg as array<T>|T
*
* @param TFirstArg $arg_1
* @param T $arg_2
*
* @return T
*/
function max($arg_1, ...$arg_2) {}
/** /**
* @psalm-pure * @psalm-pure
* *