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
20 changes: 15 additions & 5 deletions src/Type/AcceptsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
final class AcceptsResult
{

private static self $YES;

private static self $MAYBE;

private static self $NO;

/**
* @api
* @param list<string> $reasons Human-readable explanations of why acceptance failed
Expand Down Expand Up @@ -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<string> $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
Expand Down Expand Up @@ -162,7 +174,6 @@ public static function lazyMaxMin(
callable $callback,
): self
{
$results = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice phpstan would detect local variables which are only written but never read

Copy link
Member

Choose a reason for hiding this comment

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

There are feature requests for that. It'd be very easy to implement too I think. We'd need virtual expr nodes like PropertyInitializationExpr to track things in scope.

$reasons = [];
$hasNo = false;
foreach ($objects as $object) {
Expand All @@ -172,7 +183,6 @@ public static function lazyMaxMin(
} elseif ($isAcceptedBy->result->no()) {
$hasNo = true;
}
$results[] = $isAcceptedBy;

foreach ($isAcceptedBy->reasons as $reason) {
$reasons[] = $reason;
Expand Down
2 changes: 0 additions & 2 deletions src/Type/IsSuperTypeOfResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ public static function lazyMaxMin(
callable $callback,
): self
{
$results = [];
$reasons = [];
$hasNo = false;
foreach ($objects as $object) {
Expand All @@ -205,7 +204,6 @@ public static function lazyMaxMin(
} elseif ($isSuperTypeOf->result->no()) {
$hasNo = true;
}
$results[] = $isSuperTypeOf;

foreach ($isSuperTypeOf->reasons as $reason) {
$reasons[] = $reason;
Expand Down
Loading