-
Notifications
You must be signed in to change notification settings - Fork 428
/
RoundFunctionReturnTypeExtension.php
88 lines (77 loc) · 2.29 KB
/
RoundFunctionReturnTypeExtension.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;
use function in_array;
class RoundFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function __construct(private PhpVersion $phpVersion)
{
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array(
$functionReflection->getName(),
[
'round',
'ceil',
'floor',
],
true,
);
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if ($this->phpVersion->hasStricterRoundFunctions()) {
// PHP 8 fatals with a missing parameter.
$noArgsReturnType = new NeverType(true);
// PHP 8 can either return a float or fatal.
$defaultReturnType = new BenevolentUnionType([
new FloatType(),
new NeverType(true),
]);
} else {
// PHP 7 returns null with a missing parameter.
$noArgsReturnType = new NullType();
// PHP 7 can return either a float or false.
$defaultReturnType = new BenevolentUnionType([
new FloatType(),
new ConstantBooleanType(false),
]);
}
if (count($functionCall->getArgs()) < 1) {
return $noArgsReturnType;
}
$firstArgType = $scope->getType($functionCall->getArgs()[0]->value);
if ($firstArgType instanceof MixedType) {
return $defaultReturnType;
}
if ($this->phpVersion->hasStricterRoundFunctions()) {
$allowed = TypeCombinator::union(
new IntegerType(),
new FloatType(),
);
if (!$allowed->accepts($firstArgType, true)->yes()) {
// PHP 8 fatals if the parameter is not an integer or float.
return new NeverType(true);
}
} elseif ($firstArgType->isArray()->yes()) {
// PHP 7 returns false if the parameter is an array.
return new ConstantBooleanType(false);
}
return new FloatType();
}
}