diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php index 35b2c641849..44dc98e40de 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/NamedFunctionCallHandler.php @@ -422,22 +422,28 @@ private static function handleDependentTypeFunction( $var_id = '$' . $var->name; if (isset($context->vars_in_scope[$var_id])) { - if ($function_id === 'get_class') { - $atomic_type = new Type\Atomic\TDependentGetClass( - $var_id, - $context->vars_in_scope[$var_id]->hasMixed() - ? Type::getObject() - : $context->vars_in_scope[$var_id] - ); - } elseif ($function_id === 'gettype') { - $atomic_type = new Type\Atomic\TDependentGetType($var_id); - } else { - $atomic_type = new Type\Atomic\TDependentGetDebugType($var_id); - } + if (!$context->vars_in_scope[$var_id]->hasTemplate()) { + if ($function_id === 'get_class') { + $atomic_type = new Type\Atomic\TDependentGetClass( + $var_id, + $context->vars_in_scope[$var_id]->hasMixed() + ? Type::getObject() + : $context->vars_in_scope[$var_id] + ); + } elseif ($function_id === 'gettype') { + $atomic_type = new Type\Atomic\TDependentGetType($var_id); + } else { + $atomic_type = new Type\Atomic\TDependentGetDebugType($var_id); + } + + $statements_analyzer->node_data->setType($real_stmt, new Type\Union([$atomic_type])); - $statements_analyzer->node_data->setType($real_stmt, new Type\Union([$atomic_type])); + return; + } } - } elseif (($var_type = $statements_analyzer->node_data->getType($var)) + } + + if (($var_type = $statements_analyzer->node_data->getType($var)) && ($function_id === 'get_class' || $function_id === 'get_debug_type' ) diff --git a/tests/Template/FunctionTemplateTest.php b/tests/Template/FunctionTemplateTest.php index 19fcef8a654..cbef05adfa4 100644 --- a/tests/Template/FunctionTemplateTest.php +++ b/tests/Template/FunctionTemplateTest.php @@ -1529,6 +1529,26 @@ function toList(array $arr): array { return reset($arr); }' ], + 'callTemplatedFunctionWithTemplatedClassString' => [ + ' $type + * @psalm-return Tb + * @psalm-suppress InvalidReturnType + */ + function deserialize_object(string $data, string $type) {}' + ], ]; }