-
Notifications
You must be signed in to change notification settings - Fork 563
Expand file tree
/
Copy pathTypehintHelper.php
More file actions
159 lines (137 loc) · 4.61 KB
/
TypehintHelper.php
File metadata and controls
159 lines (137 loc) · 4.61 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionIntersectionType;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionNamedType;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionUnionType;
use PHPStan\Reflection\ClassReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use ReflectionType;
use function array_map;
use function count;
use function get_class;
use function sprintf;
final class TypehintHelper
{
/** @api */
public static function decideTypeFromReflection(
?ReflectionType $reflectionType,
?Type $phpDocType = null,
ClassReflection|null $selfClass = null,
bool $isVariadic = false,
): Type
{
if ($reflectionType === null) {
if ($isVariadic && ($phpDocType instanceof ArrayType || $phpDocType instanceof ConstantArrayType)) {
$phpDocType = $phpDocType->getItemType();
}
return $phpDocType ?? new MixedType();
}
if ($reflectionType instanceof ReflectionUnionType) {
$type = TypeCombinator::union(...array_map(static fn (ReflectionType $type): Type => self::decideTypeFromReflection($type, selfClass: $selfClass), $reflectionType->getTypes()));
return self::decideType($type, $phpDocType);
}
if ($reflectionType instanceof ReflectionIntersectionType) {
$types = [];
foreach ($reflectionType->getTypes() as $innerReflectionType) {
$innerType = self::decideTypeFromReflection($innerReflectionType, selfClass: $selfClass);
if (!$innerType->isObject()->yes()) {
return new NeverType();
}
$types[] = $innerType;
}
return self::decideType(TypeCombinator::intersect(...$types), $phpDocType);
}
if (!$reflectionType instanceof ReflectionNamedType) {
throw new ShouldNotHappenException(sprintf('Unexpected type: %s', get_class($reflectionType)));
}
if ($reflectionType->isIdentifier()) {
$typeNode = new Identifier($reflectionType->getName());
} else {
$typeNode = new FullyQualified($reflectionType->getName());
}
$type = ParserNodeTypeToPHPStanType::resolve($typeNode, $selfClass);
if ($reflectionType->allowsNull()) {
$type = TypeCombinator::addNull($type);
}
return self::decideType($type, $phpDocType);
}
public static function decideType(
Type $type,
?Type $phpDocType,
): Type
{
if ($phpDocType !== null && $type->isNull()->no()) {
$phpDocType = TypeCombinator::removeNull($phpDocType);
}
if ($type instanceof BenevolentUnionType) {
return $type;
}
if ($phpDocType !== null && !$phpDocType instanceof ErrorType) {
if ($phpDocType instanceof NeverType && $phpDocType->isExplicit()) {
return $phpDocType;
}
if (
$type instanceof MixedType
&& !$type->isExplicitMixed()
&& $phpDocType->isVoid()->yes()
) {
return $phpDocType;
}
if (TypeCombinator::removeNull($type) instanceof IterableType) {
if ($phpDocType instanceof UnionType) {
$innerTypes = [];
foreach ($phpDocType->getTypes() as $innerType) {
if ($innerType instanceof ArrayType && $innerType->getKeyType()->describe(VerbosityLevel::typeOnly()) === 'mixed') {
$innerTypes[] = new IterableType(
$innerType->getIterableKeyType(),
$innerType->getItemType(),
);
} else {
$innerTypes[] = $innerType;
}
}
$phpDocType = new UnionType($innerTypes);
} elseif ($phpDocType instanceof ArrayType && $phpDocType->getKeyType()->describe(VerbosityLevel::typeOnly()) === 'mixed') {
$phpDocType = new IterableType(
$phpDocType->getKeyType(),
$phpDocType->getItemType(),
);
}
}
if (
($type->isCallable()->yes() && $phpDocType->isCallable()->yes())
|| (
(!$phpDocType instanceof NeverType || ($type instanceof MixedType && !$type->isExplicitMixed()))
&& $type->isSuperTypeOf(TypeUtils::resolveLateResolvableTypes(TemplateTypeHelper::resolveToBounds($phpDocType)))->yes()
)
) {
$resultType = $phpDocType;
} else {
$resultType = $type;
}
if ($type instanceof UnionType) {
$addToUnionTypes = [];
foreach ($type->getTypes() as $innerType) {
if (!$innerType->isSuperTypeOf($resultType)->no()) {
continue;
}
$addToUnionTypes[] = $innerType;
}
if (count($addToUnionTypes) > 0) {
$type = TypeCombinator::union($resultType, ...$addToUnionTypes);
} else {
$type = $resultType;
}
} elseif (TypeCombinator::containsNull($type)) {
$type = TypeCombinator::addNull($resultType);
} else {
$type = $resultType;
}
}
return $type;
}
}