Skip to content

Commit 856a9f1

Browse files
committed
Fix BenevolentUnionType::traverseSimultaneously()
1 parent 0bddac1 commit 856a9f1

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

src/Type/BenevolentUnionType.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -151,31 +151,12 @@ public function traverse(callable $cb): Type
151151

152152
public function traverseSimultaneously(Type $right, callable $cb): Type
153153
{
154-
$types = [];
155-
$changed = false;
156-
157-
if (!$right instanceof UnionType) {
158-
return $this;
159-
}
160-
161-
if (count($this->getTypes()) !== count($right->getTypes())) {
154+
$newType = parent::traverseSimultaneously($right, $cb);
155+
if ($newType === $this) {
162156
return $this;
163157
}
164158

165-
foreach ($this->getSortedTypes() as $i => $leftType) {
166-
$rightType = $right->getSortedTypes()[$i];
167-
$newType = $cb($leftType, $rightType);
168-
if ($leftType !== $newType) {
169-
$changed = true;
170-
}
171-
$types[] = $newType;
172-
}
173-
174-
if ($changed) {
175-
return TypeUtils::toBenevolentUnion(TypeCombinator::union(...$types));
176-
}
177-
178-
return $this;
159+
return TypeUtils::toBenevolentUnion($newType);
179160
}
180161

181162
}

tests/PHPStan/Type/SimultaneousTypeTraverserTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static function dataChangeStringIntoNonEmptyString(): iterable
5757
new IntegerType(),
5858
new FloatType(),
5959
]),
60-
'(int|string)',
60+
'(int|non-empty-string)',
6161
];
6262

6363
yield [
@@ -104,6 +104,14 @@ public static function dataDescriptionBased(): iterable
104104

105105
return $right;
106106
};
107+
108+
yield [
109+
'__benevolent<object|int>',
110+
'1|2|3',
111+
$chooseScalarSubtype,
112+
'(1|2|3|object)',
113+
];
114+
107115
yield [
108116
'object|int',
109117
'1|2|3',

0 commit comments

Comments
 (0)