-
Notifications
You must be signed in to change notification settings - Fork 428
/
TemplateTypeHelper.php
63 lines (51 loc) · 1.38 KB
/
TemplateTypeHelper.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
<?php declare(strict_types = 1);
namespace PHPStan\Type\Generic;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
class TemplateTypeHelper
{
/**
* Replaces template types with standin types
*/
public static function resolveTemplateTypes(Type $type, TemplateTypeMap $standins): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($standins): Type {
if ($type instanceof TemplateType && !$type->isArgument()) {
$newType = $standins->getType($type->getName());
if ($newType === null) {
return $traverse($type);
}
if ($newType instanceof ErrorType) {
return $traverse($type->getBound());
}
return $newType;
}
return $traverse($type);
});
}
public static function resolveToBounds(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof TemplateType) {
return $traverse($type->getBound());
}
return $traverse($type);
});
}
/**
* @template T of Type
* @param T $type
* @return T
*/
public static function toArgument(Type $type): Type
{
/** @var T */
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof TemplateType) {
return $traverse($type->toArgument());
}
return $traverse($type);
});
}
}