Class PHPStan\Type\Generic\TemplateTypeMap

final

Maps template type parameter names to their resolved types.

This is the core data structure for PHPStan's generics support. When a class declares @template T, @template U of object, etc., the TemplateTypeMap tracks what concrete types T and U resolve to in a particular context.

Two kinds of type bindings are tracked:

  • types: The concrete type inferred or declared for each template. During inference these are lower bounds: T[] receiving int[] means T :> int.
  • upperBoundTypes: Types inferred from contravariant positions, such as the parameter of callable(T): void receiving callable(int): void, which means T <: int. A template is resolved to its lower bound when it has one, otherwise to its upper bound.

TemplateTypeMap supports set operations (union, intersect, benevolentUnion) that combine maps from different code paths: union() joins the lower bounds and meets the upper bounds, intersect() the other way around. resolveToBounds() replaces unresolved template types with their declared bounds.

Common usage: ParametersAcceptor::getTemplateTypeMap() returns the template declarations, and ParametersAcceptor::getResolvedTemplateTypeMap() returns inferred concrete types. Type::inferTemplateTypes() produces a TemplateTypeMap from a concrete type.

Methods