1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #709 - detect intersection template matches a bit better

This commit is contained in:
Matthew Brown 2018-05-08 23:17:11 -04:00
parent 77d4629896
commit 3380de16a2
2 changed files with 46 additions and 1 deletions

View File

@ -601,10 +601,26 @@ class Union
$generic_params[$key]->setFromDocblock();
}
} else {
$matching_atomic_type = null;
if ($input_type) {
foreach ($input_type->types as $input_key => $atomic_input_type) {
if ($input_key === $key) {
$matching_atomic_type = $atomic_input_type;
break;
}
if (strpos($input_key, $key . '&') === 0) {
$matching_atomic_type = $atomic_input_type;
break;
}
}
}
$atomic_type->replaceTemplateTypesWithStandins(
$template_types,
$generic_params,
isset($input_type->types[$key]) ? $input_type->types[$key] : null
$matching_atomic_type
);
}
}

View File

@ -471,6 +471,35 @@ class TemplateTest extends TestCase
],
'error_levels' => ['MixedAssignment', 'MixedArgument'],
],
'intersectionTemplatedTypes' => [
'<?php
namespace NS;
use Countable;
/** @template T */
class Collection
{
/** @psalm-var iterable<T> */
private $data;
/** @psalm-param iterable<T> $data */
public function __construct(iterable $data) {
$this->data = $data;
}
}
class Item {}
/** @psalm-param Collection<Item> $c */
function takesCollectionOfItems(Collection $c): void {}
/** @psalm-var iterable<Item> $data2 */
$data2 = [];
takesCollectionOfItems(new Collection($data2));
/** @psalm-var iterable<Item>&Countable $data */
$data = [];
takesCollectionOfItems(new Collection($data));',
],
];
}