2828final 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 ;
0 commit comments