mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix - #816 - check we’re not just replacing a key with itself
This commit is contained in:
parent
45a94af015
commit
dc02ca8a7a
@ -743,12 +743,14 @@ class Union
|
||||
|
||||
foreach ($this->types as $key => $atomic_type) {
|
||||
if (isset($template_types[$key])) {
|
||||
$keys_to_unset[] = $key;
|
||||
$this->types[$template_types[$key]] = Atomic::create($template_types[$key]);
|
||||
if ($template_types[$key] !== $key) {
|
||||
$keys_to_unset[] = $key;
|
||||
$this->types[$template_types[$key]] = Atomic::create($template_types[$key]);
|
||||
|
||||
if ($input_type) {
|
||||
$generic_params[$key] = clone $input_type;
|
||||
$generic_params[$key]->setFromDocblock();
|
||||
if ($input_type) {
|
||||
$generic_params[$key] = clone $input_type;
|
||||
$generic_params[$key]->setFromDocblock();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$matching_atomic_type = null;
|
||||
|
@ -596,6 +596,66 @@ class TemplateTest extends TestCase
|
||||
/** @param array<stdClass> $p */
|
||||
function takesArrayOfStdClass(array $p): void {}',
|
||||
],
|
||||
'noRepeatedTypeException' => [
|
||||
'<?php
|
||||
/** @template T */
|
||||
class Foo
|
||||
{
|
||||
/**
|
||||
* @psalm-var class-string
|
||||
*/
|
||||
private $type;
|
||||
|
||||
/** @var array<T> */
|
||||
private $items;
|
||||
|
||||
/**
|
||||
* @param class-string $type
|
||||
* @template-typeof T $type
|
||||
*/
|
||||
public function __construct(string $type)
|
||||
{
|
||||
if (!in_array($type, [A::class, B::class], true)) {
|
||||
throw new \InvalidArgumentException;
|
||||
}
|
||||
$this->type = $type;
|
||||
$this->items = [];
|
||||
}
|
||||
|
||||
/** @param T $item */
|
||||
public function add($item): void
|
||||
{
|
||||
$this->items[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
class FooFacade
|
||||
{
|
||||
/**
|
||||
* @template T
|
||||
* @param T $item
|
||||
*/
|
||||
public function add($item): void
|
||||
{
|
||||
$foo = $this->ensureFoo([$item]);
|
||||
$foo->add($item);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param array<mixed,T> $items
|
||||
* @return Foo<T>
|
||||
*/
|
||||
private function ensureFoo(array $items): EntitySeries
|
||||
{
|
||||
$type = $items[0] instanceof A ? A::class : B::class;
|
||||
return new Foo($type);
|
||||
}
|
||||
}
|
||||
|
||||
class A {}
|
||||
class B {}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user