MCPcopy Create free account
hub / github.com/PHPantom-dev/phpantom_lsp / build_trait_substitution_map

Function build_trait_substitution_map

src/inheritance.rs:1094–1126  ·  view source on GitHub ↗

Build a substitution map for a trait based on `@use` generics and the trait's `@template` parameters. If the using class declares `@use HasFactory ` and the trait `HasFactory` has `@template TFactory`, the returned map is `{TFactory => UserFactory}`.

(
    trait_name: &str,
    trait_info: &ClassInfo,
    use_generics: &[(Atom, Vec<PhpType>)],
)

Source from the content-addressed store, hash-verified

1092/// trait `HasFactory` has `@template TFactory`, the returned map is
1093/// `{TFactory => UserFactory}`.
1094fn build_trait_substitution_map(
1095 trait_name: &str,
1096 trait_info: &ClassInfo,
1097 use_generics: &[(Atom, Vec<PhpType>)],
1098) -> HashMap<String, PhpType> {
1099 if trait_info.template_params.is_empty() || use_generics.is_empty() {
1100 return HashMap::new();
1101 }
1102
1103 let trait_short = short_name(trait_name);
1104
1105 // Find the @use entry that matches this trait.
1106 let type_args = use_generics
1107 .iter()
1108 .find(|(name, _)| {
1109 let name_short = short_name(name);
1110 name_short == trait_short
1111 })
1112 .map(|(_, args)| args);
1113
1114 let type_args = match type_args {
1115 Some(args) => args,
1116 None => return HashMap::new(),
1117 };
1118
1119 let mut map = HashMap::new();
1120 for (i, param_name) in trait_info.template_params.iter().enumerate() {
1121 if let Some(arg) = type_args.get(i) {
1122 map.insert(param_name.to_string(), arg.clone());
1123 }
1124 }
1125 map
1126}
1127
1128/// Build a substitution map for a parent class based on the child's
1129/// `@extends` generics and the parent's `@template` parameters.

Callers 1

merge_traits_intoFunction · 0.85

Calls 8

iterMethod · 0.80
cloneMethod · 0.80
short_nameFunction · 0.70
is_emptyMethod · 0.45
mapMethod · 0.45
findMethod · 0.45
getMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected