Skip to content

Commit 075e0f5

Browse files
AcceptsResult: cache Yes/No/Maybe (#5095)
1 parent 1bbe9dc commit 075e0f5

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/Type/AcceptsResult.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
final class AcceptsResult
2929
{
3030

31+
private static self $YES;
32+
33+
private static self $MAYBE;
34+
35+
private static self $NO;
36+
3137
/**
3238
* @api
3339
* @param list<string> $reasons Human-readable explanations of why acceptance failed
@@ -68,23 +74,29 @@ public function no(): bool
6874

6975
public static function createYes(): self
7076
{
71-
return new self(TrinaryLogic::createYes(), []);
77+
return self::$YES ??= new self(TrinaryLogic::createYes(), []);
7278
}
7379

7480
/** @param list<string> $reasons */
7581
public static function createNo(array $reasons = []): self
7682
{
83+
if ($reasons === []) {
84+
return self::$NO ??= new self(TrinaryLogic::createNo(), $reasons);
85+
}
7786
return new self(TrinaryLogic::createNo(), $reasons);
7887
}
7988

8089
public static function createMaybe(): self
8190
{
82-
return new self(TrinaryLogic::createMaybe(), []);
91+
return self::$MAYBE ??= new self(TrinaryLogic::createMaybe(), []);
8392
}
8493

8594
public static function createFromBoolean(bool $value): self
8695
{
87-
return new self(TrinaryLogic::createFromBoolean($value), []);
96+
if ($value === true) {
97+
return self::createYes();
98+
}
99+
return self::createNo();
88100
}
89101

90102
public function and(self $other): self
@@ -162,7 +174,6 @@ public static function lazyMaxMin(
162174
callable $callback,
163175
): self
164176
{
165-
$results = [];
166177
$reasons = [];
167178
$hasNo = false;
168179
foreach ($objects as $object) {
@@ -172,7 +183,6 @@ public static function lazyMaxMin(
172183
} elseif ($isAcceptedBy->result->no()) {
173184
$hasNo = true;
174185
}
175-
$results[] = $isAcceptedBy;
176186

177187
foreach ($isAcceptedBy->reasons as $reason) {
178188
$reasons[] = $reason;

src/Type/IsSuperTypeOfResult.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ public static function lazyMaxMin(
195195
callable $callback,
196196
): self
197197
{
198-
$results = [];
199198
$reasons = [];
200199
$hasNo = false;
201200
foreach ($objects as $object) {
@@ -205,7 +204,6 @@ public static function lazyMaxMin(
205204
} elseif ($isSuperTypeOf->result->no()) {
206205
$hasNo = true;
207206
}
208-
$results[] = $isSuperTypeOf;
209207

210208
foreach ($isSuperTypeOf->reasons as $reason) {
211209
$reasons[] = $reason;

0 commit comments

Comments
 (0)