Skip to content

Commit 2d4be1d

Browse files
authored
Cache IntersectionType results (#4836)
1 parent 861cd9a commit 2d4be1d

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

src/Type/IntersectionType.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ class IntersectionType implements CompoundType
6969

7070
private bool $sortedTypes = false;
7171

72+
private ?TrinaryLogic $isBoolean = null;
73+
74+
private ?TrinaryLogic $isFloat = null;
75+
76+
private ?TrinaryLogic $isInteger = null;
77+
78+
private ?TrinaryLogic $isString = null;
79+
80+
private ?TrinaryLogic $isArray = null;
81+
82+
private ?TrinaryLogic $isList = null;
83+
84+
private ?TrinaryLogic $isConstantArray = null;
85+
86+
private ?TrinaryLogic $isOversizedArray = null;
87+
88+
private ?TrinaryLogic $isIterableAtLeastOnce = null;
89+
90+
private ?TrinaryLogic $isConstantScalarValue = null;
91+
7292
/**
7393
* @api
7494
* @param list<Type> $types
@@ -675,7 +695,7 @@ public function isIterable(): TrinaryLogic
675695

676696
public function isIterableAtLeastOnce(): TrinaryLogic
677697
{
678-
return $this->intersectResults(
698+
return $this->isIterableAtLeastOnce ??= $this->intersectResults(
679699
static fn (Type $type): TrinaryLogic => $type->isIterableAtLeastOnce(),
680700
static fn (Type $type): bool => !$type->isIterable()->no(),
681701
);
@@ -733,27 +753,27 @@ public function getLastIterableValueType(): Type
733753

734754
public function isArray(): TrinaryLogic
735755
{
736-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isArray());
756+
return $this->isArray ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isArray());
737757
}
738758

739759
public function isConstantArray(): TrinaryLogic
740760
{
741-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isConstantArray());
761+
return $this->isConstantArray ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isConstantArray());
742762
}
743763

744764
public function isOversizedArray(): TrinaryLogic
745765
{
746-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isOversizedArray());
766+
return $this->isOversizedArray ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isOversizedArray());
747767
}
748768

749769
public function isList(): TrinaryLogic
750770
{
751-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isList());
771+
return $this->isList ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isList());
752772
}
753773

754774
public function isString(): TrinaryLogic
755775
{
756-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isString());
776+
return $this->isString ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isString());
757777
}
758778

759779
public function isNumericString(): TrinaryLogic
@@ -1095,7 +1115,7 @@ public function isConstantValue(): TrinaryLogic
10951115

10961116
public function isConstantScalarValue(): TrinaryLogic
10971117
{
1098-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isConstantScalarValue());
1118+
return $this->isConstantScalarValue ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isConstantScalarValue());
10991119
}
11001120

11011121
public function getConstantScalarTypes(): array
@@ -1134,17 +1154,17 @@ public function isFalse(): TrinaryLogic
11341154

11351155
public function isBoolean(): TrinaryLogic
11361156
{
1137-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isBoolean());
1157+
return $this->isBoolean ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isBoolean());
11381158
}
11391159

11401160
public function isFloat(): TrinaryLogic
11411161
{
1142-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isFloat());
1162+
return $this->isFloat ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isFloat());
11431163
}
11441164

11451165
public function isInteger(): TrinaryLogic
11461166
{
1147-
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isInteger());
1167+
return $this->isInteger ??= $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isInteger());
11481168
}
11491169

11501170
public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic

0 commit comments

Comments
 (0)