Thanks to visit codestin.com
Credit goes to github.com

Skip to content

When combining templated class-string and Generics, the concrete class is missing. #4971

@zonuexe

Description

@zonuexe

Bug report

Classes instantiated from a class string do not have a generic type. The use case in this case is a typed collection class factory function.

Code snippet that reproduces the problem

https://phpstan.org/r/b8d7d149-be06-42bd-941d-67758ffbd708

/**
 * @template T
 * @template K of IFoo
 * @param T $v
 * @param class-string<K> $class
 * @return K<T>
 */
function make2($v, string $class)
{
    return new $class($v);
}


$obj2 = make2(1, Foo::class); // I want to type Foo<int>
\PHPStan\dumpType($obj2); // Kept generic type "int", but unexpected typed as "IFoo" interface

Expected output

Type as Foo<int> and no error will be printed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions