From 9c2ceb0f1ef7ab60153164014079abdd48a696f8 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 28 Feb 2026 14:24:22 +0100 Subject: [PATCH] AcceptsResult: cache Yes/No/Maybe --- src/Type/AcceptsResult.php | 20 +++++++++++++++----- src/Type/IsSuperTypeOfResult.php | 2 -- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Type/AcceptsResult.php b/src/Type/AcceptsResult.php index 87afd50dca..72b90d98a5 100644 --- a/src/Type/AcceptsResult.php +++ b/src/Type/AcceptsResult.php @@ -28,6 +28,12 @@ final class AcceptsResult { + private static self $YES; + + private static self $MAYBE; + + private static self $NO; + /** * @api * @param list $reasons Human-readable explanations of why acceptance failed @@ -68,23 +74,29 @@ public function no(): bool public static function createYes(): self { - return new self(TrinaryLogic::createYes(), []); + return self::$YES ??= new self(TrinaryLogic::createYes(), []); } /** @param list $reasons */ public static function createNo(array $reasons = []): self { + if ($reasons === []) { + return self::$NO ??= new self(TrinaryLogic::createNo(), $reasons); + } return new self(TrinaryLogic::createNo(), $reasons); } public static function createMaybe(): self { - return new self(TrinaryLogic::createMaybe(), []); + return self::$MAYBE ??= new self(TrinaryLogic::createMaybe(), []); } public static function createFromBoolean(bool $value): self { - return new self(TrinaryLogic::createFromBoolean($value), []); + if ($value === true) { + return self::createYes(); + } + return self::createNo(); } public function and(self $other): self @@ -162,7 +174,6 @@ public static function lazyMaxMin( callable $callback, ): self { - $results = []; $reasons = []; $hasNo = false; foreach ($objects as $object) { @@ -172,7 +183,6 @@ public static function lazyMaxMin( } elseif ($isAcceptedBy->result->no()) { $hasNo = true; } - $results[] = $isAcceptedBy; foreach ($isAcceptedBy->reasons as $reason) { $reasons[] = $reason; diff --git a/src/Type/IsSuperTypeOfResult.php b/src/Type/IsSuperTypeOfResult.php index 0c4fb92a50..dc1703bb51 100644 --- a/src/Type/IsSuperTypeOfResult.php +++ b/src/Type/IsSuperTypeOfResult.php @@ -195,7 +195,6 @@ public static function lazyMaxMin( callable $callback, ): self { - $results = []; $reasons = []; $hasNo = false; foreach ($objects as $object) { @@ -205,7 +204,6 @@ public static function lazyMaxMin( } elseif ($isSuperTypeOf->result->no()) { $hasNo = true; } - $results[] = $isSuperTypeOf; foreach ($isSuperTypeOf->reasons as $reason) { $reasons[] = $reason;