1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\ShouldNotHappenException;
6: use PHPStan\TrinaryLogic;
7: use PHPStan\Turbo\ShadowedByTurboExtension;
8: use function array_merge;
9: use function array_slice;
10: use function array_unique;
11: use function array_values;
12: use function count;
13:
14: /**
15: * Result of a Type::accepts() check — whether one type accepts another.
16: *
17: * Wraps a TrinaryLogic result together with human-readable reasons explaining
18: * why the acceptance failed. These reasons are surfaced in PHPStan error messages
19: * to help developers understand type mismatches.
20: *
21: * For example, when checking if `int` accepts `string`, the result would be No
22: * with a reason like "string is not a subtype of int".
23: *
24: * The `accepts()` method is used to check assignability — whether a value of one
25: * type can be assigned to a variable/parameter of another type. This is stricter
26: * than `isSuperTypeOf()` because it accounts for PHPStan's rule level and
27: * generics variance.
28: *
29: * @api
30: */
31: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/AcceptsResult.cpp')]
32: final class AcceptsResult
33: {
34:
35: /**
36: * Every member of a union that rejects a type adds its own reasons, so the
37: * reasons of nested unions multiply with every level they are nested in.
38: * Only the first ones are kept, which bounds both the work spent building
39: * them and the tip they end up in.
40: */
41: public const REASONS_LIMIT = 10;
42:
43: private static self $YES;
44:
45: private static self $MAYBE;
46:
47: private static self $NO;
48:
49: /**
50: * @api
51: * @param list<string> $reasons Human-readable explanations of why acceptance failed
52: */
53: public function __construct(
54: public readonly TrinaryLogic $result,
55: array $reasons,
56: )
57: {
58: $this->reasons = count($reasons) > self::REASONS_LIMIT ? array_slice($reasons, 0, self::REASONS_LIMIT) : $reasons;
59: }
60:
61: /** @var list<string> */
62: public readonly array $reasons;
63:
64: /**
65: * @phpstan-assert-if-true =false $this->no()
66: * @phpstan-assert-if-true =false $this->maybe()
67: */
68: public function yes(): bool
69: {
70: return $this->result->yes();
71: }
72:
73: /**
74: * @phpstan-assert-if-true =false $this->no()
75: * @phpstan-assert-if-true =false $this->yes()
76: */
77: public function maybe(): bool
78: {
79: return $this->result->maybe();
80: }
81:
82: /**
83: * @phpstan-assert-if-true =false $this->maybe()
84: * @phpstan-assert-if-true =false $this->yes()
85: */
86: public function no(): bool
87: {
88: return $this->result->no();
89: }
90:
91: public static function createYes(): self
92: {
93: return self::$YES ??= new self(TrinaryLogic::createYes(), []);
94: }
95:
96: /** @param list<string> $reasons */
97: public static function createNo(array $reasons = []): self
98: {
99: if ($reasons === []) {
100: return self::$NO ??= new self(TrinaryLogic::createNo(), $reasons);
101: }
102: return new self(TrinaryLogic::createNo(), $reasons);
103: }
104:
105: public static function createMaybe(): self
106: {
107: return self::$MAYBE ??= new self(TrinaryLogic::createMaybe(), []);
108: }
109:
110: public static function createFromBoolean(bool $value): self
111: {
112: if ($value === true) {
113: return self::createYes();
114: }
115: return self::createNo();
116: }
117:
118: public function and(self $other): self
119: {
120: return new self(
121: $this->result->and($other->result),
122: array_values(array_unique(array_merge($this->reasons, $other->reasons))),
123: );
124: }
125:
126: public function or(self $other): self
127: {
128: return new self(
129: $this->result->or($other->result),
130: array_values(array_unique(array_merge($this->reasons, $other->reasons))),
131: );
132: }
133:
134: /** @param callable(string): string $cb */
135: public function decorateReasons(callable $cb): self
136: {
137: $reasons = [];
138: foreach ($this->reasons as $reason) {
139: $reasons[] = $cb($reason);
140: }
141:
142: return new self($this->result, $reasons);
143: }
144:
145: /** @see TrinaryLogic::extremeIdentity() */
146: public static function extremeIdentity(self ...$operands): self
147: {
148: if ($operands === []) {
149: throw new ShouldNotHappenException();
150: }
151:
152: $results = [];
153: $reasons = [];
154: foreach ($operands as $operand) {
155: $results[] = $operand->result;
156: foreach ($operand->reasons as $reason) {
157: $reasons[] = $reason;
158: }
159: }
160:
161: return new self(TrinaryLogic::extremeIdentity(...$results), array_values(array_unique($reasons)));
162: }
163:
164: /** @see TrinaryLogic::maxMin() */
165: public static function maxMin(self ...$operands): self
166: {
167: if ($operands === []) {
168: throw new ShouldNotHappenException();
169: }
170:
171: $results = [];
172: $reasons = [];
173: foreach ($operands as $operand) {
174: $results[] = $operand->result;
175: foreach ($operand->reasons as $reason) {
176: $reasons[] = $reason;
177: }
178: }
179:
180: return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
181: }
182:
183: /**
184: * @template T
185: * @param T[] $objects
186: * @param callable(T): self $callback
187: */
188: public static function lazyMaxMin(
189: array $objects,
190: callable $callback,
191: ): self
192: {
193: $reasons = [];
194: $hasNo = false;
195: foreach ($objects as $object) {
196: $isAcceptedBy = $callback($object);
197: if ($isAcceptedBy->result->yes()) {
198: return $isAcceptedBy;
199: } elseif ($isAcceptedBy->result->no()) {
200: $hasNo = true;
201: }
202:
203: foreach ($isAcceptedBy->reasons as $reason) {
204: $reasons[] = $reason;
205: }
206: }
207:
208: return new self(
209: $hasNo ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe(),
210: array_values(array_unique($reasons)),
211: );
212: }
213:
214: }
215: