forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerbosityLevel.php
More file actions
284 lines (242 loc) · 7.17 KB
/
VerbosityLevel.php
File metadata and controls
284 lines (242 loc) · 7.17 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\GenericStaticType;
use PHPStan\Type\Generic\TemplateType;
/**
* Controls the verbosity of type descriptions in error messages.
*
* When PHPStan describes a type for an error message, it uses VerbosityLevel to
* decide how much detail to include. Higher levels include more detail like constant
* values and array shapes.
*
* The four levels (from least to most verbose):
* - **typeOnly**: Just the type name, e.g. "string", "array", "Foo"
* - **value**: Includes constant values, e.g. "'hello'", "array{foo: int}", "non-empty-string"
* - **precise**: Maximum detail — adds subtracted types on object/mixed (e.g. "object~Bar"),
* lowercase/uppercase string distinctions, untruncated array shapes, and template type scope
* - **cache**: Internal level used for generating cache keys
*
* Used as a parameter to Type::describe() to control output detail:
*
* $type->describe(VerbosityLevel::typeOnly()) // "string"
* $type->describe(VerbosityLevel::value()) // "'hello'"
* $type->describe(VerbosityLevel::precise()) // "non-empty-lowercase-string"
*
* The getRecommendedLevelByType() factory method automatically chooses the right level
* for error messages based on what types are involved — it picks the minimum verbosity
* needed to distinguish the accepting type from the accepted type.
*/
final class VerbosityLevel
{
private const TYPE_ONLY = 1;
private const VALUE = 2;
private const PRECISE = 3;
private const CACHE = 4;
/** @var self[] */
private static array $registry;
/**
* @param self::* $value
*/
private function __construct(private int $value)
{
}
/**
* @param self::* $value
*/
private static function create(int $value): self
{
self::$registry[$value] ??= new self($value);
return self::$registry[$value];
}
/** @return self::* */
public function getLevelValue(): int
{
return $this->value;
}
/** @api */
public static function typeOnly(): self
{
return self::create(self::TYPE_ONLY);
}
/** @api */
public static function value(): self
{
return self::create(self::VALUE);
}
/** @api */
public static function precise(): self
{
return self::create(self::PRECISE);
}
/**
* Internal level for generating unique cache keys — not for user-facing messages.
*
* @api
*/
public static function cache(): self
{
return self::create(self::CACHE);
}
public function isTypeOnly(): bool
{
return $this->value === self::TYPE_ONLY;
}
public function isValue(): bool
{
return $this->value === self::VALUE;
}
public function isPrecise(): bool
{
return $this->value === self::PRECISE;
}
public function isCache(): bool
{
return $this->value === self::CACHE;
}
/**
* Chooses the minimum verbosity needed to distinguish the two types in error messages.
*
* @api
*/
public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acceptedType = null): self
{
$moreVerbose = false;
$veryVerbose = false;
$moreVerboseCallback = static function (Type $type, callable $traverse) use (&$moreVerbose, &$veryVerbose): Type {
// stop deep traversal to not waste resources.
if ($veryVerbose) {
return $type;
}
if ($type->isCallable()->yes()) {
$moreVerbose = true;
// Keep checking if we need to be very verbose.
return $traverse($type);
}
if ($type->isConstantValue()->yes() && $type->isNull()->no()) {
$moreVerbose = true;
// For ConstantArrayType we need to keep checking if we need to be very verbose.
if (!$type->isArray()->no()) {
return $traverse($type);
}
return $type;
}
if (
// synced with IntersectionType::describe()
$type instanceof AccessoryNonEmptyStringType
|| $type instanceof AccessoryNonFalsyStringType
|| $type instanceof AccessoryLiteralStringType
|| $type instanceof AccessoryNumericStringType
|| $type instanceof NonEmptyArrayType
|| $type instanceof AccessoryArrayListType
) {
$moreVerbose = true;
return $type;
}
if (
$type instanceof AccessoryLowercaseStringType
|| $type instanceof AccessoryUppercaseStringType
) {
$moreVerbose = true;
$veryVerbose = true;
return $type;
}
if ($type instanceof IntegerRangeType) {
$moreVerbose = true;
return $type;
}
if ($type->isString()->yes() && $type->isClassString()->yes()) {
$moreVerbose = true;
return $type;
}
return $traverse($type);
};
TypeTraverser::map($acceptingType, $moreVerboseCallback);
if ($veryVerbose) {
return self::precise();
}
if ($moreVerbose) {
$verbosity = self::value();
}
if ($acceptedType === null) {
return $verbosity ?? self::typeOnly();
}
$containsInvariantTemplateType = false;
TypeTraverser::map($acceptingType, static function (Type $type, callable $traverse) use (&$containsInvariantTemplateType): Type {
// stop deep traversal to not waste resources.
if ($containsInvariantTemplateType) {
return $type;
}
if ($type instanceof GenericObjectType || $type instanceof GenericStaticType) {
$reflection = $type->getClassReflection();
if ($reflection !== null) {
$templateTypeMap = $reflection->getTemplateTypeMap();
foreach ($templateTypeMap->getTypes() as $templateType) {
if (!$templateType instanceof TemplateType) {
continue;
}
if (!$templateType->getVariance()->invariant()) {
continue;
}
$containsInvariantTemplateType = true;
return $type;
}
}
}
return $traverse($type);
});
if (!$containsInvariantTemplateType) {
return $verbosity ?? self::typeOnly();
}
/** @var bool $moreVerbose */
$moreVerbose = false;
/** @var bool $veryVerbose */
$veryVerbose = false;
TypeTraverser::map($acceptedType, $moreVerboseCallback);
if ($veryVerbose) {
return self::precise();
}
return $moreVerbose ? self::value() : $verbosity ?? self::typeOnly();
}
/**
* @param callable(): string $typeOnlyCallback
* @param callable(): string $valueCallback
* @param callable(): string|null $preciseCallback
* @param callable(): string|null $cacheCallback
*/
public function handle(
callable $typeOnlyCallback,
callable $valueCallback,
?callable $preciseCallback = null,
?callable $cacheCallback = null,
): string
{
if ($this->value === self::TYPE_ONLY) {
return $typeOnlyCallback();
}
if ($this->value === self::VALUE) {
return $valueCallback();
}
if ($this->value === self::PRECISE) {
if ($preciseCallback !== null) {
return $preciseCallback();
}
return $valueCallback();
}
if ($cacheCallback !== null) {
return $cacheCallback();
}
if ($preciseCallback !== null) {
return $preciseCallback();
}
return $valueCallback();
}
}