Skip to content

Commit adfd2b6

Browse files
committed
prevent nested loops
1 parent c1ff9b4 commit adfd2b6

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Type/AcceptsResult.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,26 @@ public static function lazyMaxMin(
163163
): self
164164
{
165165
$results = [];
166+
$reasons = [];
167+
$hasNo = false;
166168
foreach ($objects as $object) {
167169
$isAcceptedBy = $callback($object);
168170
if ($isAcceptedBy->result->yes()) {
169171
return $isAcceptedBy;
172+
} elseif ($isAcceptedBy->result->no()) {
173+
$hasNo = true;
170174
}
171175
$results[] = $isAcceptedBy;
176+
177+
foreach ($isAcceptedBy->reasons as $reason) {
178+
$reasons[] = $reason;
179+
}
172180
}
173-
return self::maxMin(...$results);
181+
182+
return new self(
183+
$hasNo ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe(),
184+
array_values(array_unique($reasons)),
185+
);
174186
}
175187

176188
}

src/Type/IsSuperTypeOfResult.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,26 @@ public static function lazyMaxMin(
196196
): self
197197
{
198198
$results = [];
199+
$reasons = [];
200+
$hasNo = false;
199201
foreach ($objects as $object) {
200202
$isSuperTypeOf = $callback($object);
201203
if ($isSuperTypeOf->result->yes()) {
202204
return $isSuperTypeOf;
205+
} elseif ($isSuperTypeOf->result->no()) {
206+
$hasNo = true;
203207
}
204208
$results[] = $isSuperTypeOf;
209+
210+
foreach ($isSuperTypeOf->reasons as $reason) {
211+
$reasons[] = $reason;
212+
}
205213
}
206-
return self::maxMin(...$results);
214+
215+
return new self(
216+
$hasNo ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe(),
217+
array_values(array_unique($reasons)),
218+
);
207219
}
208220

209221
public function negate(): self

0 commit comments

Comments
 (0)