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

Prevent more broad template types being captured

This commit is contained in:
Brown 2019-05-28 11:54:07 -04:00
parent f062f9bb75
commit a89892a41f
3 changed files with 31 additions and 2 deletions

View File

@ -1007,7 +1007,16 @@ class Union
$this->had_template = true;
if ($input_type) {
if ($input_type
&& ($atomic_type->as->isMixed()
|| !$codebase
|| TypeAnalyzer::isContainedBy(
$codebase,
$input_type,
$atomic_type->as
)
)
) {
$generic_param = clone $input_type;
if ($this->isNullable() && $generic_param->isNullable() && !$generic_param->isNull()) {

View File

@ -626,7 +626,7 @@ class FunctionCallTest extends TestCase
}
}',
'assertions' => [],
'error_levels' => ['MixedAssignment', 'MixedArgument'],
'error_levels' => ['MixedAssignment', 'MixedArgument', 'MixedArgumentTypeCoercion'],
],
'compact' => [
'<?php

View File

@ -2617,6 +2617,26 @@ class TemplateTest extends TestCase
'$c' => 'C',
]
],
'unionClassStringTWithTReturnsObjectWhenCoerced' => [
'<?php
/**
* @template T as object
* @param T|class-string<T> $s
* @return T
*/
function bar($s) {
if (is_object($s)) {
return $s;
}
return new $s();
}
function foo(string $s) : object {
/** @psalm-suppress ArgumentTypeCoercion */
return bar($s);
}'
],
];
}