forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcceptsResult.php
More file actions
176 lines (153 loc) · 4.03 KB
/
AcceptsResult.php
File metadata and controls
176 lines (153 loc) · 4.03 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
<?php declare(strict_types = 1);
namespace PHPStan\Type;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use function array_merge;
use function array_unique;
use function array_values;
/**
* Result of a Type::accepts() check — whether one type accepts another.
*
* Wraps a TrinaryLogic result together with human-readable reasons explaining
* why the acceptance failed. These reasons are surfaced in PHPStan error messages
* to help developers understand type mismatches.
*
* For example, when checking if `int` accepts `string`, the result would be No
* with a reason like "string is not a subtype of int".
*
* The `accepts()` method is used to check assignability — whether a value of one
* type can be assigned to a variable/parameter of another type. This is stricter
* than `isSuperTypeOf()` because it accounts for PHPStan's rule level and
* generics variance.
*
* @api
*/
final class AcceptsResult
{
/**
* @api
* @param list<string> $reasons Human-readable explanations of why acceptance failed
*/
public function __construct(
public readonly TrinaryLogic $result,
public readonly array $reasons,
)
{
}
/**
* @phpstan-assert-if-true =false $this->no()
* @phpstan-assert-if-true =false $this->maybe()
*/
public function yes(): bool
{
return $this->result->yes();
}
/**
* @phpstan-assert-if-true =false $this->no()
* @phpstan-assert-if-true =false $this->yes()
*/
public function maybe(): bool
{
return $this->result->maybe();
}
/**
* @phpstan-assert-if-true =false $this->maybe()
* @phpstan-assert-if-true =false $this->yes()
*/
public function no(): bool
{
return $this->result->no();
}
public static function createYes(): self
{
return new self(TrinaryLogic::createYes(), []);
}
/** @param list<string> $reasons */
public static function createNo(array $reasons = []): self
{
return new self(TrinaryLogic::createNo(), $reasons);
}
public static function createMaybe(): self
{
return new self(TrinaryLogic::createMaybe(), []);
}
public static function createFromBoolean(bool $value): self
{
return new self(TrinaryLogic::createFromBoolean($value), []);
}
public function and(self $other): self
{
return new self(
$this->result->and($other->result),
array_values(array_unique(array_merge($this->reasons, $other->reasons))),
);
}
public function or(self $other): self
{
return new self(
$this->result->or($other->result),
array_values(array_unique(array_merge($this->reasons, $other->reasons))),
);
}
/** @param callable(string): string $cb */
public function decorateReasons(callable $cb): self
{
$reasons = [];
foreach ($this->reasons as $reason) {
$reasons[] = $cb($reason);
}
return new self($this->result, $reasons);
}
/** @see TrinaryLogic::extremeIdentity() */
public static function extremeIdentity(self ...$operands): self
{
if ($operands === []) {
throw new ShouldNotHappenException();
}
$results = [];
$reasons = [];
foreach ($operands as $operand) {
$results[] = $operand->result;
foreach ($operand->reasons as $reason) {
$reasons[] = $reason;
}
}
return new self(TrinaryLogic::extremeIdentity(...$results), array_values(array_unique($reasons)));
}
/** @see TrinaryLogic::maxMin() */
public static function maxMin(self ...$operands): self
{
if ($operands === []) {
throw new ShouldNotHappenException();
}
$results = [];
$reasons = [];
foreach ($operands as $operand) {
$results[] = $operand->result;
foreach ($operand->reasons as $reason) {
$reasons[] = $reason;
}
}
return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
}
/**
* @template T
* @param T[] $objects
* @param callable(T): self $callback
*/
public static function lazyMaxMin(
array $objects,
callable $callback,
): self
{
$results = [];
foreach ($objects as $object) {
$isAcceptedBy = $callback($object);
if ($isAcceptedBy->result->yes()) {
return $isAcceptedBy;
}
$results[] = $isAcceptedBy;
}
return self::maxMin(...$results);
}
}