Skip to content

Commit 29af204

Browse files
Rework
1 parent 9f6d5b7 commit 29af204

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/Type/IterableType.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ private function isNestedTypeSuperTypeOf(Type $a, Type $b): IsSuperTypeOfResult
136136
public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
137137
{
138138
if ($otherType instanceof IntersectionType || $otherType instanceof UnionType) {
139-
return $otherType->isSuperTypeOf(new UnionType([
140-
$this->toArray(),
141-
$this->toTraversable(),
142-
]));
139+
return $otherType->isSuperTypeOf($this->toArrayOrTraversable());
143140
}
144141

145142
if ($otherType instanceof self) {
@@ -228,6 +225,17 @@ public function toArray(): Type
228225
return new ArrayType($this->keyType, $this->getItemType());
229226
}
230227

228+
public function toArrayOrTraversable(): Type
229+
{
230+
return new UnionType([
231+
new ArrayType($this->keyType, $this->itemType),
232+
new GenericObjectType(Traversable::class, [
233+
$this->keyType,
234+
$this->itemType,
235+
]),
236+
]);
237+
}
238+
231239
public function toArrayKey(): Type
232240
{
233241
return new ErrorType();
@@ -247,7 +255,10 @@ public function toCoercedArgumentType(bool $strictTypes): Type
247255
),
248256
$this->itemType,
249257
),
250-
$this->toTraversable(),
258+
new GenericObjectType(Traversable::class, [
259+
$this->keyType,
260+
$this->itemType,
261+
]),
251262
);
252263
}
253264

@@ -475,12 +486,15 @@ public function tryRemove(Type $typeToRemove): ?Type
475486
{
476487
$arrayType = new ArrayType(new MixedType(), new MixedType());
477488
if ($typeToRemove->isSuperTypeOf($arrayType)->yes()) {
478-
return $this->toTraversable();
489+
return new GenericObjectType(Traversable::class, [
490+
$this->keyType,
491+
$this->itemType,
492+
]);
479493
}
480494

481495
$traversableType = new ObjectType(Traversable::class);
482496
if ($typeToRemove->isSuperTypeOf($traversableType)->yes()) {
483-
return $this->toArray();
497+
return new ArrayType($this->getIterableKeyType(), $this->getIterableValueType());
484498
}
485499

486500
return null;
@@ -528,12 +542,4 @@ public function hasTemplateOrLateResolvableType(): bool
528542
return $this->keyType->hasTemplateOrLateResolvableType() || $this->itemType->hasTemplateOrLateResolvableType();
529543
}
530544

531-
public function toTraversable(): Type
532-
{
533-
return new GenericObjectType(Traversable::class, [
534-
$this->keyType,
535-
$this->itemType,
536-
]);
537-
}
538-
539545
}

src/Type/UnionType.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,7 @@ public function getConstantStrings(): array
195195
public function accepts(Type $type, bool $strictTypes): AcceptsResult
196196
{
197197
if ($type instanceof IterableType) {
198-
return $this->accepts(new UnionType([
199-
$type->toArray(),
200-
$type->toTraversable(),
201-
]), $strictTypes);
198+
return $this->accepts($type->toArrayOrTraversable(), $strictTypes);
202199
}
203200

204201
foreach (self::EQUAL_UNION_CLASSES as $baseClass => $classes) {
@@ -1081,10 +1078,7 @@ public function toCoercedArgumentType(bool $strictTypes): Type
10811078
public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
10821079
{
10831080
if ($receivedType instanceof IterableType) {
1084-
$receivedType = new UnionType([
1085-
$receivedType->toArray(),
1086-
$receivedType->toTraversable(),
1087-
]);
1081+
$receivedType = $receivedType->toArrayOrTraversable();
10881082
}
10891083

10901084
$types = TemplateTypeMap::createEmpty();

0 commit comments

Comments
 (0)