mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Add checks for rounded values
This commit is contained in:
parent
b5059f45a0
commit
f33415827e
@ -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);
|
||||
|
||||
|
@ -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',
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user