Skip to content

Commit

Permalink
Fix TypeCombinator::union() for intersection of array and template type
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen authored and ondrejmirtes committed Jun 20, 2022
1 parent 20a6b38 commit b1069e1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Type/TypeCombinator.php
Expand Up @@ -161,6 +161,9 @@ public static function union(Type ...$types): Type
$intermediateArrayType = null;
$intermediateAccessoryTypes = [];
foreach ($types[$i]->getTypes() as $innerType) {
if ($innerType instanceof TemplateType) {
continue 2;
}
if ($innerType instanceof ArrayType) {
$intermediateArrayType = $innerType;
continue;
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Expand Up @@ -706,4 +706,10 @@ public function testBug7460(): void
$this->analyse([__DIR__ . '/data/bug-7460.php'], []);
}

public function testBug4117(): void
{
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-4117.php'], []);
}

}
47 changes: 47 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-4117.php
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1);

namespace Bug4117;

use ArrayIterator;
use IteratorAggregate;

/**
* @refactor Refactor utils into base library
* @template T of mixed
* @implements IteratorAggregate<int, T>
*/
class GenericList implements IteratorAggregate
{
/** @var array<int, T> */
protected $items = [];

/**
* @return ArrayIterator<int, T>
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->items);
}

/**
* @return ?T
*/
public function broken(int $key)
{
$item = $this->items[$key] ?? null;
if ($item) {
}

return $item;
}

/**
* @return ?T
*/
public function works(int $key)
{
$item = $this->items[$key] ?? null;

return $item;
}
}

0 comments on commit b1069e1

Please sign in to comment.