-
Notifications
You must be signed in to change notification settings - Fork 432
/
GetDebugTypeFunctionReturnTypeExtension.php
89 lines (76 loc) · 2.49 KB
/
GetDebugTypeFunctionReturnTypeExtension.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
89
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use Closure;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use function array_map;
use function count;
class GetDebugTypeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'get_debug_type';
}
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
$argType = $scope->getType($functionCall->getArgs()[0]->value);
if ($argType instanceof UnionType) {
return new UnionType(array_map(Closure::fromCallable([self::class, 'resolveOneType']), $argType->getTypes()));
}
return self::resolveOneType($argType);
}
/**
* @see https://www.php.net/manual/en/function.get-debug-type.php#refsect1-function.get-debug-type-returnvalues
*/
private static function resolveOneType(Type $type): Type
{
if ($type->isNull()->yes()) {
return new ConstantStringType('null');
}
if ($type->isBoolean()->yes()) {
return new ConstantStringType('bool');
}
if ($type->isInteger()->yes()) {
return new ConstantStringType('int');
}
if ($type->isFloat()->yes()) {
return new ConstantStringType('float');
}
if ($type->isString()->yes()) {
return new ConstantStringType('string');
}
if ($type->isArray()->yes()) {
return new ConstantStringType('array');
}
// "resources" type+state is skipped since we cannot infer the state
if ($type->isObject()->yes()) {
$classNames = $type->getObjectClassNames();
$reflections = $type->getObjectClassReflections();
$types = [];
foreach ($classNames as $index => $className) {
// if the class is not final, the actual returned string might be of a child class
if ($reflections[$index]->isFinal() && !$reflections[$index]->isAnonymous()) {
$types[] = new ConstantStringType($className);
}
if ($reflections[$index]->isAnonymous()) { // phpcs:ignore
$types[] = new ConstantStringType('class@anonymous');
}
}
switch (count($types)) {
case 0:
return new StringType();
case 1:
return $types[0];
default:
return new UnionType($types);
}
}
return new StringType();
}
}