1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Improve handling of template params a smidge

This commit is contained in:
Matthew Brown 2019-12-27 11:49:44 -05:00
parent 2f2cd85a6a
commit b78f273ccf
3 changed files with 23 additions and 9 deletions

View File

@ -99,7 +99,7 @@ class UnionTemplateHandler
}
if ($atomic_type instanceof Atomic\TTemplateParam
&& isset($template_result->template_types[$key][$atomic_type->defining_class ?: ''])
&& isset($template_result->template_types[$atomic_type->param_name][$atomic_type->defining_class ?: ''])
) {
$a = self::handleTemplateParamStandin(
$atomic_type,
@ -371,7 +371,10 @@ class UnionTemplateHandler
bool $was_nullable,
bool &$had_template
) : array {
$template_type = $template_result->template_types[$key][$atomic_type->defining_class ?: ''][0];
$template_type = $template_result->template_types
[$atomic_type->param_name]
[$atomic_type->defining_class ?: '']
[0];
if ($template_type->getId() === $key) {
return array_values($template_type->getTypes());
@ -420,7 +423,7 @@ class UnionTemplateHandler
$atomic_types[] = clone $key_type_atomic;
}
$template_result->generic_params[$key][$atomic_type->defining_class ?: ''][0]
$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class ?: ''][0]
= clone $key_type;
}
}
@ -462,8 +465,14 @@ class UnionTemplateHandler
$generic_param->setFromDocblock();
if (isset($template_result->generic_params[$key][$atomic_type->defining_class ?: ''][0])) {
$existing_depth = $template_result->generic_params[$key][$atomic_type->defining_class ?: ''][1] ?? -1;
if (isset(
$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class ?: ''][0]
)) {
$existing_depth = $template_result->generic_params
[$atomic_type->param_name]
[$atomic_type->defining_class ?: '']
[1]
?? -1;
if ($existing_depth > $depth) {
return $atomic_types ?: [$atomic_type];
@ -471,14 +480,17 @@ class UnionTemplateHandler
if ($existing_depth === $depth) {
$generic_param = \Psalm\Type::combineUnionTypes(
$template_result->generic_params[$key][$atomic_type->defining_class ?: ''][0],
$template_result->generic_params
[$atomic_type->param_name]
[$atomic_type->defining_class ?: '']
[0],
$generic_param,
$codebase
);
}
}
$template_result->generic_params[$key][$atomic_type->defining_class ?: ''] = [
$template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class ?: ''] = [
$generic_param,
$depth,
];
@ -495,7 +507,8 @@ class UnionTemplateHandler
$replacement_type
)
) {
$template_result->template_types[$key][$atomic_type->defining_class ?: ''][0] = clone $input_type;
$template_result->template_types[$atomic_type->param_name][$atomic_type->defining_class ?: ''][0]
= clone $input_type;
}
}

View File

@ -52,7 +52,7 @@ class TTemplateParam extends \Psalm\Type\Atomic
return $this->param_name . '&' . implode('&', $this->extra_types);
}
return $this->param_name;
return $this->param_name . ($this->defining_class ? ':' . $this->defining_class : '');
}
/**

View File

@ -618,6 +618,7 @@ class ClassTemplateTest extends TestCase
*/
public function bar(Foo $object) : void
{
/** @psalm-suppress RedundantConditionGivenDocblockType */
if ($this->getType() !== get_class($object)) {
return;
}