1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Fix #4326 - prevent more mapping than necessary

This commit is contained in:
Matt Brown 2020-10-14 21:22:51 -04:00 committed by Daniil Gentili
parent 84126fbbca
commit 1915f34959
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 47 additions and 1 deletions

View File

@ -876,6 +876,10 @@ class UnionTemplateHandler
string $defining_class, string $defining_class,
array $visited_classes = [] array $visited_classes = []
) : ?array { ) : ?array {
if (isset($visited_classes[$defining_class])) {
return null;
}
if (isset($template_types[$param_name][$defining_class])) { if (isset($template_types[$param_name][$defining_class])) {
$mapped_type = $template_types[$param_name][$defining_class][0]; $mapped_type = $template_types[$param_name][$defining_class][0];
@ -883,7 +887,6 @@ class UnionTemplateHandler
if (count($mapped_type_atomic_types) > 1 if (count($mapped_type_atomic_types) > 1
|| !$mapped_type_atomic_types[0] instanceof Atomic\TTemplateParam || !$mapped_type_atomic_types[0] instanceof Atomic\TTemplateParam
|| isset($visited_classes[$defining_class])
) { ) {
return $template_types[$param_name][$defining_class]; return $template_types[$param_name][$defining_class];
} }

View File

@ -3005,6 +3005,49 @@ class ClassTemplateTest extends TestCase
} }
}' }'
], ],
'flippedParamsInside' => [
'<?php
/**
* @template A
* @template B
*/
abstract class Foo
{
/** @return Traversable<A, B> */
public abstract function getTraversable() : Traversable;
/**
* @param Foo<B, A> $flipped
* @return Traversable<B, A>
*/
public function getFlippedTraversable(Foo $flipped): Traversable
{
return $flipped->getTraversable();
}
}'
],
'flippedParamsOutside' => [
'<?php
/**
* @template B
* @template A
* @param Foo<B, A> $flipped
* @return Traversable<B, A>
*/
function getFlippedTraversable(Foo $flipped): Traversable {
return $flipped->getTraversable();
}
/**
* @template A
* @template B
*/
abstract class Foo
{
/** @return Traversable<A, B> */
public abstract function getTraversable() : Traversable;
}'
],
]; ];
} }