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

Fix implementation kink

This commit is contained in:
Matthew Brown 2020-03-15 16:14:09 -04:00
parent 3339c12179
commit 50eb12e562
3 changed files with 79 additions and 26 deletions

View File

@ -461,7 +461,9 @@ class Methods
$type = clone $type;
foreach ($type->getAtomicTypes() as $key => $atomic_type) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam
&& $atomic_type->defining_class === $base_fq_class_name
) {
$types_to_add = self::getExtendedTemplatedTypes(
$atomic_type,
$extends
@ -558,10 +560,12 @@ class Methods
$extends
)
);
} else {
$extra_added_types[] = $extended_atomic_type;
}
$extra_added_types[] = $extended_atomic_type;
}
} else {
$extra_added_types[] = $atomic_type;
}
return $extra_added_types;

View File

@ -3231,6 +3231,75 @@ class ClassTemplateExtendsTest extends TestCase
$a->boo("boo");
}'
],
'allowPropertyCoercionExtendedParam' => [
'<?php
class Test
{
/**
* @var ArrayCollection<int, DateTime>
*/
private $c;
public function __construct()
{
$this->c = new ArrayCollection();
$this->c->filter(function (DateTime $dt): bool {
return $dt === $dt;
});
}
}
/**
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Collection
{
/**
* @param Closure $p
*
* @return Collection A
*
* @psalm-param Closure(T=):bool $p
* @psalm-return Collection<TKey, T>
*/
public function filter(Closure $p);
}
/**
* @psalm-template TKey of array-key
* @psalm-template T
* @template-implements Collection<TKey,T>
*/
class ArrayCollection implements Collection
{
/**
* @psalm-var array<TKey,T>
* @var array
*/
private $elements;
/**
* @param array $elements
*
* @psalm-param array<TKey,T> $elements
*/
public function __construct(array $elements = [])
{
$this->elements = $elements;
}
/**
* {@inheritDoc}
*
* @return static
*/
public function filter(Closure $p)
{
return $this;
}
}'
],
];
}

View File

@ -2387,26 +2387,7 @@ class ClassTemplateTest extends TestCase
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Collection
{
/**
* @param Closure $p
*
* @return Collection A
*
* @psalm-param Closure(T=):bool $p
* @psalm-return Collection<TKey, T>
*/
public function filter(Closure $p);
}
/**
* @psalm-template TKey of array-key
* @psalm-template T
* @template-implements Collection<TKey,T>
*/
class ArrayCollection implements Collection
class ArrayCollection
{
/**
* @psalm-var array<TKey,T>
@ -2425,9 +2406,8 @@ class ClassTemplateTest extends TestCase
}
/**
* {@inheritDoc}
*
* @return static
* @psalm-param Closure(T=):bool $p
* @psalm-return self<TKey, T>
*/
public function filter(Closure $p)
{