-
Notifications
You must be signed in to change notification settings - Fork 570
Expand file tree
/
Copy pathMissingTypehintCheck.php
More file actions
209 lines (188 loc) · 6.01 KB
/
MissingTypehintCheck.php
File metadata and controls
209 lines (188 loc) · 6.01 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php declare(strict_types = 1);
namespace PHPStan\Rules;
use Closure;
use Generator;
use Iterator;
use IteratorAggregate;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\CallableType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\ConditionalType;
use PHPStan\Type\ConditionalTypeForParameter;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\GenericStaticType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\GenericTypeAliasType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use Traversable;
use function array_filter;
use function array_keys;
use function array_merge;
use function array_unique;
use function count;
use function implode;
use function in_array;
use function sprintf;
use function strtolower;
#[AutowiredService]
final class MissingTypehintCheck
{
public const MISSING_ITERABLE_VALUE_TYPE_TIP = 'See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type';
private const ITERABLE_GENERIC_CLASS_NAMES = [
Traversable::class,
Iterator::class,
IteratorAggregate::class,
Generator::class,
];
/**
* @param string[] $skipCheckGenericClasses
*/
public function __construct(
#[AutowiredParameter]
private bool $checkMissingCallableSignature,
#[AutowiredParameter(ref: '%featureToggles.skipCheckGenericClasses%')]
private array $skipCheckGenericClasses,
#[AutowiredParameter(ref: '%featureToggles.checkGenericIterableClasses%')]
private bool $checkGenericIterableClasses,
)
{
}
/**
* @return Type[]
*/
public function getIterableTypesWithMissingValueTypehint(Type $type): array
{
$iterablesWithMissingValueTypehint = [];
TypeTraverser::map($type, function (Type $type, callable $traverse) use (&$iterablesWithMissingValueTypehint): Type {
if ($type instanceof TemplateType) {
return $type;
}
if ($type instanceof AccessoryType) {
return $type;
}
if ($type instanceof ConditionalType || $type instanceof ConditionalTypeForParameter) {
$iterablesWithMissingValueTypehint = array_merge(
$iterablesWithMissingValueTypehint,
$this->getIterableTypesWithMissingValueTypehint($type->getIf()),
$this->getIterableTypesWithMissingValueTypehint($type->getElse()),
);
return $type;
}
if ($type->isIterable()->yes()) {
$iterableValue = $type->getIterableValueType();
if ($iterableValue instanceof MixedType && !$iterableValue->isExplicitMixed()) {
$iterablesWithMissingValueTypehint[] = $type;
}
if ($type instanceof IntersectionType) {
if ($type->isList()->yes()) {
return $traverse($iterableValue);
}
return $type;
}
}
return $traverse($type);
});
return $iterablesWithMissingValueTypehint;
}
/**
* @return array<int, array{string, string}>
*/
public function getNonGenericObjectTypesWithGenericClass(Type $type): array
{
$objectTypes = [];
TypeTraverser::map($type, function (Type $type, callable $traverse) use (&$objectTypes): Type {
if ($type instanceof GenericObjectType || $type instanceof GenericStaticType) {
$traverse($type);
return $type;
}
if ($type instanceof TemplateType) {
return $type;
}
if ($type instanceof GenericTypeAliasType) {
$missing = $type->getMissingRequiredParamNames();
if ($missing !== []) {
$objectTypes[] = [
sprintf('type alias %s', $type->getAliasName()),
implode(', ', array_unique($missing)),
];
}
return $traverse($type);
}
if ($type instanceof ObjectType) {
$classReflection = $type->getClassReflection();
if ($classReflection === null) {
return $type;
}
if (
$classReflection->getName() === Traversable::class // already covered by getIterableTypesWithMissingValueTypehint
|| (
!$this->checkGenericIterableClasses &&
in_array($classReflection->getName(), self::ITERABLE_GENERIC_CLASS_NAMES, true)
)
) {
// checked by getIterableTypesWithMissingValueTypehint() already
return $type;
}
if (in_array($classReflection->getName(), $this->skipCheckGenericClasses, true)) {
return $type;
}
if ($classReflection->isTrait()) {
return $type;
}
if (!$classReflection->isGeneric()) {
return $type;
}
$resolvedType = TemplateTypeHelper::resolveToBounds($type);
if (!$resolvedType instanceof ObjectType) {
throw new ShouldNotHappenException();
}
$templateTypes = $classReflection->getTemplateTypeMap()->getTypes();
$templateTypesCount = count($templateTypes);
$requiredTemplateTypesCount = count(array_filter($templateTypes, static fn (Type $type) => $type instanceof TemplateType && $type->getDefault() === null));
if ($requiredTemplateTypesCount === 0) {
return $type;
}
$templateTypesList = implode(', ', array_keys($templateTypes));
if ($requiredTemplateTypesCount !== $templateTypesCount) {
$templateTypesList .= sprintf(' (%d-%d required)', $requiredTemplateTypesCount, $templateTypesCount);
}
$objectTypes[] = [
sprintf('%s %s', strtolower($classReflection->getClassTypeDescription()), $classReflection->getDisplayName(false)),
$templateTypesList,
];
return $type;
}
return $traverse($type);
});
return $objectTypes;
}
/**
* @return Type[]
*/
public function getCallablesWithMissingSignature(Type $type): array
{
if (!$this->checkMissingCallableSignature) {
return [];
}
$result = [];
TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$result): Type {
if (
($type instanceof CallableType && $type->isCommonCallable())
|| ($type instanceof ClosureType && $type->isCommonCallable())
|| ($type instanceof ObjectType && $type->getClassName() === Closure::class)
) {
$result[] = $type;
}
return $traverse($type);
});
return $result;
}
}