Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Type/AcceptsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,37 @@ public static function maxMin(self ...$operands): self
return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
}

/**
* @template T
* @param T[] $objects
* @param callable(T): self $callback
*/
public static function lazyMaxMin(
array $objects,
callable $callback,
): self
{
$results = [];
$reasons = [];
$hasNo = false;
foreach ($objects as $object) {
$isAcceptedBy = $callback($object);
if ($isAcceptedBy->result->yes()) {
return $isAcceptedBy;
} elseif ($isAcceptedBy->result->no()) {
$hasNo = true;
}
$results[] = $isAcceptedBy;

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

return new self(
$hasNo ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe(),
array_values(array_unique($reasons)),
);
}

}
12 changes: 10 additions & 2 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
return $otherType->isSuperTypeOf($this);
}

$result = IsSuperTypeOfResult::maxMin(...array_map(static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType), $this->types));
$result = IsSuperTypeOfResult::lazyMaxMin(
$this->types,
static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType),
);

if (
!$result->no()
&& $this->isOversizedArray()->yes()
Expand All @@ -280,7 +284,11 @@ public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult

public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
{
$result = AcceptsResult::maxMin(...array_map(static fn (Type $innerType) => $acceptingType->accepts($innerType, $strictTypes), $this->types));
$result = AcceptsResult::lazyMaxMin(
$this->types,
static fn (Type $innerType) => $acceptingType->accepts($innerType, $strictTypes),
);

if ($this->isOversizedArray()->yes()) {
if (!$result->no()) {
return AcceptsResult::createYes();
Expand Down
33 changes: 33 additions & 0 deletions src/Type/IsSuperTypeOfResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ public static function maxMin(self ...$operands): self
return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
}

/**
* @template T
* @param T[] $objects
* @param callable(T): self $callback
*/
public static function lazyMaxMin(
array $objects,
callable $callback,
): self
{
$results = [];
$reasons = [];
$hasNo = false;
foreach ($objects as $object) {
$isSuperTypeOf = $callback($object);
if ($isSuperTypeOf->result->yes()) {
return $isSuperTypeOf;
} elseif ($isSuperTypeOf->result->no()) {
$hasNo = true;
}
$results[] = $isSuperTypeOf;

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

return new self(
$hasNo ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe(),
array_values(array_unique($reasons)),
);
}

public function negate(): self
{
return new self($this->result->negate(), $this->reasons);
Expand Down
Loading