Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix template replacement edge case #10586

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,6 @@ public static function getMappedGenericTypeParams(
Atomic $container_type_part,
?array &$container_type_params_covariant = null
): array {
$_ = null;
if ($input_type_part instanceof TGenericObject || $input_type_part instanceof TIterable) {
$input_type_params = $input_type_part->type_params;
} elseif ($codebase->classlike_storage_provider->has($input_type_part->value)) {
Expand Down Expand Up @@ -1290,7 +1289,6 @@ public static function getMappedGenericTypeParams(
$replacement_templates = [];

if ($input_template_types
&& (!$input_type_part instanceof TGenericObject || !$input_type_part->remapped_params)
&& (!$container_type_part instanceof TGenericObject || !$container_type_part->remapped_params)
) {
foreach ($input_template_types as $template_name => $_) {
Expand Down
62 changes: 62 additions & 0 deletions tests/Template/FunctionTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,68 @@ class FunctionTemplateTest extends TestCase
public function providerValidCodeParse(): iterable
{
return [
'extractTypeParameterValue' => [
'code' => '<?php
/**
* @template T
*/
interface Type {}

/**
* @implements Type<int>
*/
final readonly class IntType implements Type {}

/**
* @template T
* @implements Type<list<T>>
*/
final readonly class ListType implements Type
{
/**
* @param Type<T> $type
*/
public function __construct(
public Type $type,
) {
}
}

/**
* @template T
* @param Type<T> $type
* @return T
*/
function extractType(Type $type): mixed
{
throw new \RuntimeException("Should never be called at runtime");
}

/**
* @template T
* @param Type<T> $t
* @return ListType<T>
*/
function listType(Type $t): ListType
{
return new ListType($t);
}

function intType(): IntType
{
return new IntType();
}

$listType = listType(intType());
$list = extractType($listType);
',
'assertions' => [
'$listType===' => 'ListType<int>',
'$list' => 'list<int>',
],
'ignored_issues' => [],
'php_version' => '8.2',
],
'validTemplatedType' => [
'code' => '<?php
namespace FooFoo;
Expand Down