Skip to content

Commit

Permalink
Fix accepting BenevolentUnionType as part of GenericObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 25, 2021
1 parent 92994d6 commit d29d098
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Type/Generic/TemplateTypeVariance.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function isValidVariance(Type $a, Type $b): TrinaryLogic
return TrinaryLogic::createYes();
}

if ($b instanceof BenevolentUnionType) {
if ($b instanceof BenevolentUnionType && !$a instanceof BenevolentUnionType) {
$results = [];
foreach ($b->getTypes() as $innerType) {
$results[] = $this->isValidVariance($a, $innerType);
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,9 @@ public function testTemplateStringBound(): void
$this->analyse([__DIR__ . '/data/template-string-bound.php'], []);
}

public function testBug4605(): void
{
$this->analyse([__DIR__ . '/data/bug-4605.php'], []);
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-4605.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Bug4605;

/**
* @phpstan-template TKey
* @psalm-template TKey of array-key
* @psalm-template T
* @template-extends IteratorAggregate<TKey, T>
* @template-extends ArrayAccess<TKey|null, T>
*/
interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess {}

class Boo {
/**
* @param Collection<array-key, string> $collection
* @return Collection<array-key, string>
*/
public function foo(Collection $collection): Collection
{
return $collection;
}

/**
* @param Collection<int, string> $collection
* @return Collection<int, string>
*/
public function boo(Collection $collection): Collection
{
return $collection;
}
}

0 comments on commit d29d098

Please sign in to comment.