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

Add checks for rounded values

This commit is contained in:
Brown 2019-01-28 11:57:49 -05:00
parent b5059f45a0
commit f33415827e
2 changed files with 38 additions and 0 deletions

View File

@ -624,6 +624,26 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
break;
case 'round':
if (isset($call_args[1])) {
$second_arg = $call_args[1]->value;
if (isset($second_arg->inferredType)
&& $second_arg->inferredType->isSingleIntLiteral()
) {
switch ($second_arg->inferredType->getSingleIntLiteral()->value) {
case 0:
return Type::getInt(true);
default:
return Type::getFloat();
}
}
return new Type\Union([new Type\Atomic\TInt, new Type\Atomic\TFloat]);
}
return Type::getInt(true);
case 'filter_var':
return self::getFilterVar($call_args);

View File

@ -1386,6 +1386,24 @@ class FunctionCallTest extends TestCase
foo();
}',
],
'round' => [
'<?php
$a = round(4.6);
$b = round(3.6, 0);
$c = round(3.0, 1);
$d = round(3.1, 2);
/** @var int */
$sig = 1;
$e = round(3.1, $sig);',
'assertions' => [
'$a' => 'int',
'$b' => 'int',
'$c' => 'float',
'$d' => 'float',
'$e' => 'int|float',
],
]
];
}