-
Notifications
You must be signed in to change notification settings - Fork 650
/
RoundReturnTypeProvider.php
60 lines (48 loc) · 1.81 KB
/
RoundReturnTypeProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php declare(strict_types=1);
namespace Psalm\Internal\Provider\ReturnTypeProvider;
use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent;
use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface;
use Psalm\Type;
use function array_values;
use function count;
use function round;
use const PHP_ROUND_HALF_UP;
class RoundReturnTypeProvider implements FunctionReturnTypeProviderInterface
{
/**
* @return array<lowercase-string>
*/
public static function getFunctionIds(): array
{
return ['round'];
}
public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union
{
$call_args = $event->getCallArgs();
if (count($call_args) === 0) {
return null;
}
$statements_source = $event->getStatementsSource();
$nodeTypeProvider = $statements_source->getNodeTypeProvider();
$num_arg = $nodeTypeProvider->getType($call_args[0]->value);
if (count($call_args) > 1) {
$precision_val = (int)$call_args[1]->value;
} else {
$precision_val = 0;
}
if (count($call_args) > 2) {
/** @var positive-int|0 $mode_val */
$mode_val = (int)$call_args[2]->value;
} else {
$mode_val = PHP_ROUND_HALF_UP;
}
if ($num_arg !== null && $num_arg->isSingle()) {
$num_type = array_values($num_arg->getAtomicTypes())[0];
if ($num_type instanceof Type\Atomic\TLiteralFloat || $num_type instanceof Type\Atomic\TLiteralInt) {
$rounded_val = round($num_type->value, $precision_val, $mode_val);
return new Type\Union([new Type\Atomic\TLiteralFloat($rounded_val)]);
}
}
return new Type\Union([new Type\Atomic\TFloat()]);
}
}