Skip to content

Commit c3caa6c

Browse files
committed
Fix unions of TemplateType child classes
1 parent ff7311a commit c3caa6c

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

src/Type/Generic/TemplateMixedType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
101101
->and(TrinaryLogic::createMaybe());
102102
}
103103

104+
public function isSuperTypeOfMixed(MixedType $type): TrinaryLogic
105+
{
106+
return $this->isSuperTypeOf($type);
107+
}
108+
104109
public function isSubTypeOf(Type $type): TrinaryLogic
105110
{
106111
if ($type instanceof UnionType || $type instanceof IntersectionType) {

src/Type/Generic/TemplateObjectWithoutClassType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function isSubTypeOf(Type $type): TrinaryLogic
169169
return $type->isSuperTypeOf($this);
170170
}
171171

172-
if ($type instanceof ObjectWithoutClassType) {
172+
if ($type instanceof ObjectWithoutClassType && !$type instanceof self) {
173173
return TrinaryLogic::createYes();
174174
}
175175

tests/PHPStan/Rules/Generators/data/yield.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,32 @@ function iterator_map(iterable $iterator, callable $callback): iterable
9090
}
9191
}
9292

93+
/**
94+
* @template T
95+
* @template K
96+
* @param iterable<K, T> $hash
97+
* @return \Generator<int, K|T, void, void>
98+
*/
99+
function hash_to_alt_sequence(iterable $hash): iterable
100+
{
101+
foreach ($hash as $k => $v) {
102+
yield $k;
103+
yield $v;
104+
}
105+
}
106+
107+
/**
108+
* @template T
109+
* @template K
110+
* @param iterable<K, T> $hash
111+
* @return iterable<int, K|T>
112+
*/
113+
function hash_to_alt_sequence2(iterable $hash): iterable
114+
{
115+
foreach ($hash as $k => $v) {
116+
yield $k;
117+
yield $v;
118+
}
119+
}
120+
93121
}

tests/PHPStan/Type/Generic/GenericClassStringTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public function dataAccepts(): array
234234
new ObjectWithoutClassType(),
235235
TemplateTypeVariance::createInvariant()
236236
)),
237-
TrinaryLogic::createYes(),
237+
TrinaryLogic::createMaybe(),
238238
],
239239
];
240240
}

tests/PHPStan/Type/TypeCombinatorTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,60 @@ public function dataUnion(): array
14971497
MixedType::class,
14981498
'mixed=implicit',
14991499
],
1500+
[
1501+
[
1502+
TemplateTypeFactory::create(
1503+
TemplateTypeScope::createWithFunction('a'),
1504+
'T',
1505+
null,
1506+
TemplateTypeVariance::createInvariant()
1507+
),
1508+
TemplateTypeFactory::create(
1509+
TemplateTypeScope::createWithFunction('a'),
1510+
'K',
1511+
null,
1512+
TemplateTypeVariance::createInvariant()
1513+
),
1514+
],
1515+
UnionType::class,
1516+
'K (function a(), parameter)|T (function a(), parameter)',
1517+
],
1518+
[
1519+
[
1520+
TemplateTypeFactory::create(
1521+
TemplateTypeScope::createWithFunction('a'),
1522+
'T',
1523+
new ObjectWithoutClassType(),
1524+
TemplateTypeVariance::createInvariant()
1525+
),
1526+
TemplateTypeFactory::create(
1527+
TemplateTypeScope::createWithFunction('a'),
1528+
'K',
1529+
new ObjectWithoutClassType(),
1530+
TemplateTypeVariance::createInvariant()
1531+
),
1532+
],
1533+
UnionType::class,
1534+
'K of object (function a(), parameter)|T of object (function a(), parameter)',
1535+
],
1536+
[
1537+
[
1538+
TemplateTypeFactory::create(
1539+
TemplateTypeScope::createWithFunction('a'),
1540+
'T',
1541+
new ObjectType(\Exception::class),
1542+
TemplateTypeVariance::createInvariant()
1543+
),
1544+
TemplateTypeFactory::create(
1545+
TemplateTypeScope::createWithFunction('a'),
1546+
'K',
1547+
new ObjectType(\stdClass::class),
1548+
TemplateTypeVariance::createInvariant()
1549+
),
1550+
],
1551+
UnionType::class,
1552+
'K of stdClass (function a(), parameter)|T of Exception (function a(), parameter)',
1553+
],
15001554
];
15011555
}
15021556

0 commit comments

Comments
 (0)