forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
VerbosityLevel.php
188 lines (155 loc) · 4.2 KB
/
VerbosityLevel.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
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
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Generic\TemplateType;
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;
private function __construct(private int $value)
{
}
private static function create(int $value): self
{
self::$registry[$value] ??= new self($value);
return self::$registry[$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);
}
/** @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;
}
/** @api */
public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acceptedType = null): self
{
$moreVerboseCallback = static function (Type $type, callable $traverse) use (&$moreVerbose): Type {
if ($type->isCallable()->yes()) {
$moreVerbose = true;
return $type;
}
if ($type instanceof ConstantType && !$type instanceof NullType) {
$moreVerbose = true;
return $type;
}
if (
// synced with IntersectionType::describe()
$type instanceof AccessoryNonEmptyStringType
|| $type instanceof AccessoryLiteralStringType
|| $type instanceof AccessoryNumericStringType
|| $type instanceof NonEmptyArrayType
) {
$moreVerbose = true;
return $type;
}
if ($type instanceof IntegerRangeType) {
$moreVerbose = true;
return $type;
}
return $traverse($type);
};
/** @var bool $moreVerbose */
$moreVerbose = false;
TypeTraverser::map($acceptingType, $moreVerboseCallback);
if ($moreVerbose) {
return self::value();
}
if ($acceptedType === null) {
return self::typeOnly();
}
$containsInvariantTemplateType = false;
TypeTraverser::map($acceptingType, static function (Type $type, callable $traverse) use (&$containsInvariantTemplateType): Type {
if ($type instanceof GenericObjectType) {
$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 self::typeOnly();
}
/** @var bool $moreVerbose */
$moreVerbose = false;
TypeTraverser::map($acceptedType, $moreVerboseCallback);
return $moreVerbose ? self::value() : 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 ($this->value === self::CACHE) {
if ($cacheCallback !== null) {
return $cacheCallback();
}
if ($preciseCallback !== null) {
return $preciseCallback();
}
return $valueCallback();
}
throw new ShouldNotHappenException();
}
}