Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeCombinator::union() for intersection of array and template type #1444

Merged
merged 1 commit into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}