Repository navigation
Expand file tree
/
Copy pathTypehintHelper.php
More file actions
182 lines (159 loc) · 5.6 KB
/
Copy pathTypehintHelper.php
File metadata and controls
182 lines (159 loc) · 5.6 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?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\Turbo\ShadowedByTurboExtension;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use ReflectionType;
use Traversable;
use function array_map;
use function count;
use function get_class;
use function sprintf;
#[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/TypehintHelper.cpp')]
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 ($phpDocType instanceof ClassConstantAccessType && !$phpDocType->isResolvable()) {
// static::FOO_* or T::* - which constants it stands for is known only for the class the
// method is called on, and cannot be compared with the native type before that
return $phpDocType->withNativeType($type);
}
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(),
);
}
}
$resolvedPhpDocTypeToBounds = TemplateTypeHelper::resolveToBounds($phpDocType);
$isSuperType = $type->isSuperTypeOf($resolvedPhpDocTypeToBounds);
if (
!$isSuperType->yes()
&& (new ObjectType(Traversable::class))->isSuperTypeOf($phpDocType)->yes()
&& $type instanceof TypeWithClassName
) {
// if native type is Iterator and PHPDoc type is Traversable
// Allow PHPDoc type to win
$traversableAncestor = $type->getAncestorWithClassName(Traversable::class);
if ($traversableAncestor !== null) {
$isSuperType = $traversableAncestor->isSuperTypeOf($resolvedPhpDocTypeToBounds);
}
}
if (
($type->isCallable()->yes() && $phpDocType->isCallable()->yes())
|| (
(!$phpDocType instanceof NeverType || ($type instanceof MixedType && !$type->isExplicitMixed()))
&& $isSuperType->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;
}
}