1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Convert a bunch of custom-handled functions to conditional stubbed ones

This commit is contained in:
Brown 2020-04-07 16:48:29 -04:00
parent 550ba0a4c9
commit 69045daea9
3 changed files with 45 additions and 89 deletions

View File

@ -160,54 +160,6 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
break;
case 'var_export':
case 'highlight_string':
case 'highlight_file':
if (isset($call_args[1])
&& ($second_arg_type = $statements_analyzer->node_data->getType($call_args[1]->value))
) {
if ((string) $second_arg_type === 'true') {
return Type::getString();
}
return new Type\Union([
new Type\Atomic\TString,
$call_map_key === 'var_export' ? new Type\Atomic\TNull : new Type\Atomic\TBool
]);
}
return $call_map_key === 'var_export' ? Type::getVoid() : Type::getBool();
case 'print_r':
if (isset($call_args[1])
&& ($second_arg_type = $statements_analyzer->node_data->getType($call_args[1]->value))
) {
if ((string) $second_arg_type === 'true') {
return Type::getString();
}
}
return new Type\Union([
new Type\Atomic\TString,
new Type\Atomic\TTrue
]);
case 'microtime':
if (($first_arg_type = $statements_analyzer->node_data->getType($call_args[0]->value))) {
if ((string) $first_arg_type === 'true') {
return Type::getFloat();
}
if ((string) $first_arg_type === 'false') {
return Type::getString();
}
}
return new Type\Union([
new Type\Atomic\TFloat,
new Type\Atomic\TString
]);
case 'hrtime':
if (($first_arg_type = $statements_analyzer->node_data->getType($call_args[0]->value))) {
if ((string) $first_arg_type === 'true') {
@ -241,45 +193,6 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
case 'getenv':
return new Type\Union([new Type\Atomic\TString, new Type\Atomic\TFalse]);
case 'gettimeofday':
if (($first_arg_type = $statements_analyzer->node_data->getType($call_args[0]->value))) {
if ((string) $first_arg_type === 'true') {
return Type::getFloat();
}
if ((string) $first_arg_type === 'false') {
return new Type\Union([
new Type\Atomic\TArray([
Type::getString(),
Type::getInt()
])
]);
}
}
break;
case 'abs':
if (isset($call_args[0]->value)) {
$first_arg = $call_args[0]->value;
if ($first_arg_type = $statements_analyzer->node_data->getType($first_arg)) {
$numeric_types = [];
foreach ($first_arg_type->getAtomicTypes() as $inner_type) {
if ($inner_type->isNumericType()) {
$numeric_types[] = $inner_type;
}
}
if ($numeric_types) {
return new Type\Union($numeric_types);
}
}
}
break;
case 'min':
case 'max':
if (isset($call_args[0])) {
@ -333,6 +246,7 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
return Type::getInt(true);
case 'get_parent_class':
// this is unreliable, as it's hard to know exactly what's wanted - attempted this in
// https://github.com/vimeo/psalm/commit/355ed831e1c69c96bbf9bf2654ef64786cbe9fd7

View File

@ -327,3 +327,45 @@ function fclose(&$handle) : bool
function sodium_memzero(string &$reference): void
{
}
/**
* @param mixed $var
* @param bool $return
* @return ($return is true ? string : void)
*/
function var_export($var, bool $return = false) {}
/**
* @param mixed $var
* @param bool $return
* @return ($return is true ? string : true)
*/
function print_r($var, bool $return = false) {}
/**
* @param mixed $var
* @return ($return is true ? string : bool)
*/
function highlight_file($var, bool $return = false) {}
/**
* @param mixed $var
* @return ($return is true ? string : bool)
*/
function highlight_string($var, bool $return = false) {}
/**
* @return ($get_as_float is true ? float : string)
*/
function microtime(bool $get_as_float = false) {}
/**
* @return ($return_float is true ? float : array<string, int>)
*/
function gettimeofday(bool $return_float = false) {}
/**
* @param numeric $number
* @return ($number is int ? int : ($number is float ? float : int|float))
*/
function abs($number) {}

View File

@ -44,7 +44,7 @@ class FunctionCallTest extends TestCase
'assertions' => [
'$a' => 'int',
'$b' => 'float',
'$c' => 'null|numeric',
'$c' => 'float|int|null',
],
'error_levels' => ['MixedAssignment', 'MixedArgument'],
],