Skip to content

Commit

Permalink
Improve handling of template params a smidge
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Dec 27, 2019
1 parent 2f2cd85 commit b78f273
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
29 changes: 21 additions & 8 deletions src/Psalm/Internal/Type/UnionTemplateHandler.php
Expand Up @@ -99,7 +99,7 @@ private static function handleAtomicStandin(
} }


if ($atomic_type instanceof Atomic\TTemplateParam 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( $a = self::handleTemplateParamStandin(
$atomic_type, $atomic_type,
Expand Down Expand Up @@ -371,7 +371,10 @@ private static function handleTemplateParamStandin(
bool $was_nullable, bool $was_nullable,
bool &$had_template bool &$had_template
) : array { ) : 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) { if ($template_type->getId() === $key) {
return array_values($template_type->getTypes()); return array_values($template_type->getTypes());
Expand Down Expand Up @@ -420,7 +423,7 @@ private static function handleTemplateParamStandin(
$atomic_types[] = clone $key_type_atomic; $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; = clone $key_type;
} }
} }
Expand Down Expand Up @@ -462,23 +465,32 @@ private static function handleTemplateParamStandin(


$generic_param->setFromDocblock(); $generic_param->setFromDocblock();


if (isset($template_result->generic_params[$key][$atomic_type->defining_class ?: ''][0])) { if (isset(
$existing_depth = $template_result->generic_params[$key][$atomic_type->defining_class ?: ''][1] ?? -1; $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) { if ($existing_depth > $depth) {
return $atomic_types ?: [$atomic_type]; return $atomic_types ?: [$atomic_type];
} }


if ($existing_depth === $depth) { if ($existing_depth === $depth) {
$generic_param = \Psalm\Type::combineUnionTypes( $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, $generic_param,
$codebase $codebase
); );
} }
} }


$template_result->generic_params[$key][$atomic_type->defining_class ?: ''] = [ $template_result->generic_params[$atomic_type->param_name][$atomic_type->defining_class ?: ''] = [
$generic_param, $generic_param,
$depth, $depth,
]; ];
Expand All @@ -495,7 +507,8 @@ private static function handleTemplateParamStandin(
$replacement_type $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;
} }
} }


Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TTemplateParam.php
Expand Up @@ -52,7 +52,7 @@ public function getKey()
return $this->param_name . '&' . implode('&', $this->extra_types); return $this->param_name . '&' . implode('&', $this->extra_types);
} }


return $this->param_name; return $this->param_name . ($this->defining_class ? ':' . $this->defining_class : '');
} }


/** /**
Expand Down
1 change: 1 addition & 0 deletions tests/Template/ClassTemplateTest.php
Expand Up @@ -618,6 +618,7 @@ public function getType()
*/ */
public function bar(Foo $object) : void public function bar(Foo $object) : void
{ {
/** @psalm-suppress RedundantConditionGivenDocblockType */
if ($this->getType() !== get_class($object)) { if ($this->getType() !== get_class($object)) {
return; return;
} }
Expand Down

0 comments on commit b78f273

Please sign in to comment.