Skip to content

Commit

Permalink
Fix #2537 - allow complex intersection templates to be preserved
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 1, 2020
1 parent d175abe commit 086d141
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Psalm/Internal/Type/UnionTemplateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ private static function handleAtomicStandin(
}

if ($atomic_type instanceof Atomic\TTemplateParam
&& isset($template_result->template_types[$atomic_type->param_name][$atomic_type->defining_class])
&& ($param_name_key = strpos($key, '&') ? $key : $atomic_type->param_name)
&& isset($template_result->template_types[$param_name_key][$atomic_type->defining_class])
) {
$a = self::handleTemplateParamStandin(
$atomic_type,
Expand Down Expand Up @@ -390,6 +391,12 @@ private static function handleTemplateParamStandin(

$replacement_type = $template_type;

$param_name_key = $atomic_type->param_name;

if (strpos($key, '&')) {
$param_name_key = $key;
}

if ($replace) {
$atomic_types = [];

Expand Down Expand Up @@ -474,12 +481,6 @@ private static function handleTemplateParamStandin(

$generic_param->setFromDocblock();

$param_name_key = $atomic_type->param_name;

if (strpos($key, '&')) {
$param_name_key = $key;
}

if (isset(
$template_result->generic_params[$param_name_key][$atomic_type->defining_class][0]
)) {
Expand Down Expand Up @@ -522,7 +523,7 @@ private static function handleTemplateParamStandin(
$replacement_type
)
) {
$template_result->template_types[$atomic_type->param_name][$atomic_type->defining_class][0]
$template_result->template_types[$param_name_key][$atomic_type->defining_class][0]
= clone $input_type;
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Template/ClassTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2219,6 +2219,29 @@ function order(array $collection, callable $sorter): array {
}
}'
],
'intersectOnTOfObject' => [
'<?php
/**
* @psalm-template InterceptedObjectType of object
*/
interface AccessInterceptorInterface
{
/**
* @psalm-param Closure(
* InterceptedObjectType&AccessInterceptorInterface
* ) : mixed $prefixInterceptor
*/
public function setMethodPrefixInterceptor(Closure $prefixInterceptor = null) : void;
}
function foo(AccessInterceptorInterface $i) : void {
$i->setMethodPrefixInterceptor(
function(AccessInterceptorInterface $i) : string {
return "hello";
}
);
}'
]
];
}

Expand Down

0 comments on commit 086d141

Please sign in to comment.