-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathVersionCompareFunctionDynamicThrowTypeExtension.php
More file actions
60 lines (49 loc) · 1.42 KB
/
VersionCompareFunctionDynamicThrowTypeExtension.php
File metadata and controls
60 lines (49 loc) · 1.42 KB
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 PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\Type;
use function count;
use function in_array;
#[AutowiredService]
final class VersionCompareFunctionDynamicThrowTypeExtension implements DynamicFunctionThrowTypeExtension
{
public function __construct(
private PhpVersion $phpVersion,
)
{
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'version_compare';
}
public function getThrowTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $funcCall,
Scope $scope,
): ?Type
{
if (!$this->phpVersion->throwsValueErrorForInternalFunctions()) {
return null;
}
$args = $funcCall->getArgs();
if (!isset($args[2])) {
return null;
}
$operatorStrings = $scope->getType($args[2]->value)->getConstantStrings();
if (count($operatorStrings) === 0) {
return $functionReflection->getThrowType();
}
foreach ($operatorStrings as $operatorString) {
$operatorValue = $operatorString->getValue();
if (!in_array($operatorValue, VersionCompareHelper::VALID_OPERATORS, true)) {
return $functionReflection->getThrowType();
}
}
return null;
}
}