Skip to content

Commit d29d098

Browse files
committed
Fix accepting BenevolentUnionType as part of GenericObjectType
1 parent 92994d6 commit d29d098

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/Type/Generic/TemplateTypeVariance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function isValidVariance(Type $a, Type $b): TrinaryLogic
102102
return TrinaryLogic::createYes();
103103
}
104104

105-
if ($b instanceof BenevolentUnionType) {
105+
if ($b instanceof BenevolentUnionType && !$a instanceof BenevolentUnionType) {
106106
$results = [];
107107
foreach ($b->getTypes() as $innerType) {
108108
$results[] = $this->isValidVariance($a, $innerType);

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,9 @@ public function testTemplateStringBound(): void
449449
$this->analyse([__DIR__ . '/data/template-string-bound.php'], []);
450450
}
451451

452+
public function testBug4605(): void
453+
{
454+
$this->analyse([__DIR__ . '/data/bug-4605.php'], []);
455+
}
456+
452457
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Bug4605;
4+
5+
/**
6+
* @phpstan-template TKey
7+
* @psalm-template TKey of array-key
8+
* @psalm-template T
9+
* @template-extends IteratorAggregate<TKey, T>
10+
* @template-extends ArrayAccess<TKey|null, T>
11+
*/
12+
interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess {}
13+
14+
class Boo {
15+
/**
16+
* @param Collection<array-key, string> $collection
17+
* @return Collection<array-key, string>
18+
*/
19+
public function foo(Collection $collection): Collection
20+
{
21+
return $collection;
22+
}
23+
24+
/**
25+
* @param Collection<int, string> $collection
26+
* @return Collection<int, string>
27+
*/
28+
public function boo(Collection $collection): Collection
29+
{
30+
return $collection;
31+
}
32+
}

0 commit comments

Comments
 (0)