forked from vimeo/psalm
/
RoundReturnTypeProvider.php
57 lines (45 loc) · 1.68 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
<?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;
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 = $call_args[1]->value;
} else {
$precision_val = 0;
}
if ($num_arg !== null && $num_arg->isSingle()) {
$num_type = array_values($num_arg->getAtomicTypes())[0];
if ($num_type instanceof Type\Atomic\TFloat) {
if ($precision_val > 0) {
return new Type\Union([new Type\Atomic\TFloat()]);
}
return new Type\Union([new Type\Atomic\TInt()]);
}
if ($num_type instanceof Type\Atomic\TInt) {
return new Type\Union([new Type\Atomic\TInt()]);
}
}
return new Type\Union([new Type\Atomic\TInt(), new Type\Atomic\TFloat()]);
}
}