Skip to content

Commit c1ff9b4

Browse files
committed
refactor
1 parent f6d99ee commit c1ff9b4

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

src/Type/AcceptsResult.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,25 @@ public static function maxMin(self ...$operands): self
152152
return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
153153
}
154154

155+
/**
156+
* @template T
157+
* @param T[] $objects
158+
* @param callable(T): self $callback
159+
*/
160+
public static function lazyMaxMin(
161+
array $objects,
162+
callable $callback,
163+
): self
164+
{
165+
$results = [];
166+
foreach ($objects as $object) {
167+
$isAcceptedBy = $callback($object);
168+
if ($isAcceptedBy->result->yes()) {
169+
return $isAcceptedBy;
170+
}
171+
$results[] = $isAcceptedBy;
172+
}
173+
return self::maxMin(...$results);
174+
}
175+
155176
}

src/Type/IntersectionType.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,10 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
266266
return $otherType->isSuperTypeOf($this);
267267
}
268268

269-
$results = [];
270-
foreach ($this->types as $innerType) {
271-
$isSuperTypeOf = $otherType->isSuperTypeOf($innerType);
272-
if ($isSuperTypeOf->yes()) {
273-
return IsSuperTypeOfResult::createYes();
274-
}
275-
$results[] = $isSuperTypeOf;
276-
}
277-
$result = IsSuperTypeOfResult::maxMin(...$results);
269+
$result = IsSuperTypeOfResult::lazyMaxMin(
270+
$this->types,
271+
static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType),
272+
);
278273

279274
if (
280275
!$result->no()
@@ -289,15 +284,10 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
289284

290285
public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
291286
{
292-
$results = [];
293-
foreach ($this->types as $innerType) {
294-
$isAcceptedBy = $acceptingType->accepts($innerType, $strictTypes);
295-
if ($isAcceptedBy->yes()) {
296-
return AcceptsResult::createYes();
297-
}
298-
$results[] = $isAcceptedBy;
299-
}
300-
$result = AcceptsResult::maxMin(...$results);
287+
$result = AcceptsResult::lazyMaxMin(
288+
$this->types,
289+
static fn (Type $innerType) => $acceptingType->accepts($innerType, $strictTypes),
290+
);
301291

302292
if ($this->isOversizedArray()->yes()) {
303293
if (!$result->no()) {

src/Type/IsSuperTypeOfResult.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,27 @@ public static function maxMin(self ...$operands): self
185185
return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
186186
}
187187

188+
/**
189+
* @template T
190+
* @param T[] $objects
191+
* @param callable(T): self $callback
192+
*/
193+
public static function lazyMaxMin(
194+
array $objects,
195+
callable $callback,
196+
): self
197+
{
198+
$results = [];
199+
foreach ($objects as $object) {
200+
$isSuperTypeOf = $callback($object);
201+
if ($isSuperTypeOf->result->yes()) {
202+
return $isSuperTypeOf;
203+
}
204+
$results[] = $isSuperTypeOf;
205+
}
206+
return self::maxMin(...$results);
207+
}
208+
188209
public function negate(): self
189210
{
190211
return new self($this->result->negate(), $this->reasons);

0 commit comments

Comments
 (0)