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

Fix leaky template type

This commit is contained in:
Matthew Brown 2019-05-24 13:35:14 -04:00
parent 3e2b7163ca
commit 7a48225184
3 changed files with 33 additions and 3 deletions

View File

@ -752,7 +752,7 @@ class StaticCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
if ($return_type_candidate) {
$return_type_candidate = clone $return_type_candidate;
if ($found_generic_params) {
if ($found_generic_params !== null) {
$return_type_candidate->replaceTemplateTypesWithArgTypes(
$found_generic_params
);

View File

@ -1314,7 +1314,11 @@ class Union
$new_types[$template_type_part->getKey()] = $template_type_part;
}
} else {
$new_types[$key] = $atomic_type;
if ($atomic_type->as->isSingle()) {
$new_types[$key] = array_values($atomic_type->as->getTypes())[0];
} else {
$new_types[$key] = new Type\Atomic\TMixed;
}
}
} elseif ($atomic_type instanceof Type\Atomic\TTemplateParamClass) {
$template_type = isset($template_types[$atomic_type->param_name][$atomic_type->defining_class ?: ''])

View File

@ -1626,7 +1626,7 @@ class TemplateTest extends TestCase
'$arr' => 'array<int, string>',
],
],
'templatedClassStringParam' => [
'templatedClassStringParamAsClass' => [
'<?php
abstract class C {
public function foo() : void{}
@ -1663,6 +1663,32 @@ class TemplateTest extends TestCase
$c->foo();
}',
],
'templatedClassStringParamAsObject' => [
'<?php
abstract class C {
public function foo() : void{}
}
class E {
/**
* @template T as object
* @param class-string<T> $c_class
*
* @psalm-return T
*/
public static function get(string $c_class) {
return new $c_class;
}
}
/**
* @psalm-suppress TypeCoercion
*/
function bat(string $c_class) : void {
$c = E::get($c_class);
$c->bar = "bax";
}',
],
'templatedClassStringParamMoreSpecific' => [
'<?php
abstract class C {