Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 21 additions & 0 deletions src/Type/AcceptsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,25 @@ 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 = [];
foreach ($objects as $object) {
$isAcceptedBy = $callback($object);
if ($isAcceptedBy->result->yes()) {
return $isAcceptedBy;
}
$results[] = $isAcceptedBy;
}
return self::maxMin(...$results);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This add another foreach.
Looking at TrinaryLogic::lazyMaxMin it does not call self::maxMin.

Should we implement it this way

public static function lazyMaxMin(
    array $objects,
    callable $callback,
): self
{
    if ($objects === []) {
        throw new ShouldNotHappenException();
    }

    $results = [];
    $reasons = [];
    foreach ($objects as $object) {
        $isAcceptedBy = $callback($object);
        if ($isAcceptedBy->result->yes()) {
            return $isAcceptedBy;
        }
        $results[] = $isAcceptedBy->result;
        foreach ($isAcceptedBy->reasons as $reason) {
            $reasons[] = $reason;
        }
    }

    return new self(TrinaryLogic::maxMin(...$results), array_values(array_unique($reasons)));
}

instead ?

(I also wonder if we should -- now or later -- avoid calling TrinaryLogic::maxMin to remove an extra foreach and doing it in the existing one)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok - unrolled all nested loops now

}

}
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
21 changes: 21 additions & 0 deletions src/Type/IsSuperTypeOfResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ 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 = [];
foreach ($objects as $object) {
$isSuperTypeOf = $callback($object);
if ($isSuperTypeOf->result->yes()) {
return $isSuperTypeOf;
}
$results[] = $isSuperTypeOf;
}
return self::maxMin(...$results);
}

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