Skip to content

Commit 15509bf

Browse files
Try to improve traverseSimultaneously
1 parent 69bbbe0 commit 15509bf

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

src/Type/IntersectionType.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,13 +1260,17 @@ public function traverse(callable $cb): Type
12601260

12611261
public function traverseSimultaneously(Type $right, callable $cb): Type
12621262
{
1263-
if ($this->isArray()->yes() && $right->isArray()->yes()) {
1263+
if ($this->isArray()->yes() && !$right->isArray()->no()) {
1264+
$rightArray = $right->isArray()->maybe()
1265+
? TypeCombinator::intersect($right, new ArrayType(new MixedType(), new MixedType()))
1266+
: $right;
1267+
12641268
$changed = false;
12651269
$newTypes = [];
12661270

12671271
foreach ($this->types as $innerType) {
1268-
$newKeyType = $cb($innerType->getIterableKeyType(), $right->getIterableKeyType());
1269-
$newValueType = $cb($innerType->getIterableValueType(), $right->getIterableValueType());
1272+
$newKeyType = $cb($innerType->getIterableKeyType(), $rightArray->getIterableKeyType());
1273+
$newValueType = $cb($innerType->getIterableValueType(), $rightArray->getIterableValueType());
12701274
if ($newKeyType === $innerType->getIterableKeyType() && $newValueType === $innerType->getIterableValueType()) {
12711275
$newTypes[] = $innerType;
12721276
continue;

src/Type/ObjectType.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,11 +1690,21 @@ public function traverse(callable $cb): Type
16901690

16911691
public function traverseSimultaneously(Type $right, callable $cb): Type
16921692
{
1693-
if ($this->subtractedType === null) {
1693+
if (!$right instanceof SubtractableType) {
1694+
return $this;
1695+
}
1696+
1697+
$rightSubtractable = $right->getSubtractedType();
1698+
if ($this->subtractedType === null || $rightSubtractable === null) {
16941699
return $this;
16951700
}
16961701

1697-
return new self($this->className);
1702+
$newSubtractedType = $cb($this->subtractedType, $rightSubtractable);
1703+
if ($newSubtractedType !== $this->subtractedType) {
1704+
return new self($newSubtractedType);
1705+
}
1706+
1707+
return $this;
16981708
}
16991709

17001710
public function getNakedClassReflection(): ?ClassReflection

src/Type/ObjectWithoutClassType.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,21 @@ public function traverse(callable $cb): Type
170170

171171
public function traverseSimultaneously(Type $right, callable $cb): Type
172172
{
173-
if ($this->subtractedType === null) {
173+
if (!$right instanceof SubtractableType) {
174174
return $this;
175175
}
176176

177-
return new self();
177+
$rightSubtractable = $right->getSubtractedType();
178+
if ($this->subtractedType === null || $rightSubtractable === null) {
179+
return $this;
180+
}
181+
182+
$newSubtractedType = $cb($this->subtractedType, $rightSubtractable);
183+
if ($newSubtractedType !== $this->subtractedType) {
184+
return new self($newSubtractedType);
185+
}
186+
187+
return $this;
178188
}
179189

180190
public function tryRemove(Type $typeToRemove): ?Type

src/Type/StaticType.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,11 +745,21 @@ public function traverse(callable $cb): Type
745745

746746
public function traverseSimultaneously(Type $right, callable $cb): Type
747747
{
748-
if ($this->subtractedType === null) {
748+
if (!$right instanceof SubtractableType) {
749749
return $this;
750750
}
751751

752-
return new self($this->classReflection);
752+
$rightSubtractable = $right->getSubtractedType();
753+
if ($this->subtractedType === null || $rightSubtractable === null) {
754+
return $this;
755+
}
756+
757+
$newSubtractedType = $cb($this->subtractedType, $rightSubtractable);
758+
if ($newSubtractedType !== $this->subtractedType) {
759+
return new self($this->classReflection, $newSubtractedType);
760+
}
761+
762+
return $this;
753763
}
754764

755765
public function subtract(Type $type): Type

0 commit comments

Comments
 (0)