Skip to main content

php_lsp/types/
type_map.rs

1/// Single-pass type inference: collects `$var = new ClassName()` assignments,
2/// `@var` docblocks, `@param` hints, and PHPStorm meta factory calls to map
3/// variable names to class names. Used to scope method completions after `->`.
4use std::collections::HashMap;
5
6use php_ast::{
7    ClassMemberKind, EnumMemberKind, ExprKind, NamespaceBody, Stmt, StmtKind, TypeHint,
8    TypeHintKind,
9};
10use tower_lsp::lsp_types::Position;
11
12use crate::document::ast::{ParsedDoc, SourceView};
13use crate::lang::docblock::{docblock_before, parse_docblock};
14use crate::lang::phpstorm_meta::PhpStormMeta;
15use crate::text::fqn_short_name;
16
17/// Maps variable name (with `$`) → class name.
18#[derive(Debug, Default, Clone)]
19pub struct TypeMap(HashMap<String, String>);
20
21impl TypeMap {
22    #[cfg(test)]
23    pub fn from_doc(doc: &ParsedDoc) -> Self {
24        Self::from_doc_with_meta(doc, None)
25    }
26
27    /// Build from a parsed document, optionally enriched by PHPStorm metadata
28    /// for factory-method return type inference.
29    pub fn from_doc_with_meta(doc: &ParsedDoc, meta: Option<&PhpStormMeta>) -> Self {
30        let mut map = HashMap::new();
31        collect_types_stmts(
32            doc.source(),
33            &doc.program().stmts,
34            &mut map,
35            meta,
36            None,
37            doc,
38        );
39        let aliases = collect_file_type_aliases(doc.source(), &doc.program().stmts);
40        expand_type_aliases(&mut map, &aliases);
41        TypeMap(map)
42    }
43
44    /// Like [`from_doc_with_meta`] but scopes method-body variable collection
45    /// to the method (or function) containing `position`. Variables local to
46    /// other method bodies are excluded so they cannot pollute the map with
47    /// wrong types at the cursor site. Sets `$this` when the position is inside
48    /// an instance method.
49    pub fn from_doc_at_position(
50        doc: &ParsedDoc,
51        meta: Option<&PhpStormMeta>,
52        position: Position,
53    ) -> Self {
54        let cursor_byte = {
55            let line_starts = doc.line_starts();
56            let line = position.line as usize;
57            if line < line_starts.len() {
58                let line_start = line_starts[line] as usize;
59                let col_byte = crate::text::utf16_offset_to_byte(
60                    &doc.source()[line_start..],
61                    position.character as usize,
62                );
63                Some((line_start + col_byte) as u32)
64            } else {
65                None
66            }
67        };
68        let mut map = HashMap::new();
69        collect_types_stmts(
70            doc.source(),
71            &doc.program().stmts,
72            &mut map,
73            meta,
74            cursor_byte,
75            doc,
76        );
77        let aliases = collect_file_type_aliases(doc.source(), &doc.program().stmts);
78        expand_type_aliases(&mut map, &aliases);
79        TypeMap(map)
80    }
81
82    /// Returns the class name for a variable, e.g. `get("$obj")` → `Some("Foo")`.
83    pub fn get<'a>(&'a self, var: &str) -> Option<&'a str> {
84        self.0.get(var).map(|s| s.as_str())
85    }
86}
87
88/// Extract a class-name string from a type hint using mir's type resolver.
89/// - `Named(Foo)` → `"Foo"`, `Named(\App\Foo)` → `"Foo"` (short name)
90/// - `Nullable(Named(Foo))` → `"Foo"` (strips the nullable wrapper)
91/// - `Union([Named(Foo), Named(Bar)])` → `"Foo|Bar"`
92/// - `Intersection([Foo, Bar])` → `"Foo|Bar"` (flattened; `type_candidates()` splits on both `|` and `&`)
93/// - `self` / `static` with `enclosing` → returns the enclosing short name
94/// - Primitives and unrecognised kinds → `None`
95fn type_hint_to_class_string(
96    hint: &TypeHint<'_, '_>,
97    enclosing_class: Option<&str>,
98    doc: Option<&ParsedDoc>,
99) -> Option<String> {
100    use mir_types::Atomic;
101    let union = mir_analyzer::parser::type_from_hint(hint, enclosing_class);
102    let classes: Vec<String> = union
103        .types
104        .iter()
105        .filter_map(|a| match a {
106            Atomic::TNamedObject { fqcn, .. }
107            | Atomic::TSelf { fqcn }
108            | Atomic::TStaticObject { fqcn } => {
109                let short = fqn_short_name(fqcn);
110                Some(short.to_string())
111            }
112            Atomic::TParent { fqcn } => {
113                if let (Some(doc), Some(enc_class)) = (doc, enclosing_class) {
114                    if let Some(parent) = parent_class_name(doc, enc_class) {
115                        let short = fqn_short_name(&parent);
116                        Some(short.to_string())
117                    } else {
118                        let short = fqn_short_name(fqcn);
119                        Some(short.to_string())
120                    }
121                } else {
122                    let short = fqn_short_name(fqcn);
123                    Some(short.to_string())
124                }
125            }
126            Atomic::TIntersection { parts } => {
127                let intersection_classes: Vec<String> = parts
128                    .iter()
129                    .flat_map(|part| {
130                        part.types.iter().filter_map(|a| match a {
131                            Atomic::TNamedObject { fqcn, .. }
132                            | Atomic::TSelf { fqcn }
133                            | Atomic::TStaticObject { fqcn } => {
134                                let short = fqn_short_name(fqcn);
135                                Some(short.to_string())
136                            }
137                            Atomic::TParent { fqcn } => {
138                                if let (Some(doc), Some(enc_class)) = (doc, enclosing_class) {
139                                    if let Some(parent) = parent_class_name(doc, enc_class) {
140                                        let short = fqn_short_name(&parent);
141                                        Some(short.to_string())
142                                    } else {
143                                        let short =
144                                            fqcn.rsplit('\\').next().unwrap_or(fqcn.as_ref());
145                                        Some(short.to_string())
146                                    }
147                                } else {
148                                    let short = fqn_short_name(fqcn);
149                                    Some(short.to_string())
150                                }
151                            }
152                            _ => None,
153                        })
154                    })
155                    .collect();
156                if intersection_classes.is_empty() {
157                    None
158                } else {
159                    Some(intersection_classes.join("|"))
160                }
161            }
162            _ => None,
163        })
164        .collect();
165    if classes.is_empty() {
166        None
167    } else {
168        Some(classes.join("|"))
169    }
170}
171
172/// Extract class names from a docblock type hint, in declaration order.
173///
174/// Splits a union (`Foo|Bar|null`), trims a leading `\` and nullable `?`, and
175/// reduces each class part to its short name (`A\B\Foo` → `Foo`).
176///
177/// Generic parameters on *user-defined* class names are stripped so the result
178/// is a bare FQCN suitable for member lookups (`Collection<User>` → `Collection`).
179///
180/// PHP built-in iterable types whose sole purpose is to carry an element type
181/// (`list<T>`, `array<K,V>`, `iterable<K,V>`, `non-empty-list<T>`,
182/// `non-empty-array<K,V>`) are unwrapped and their element-type class names are
183/// returned instead — this enables the TypeMap fallback to propagate element
184/// types through `foreach` loops when the mir-primary path is unavailable.
185fn docblock_class_parts(type_hint: &str) -> Vec<String> {
186    type_hint
187        .split('|')
188        .flat_map(|p| {
189            let p = p.trim().trim_start_matches('\\').trim_start_matches('?');
190            // PHP built-in iterable types: extract element class from the generic param.
191            if let Some(elem) = php_iterable_element_type(p) {
192                return docblock_class_parts(elem);
193            }
194            // User-defined class (starts uppercase): strip any generic params.
195            if p.chars().next().map(|c| c.is_uppercase()).unwrap_or(false) {
196                let base = p.split_once('<').map(|(b, _)| b).unwrap_or(p);
197                if let Some(short) = base.rsplit('\\').next() {
198                    return vec![short.to_string()];
199                }
200            }
201            vec![]
202        })
203        .collect()
204}
205
206/// If `ty` is one of the PHP built-in iterable types with a generic element
207/// (`list<T>`, `array<K,V>`, `iterable<K,V>`, `non-empty-list<T>`,
208/// `non-empty-array<K,V>`), returns the element-type substring; otherwise `None`.
209fn php_iterable_element_type(ty: &str) -> Option<&str> {
210    let base = ty.split_once('<').map(|(b, _)| b).unwrap_or(ty);
211    if !matches!(
212        base,
213        "list" | "array" | "iterable" | "non-empty-list" | "non-empty-array"
214    ) {
215        return None;
216    }
217    let inner = ty.split_once('<')?.1.strip_suffix('>')?;
218    // array<K,V> / iterable<K,V>: element type is the last generic argument.
219    Some(if let Some((_, last)) = inner.rsplit_once(',') {
220        last.trim()
221    } else {
222        inner.trim()
223    })
224}
225
226/// Collect `@psalm-type` / `@phpstan-type` aliases from all statement-level
227/// docblocks in `stmts`.  Aliases declared in a class docblock (the docblock
228/// immediately before the `class` keyword) are the most common case.
229fn collect_file_type_aliases(source: &str, stmts: &[Stmt<'_, '_>]) -> HashMap<String, String> {
230    let mut aliases = HashMap::new();
231    collect_aliases_in_stmts(source, stmts, &mut aliases);
232    aliases
233}
234
235fn collect_aliases_in_stmts(
236    source: &str,
237    stmts: &[Stmt<'_, '_>],
238    aliases: &mut HashMap<String, String>,
239) {
240    for stmt in stmts {
241        if let Some(raw) = docblock_before(source, stmt.span.start) {
242            let db = parse_docblock(&raw);
243            for ta in &db.type_aliases {
244                aliases.insert(ta.name.clone(), ta.type_expr.clone());
245            }
246        }
247        if let StmtKind::Namespace(ns) = &stmt.kind
248            && let NamespaceBody::Braced(inner) = &ns.body
249        {
250            collect_aliases_in_stmts(source, &inner.stmts, aliases);
251        }
252    }
253}
254
255/// Post-process `map` by expanding any value that is a known alias name.
256///
257/// `@psalm-type Result = Success|Failure` → variables mapped to `"Result"` are
258/// updated to `"Success|Failure"` so member lookups find the actual classes.
259/// Only one level of expansion is performed (no recursive alias resolution).
260fn expand_type_aliases(map: &mut HashMap<String, String>, aliases: &HashMap<String, String>) {
261    if aliases.is_empty() {
262        return;
263    }
264    for value in map.values_mut() {
265        if let Some(expanded_expr) = aliases.get(value.as_str()) {
266            let expanded_classes = docblock_class_parts(expanded_expr);
267            if !expanded_classes.is_empty() {
268                *value = expanded_classes.join("|");
269            }
270        }
271    }
272}
273
274/// Apply `@param` class-type hints from the docblock preceding `span_start` to
275/// `map`, keyed by `$name`. Uses `or_insert` so real signature type hints
276/// (collected separately) take precedence.
277fn collect_param_docblock_types(source: &str, span_start: u32, map: &mut HashMap<String, String>) {
278    let Some(raw) = docblock_before(source, span_start) else {
279        return;
280    };
281    let db = parse_docblock(&raw);
282    for param in &db.params {
283        let classes = docblock_class_parts(&param.type_hint);
284        if classes.is_empty() {
285            continue;
286        }
287        let key = if param.name.starts_with('$') {
288            param.name.clone()
289        } else {
290            format!("${}", param.name)
291        };
292        map.entry(key).or_insert_with(|| classes.join("|"));
293    }
294}
295
296#[allow(clippy::too_many_arguments)]
297fn collect_types_stmts(
298    source: &str,
299    stmts: &[Stmt<'_, '_>],
300    map: &mut HashMap<String, String>,
301    meta: Option<&PhpStormMeta>,
302    cursor_byte: Option<u32>,
303    doc: &ParsedDoc,
304) {
305    for stmt in stmts {
306        if let Some(raw) = docblock_before(source, stmt.span.start) {
307            let db = parse_docblock(&raw);
308            if let Some(type_str) = db.var_type {
309                let class_name = docblock_class_parts(&type_str).into_iter().next();
310                if let Some(class_name) = class_name {
311                    if let Some(vname) = db.var_name {
312                        map.insert(format!("${}", vname.as_str()), class_name);
313                    } else if let StmtKind::Expression(e) = &stmt.kind
314                        && let ExprKind::Assign(a) = &e.kind
315                        && let ExprKind::Variable(vn) = &a.target.kind
316                    {
317                        map.insert(format!("${}", vn.as_str()), class_name);
318                    }
319                }
320            }
321        }
322
323        match &stmt.kind {
324            StmtKind::Expression(e) => collect_types_expr(source, e, map, meta, cursor_byte, doc),
325            StmtKind::Function(f) => {
326                let in_scope =
327                    cursor_byte.is_none_or(|c| stmt.span.start <= c && c <= stmt.span.end);
328                if !in_scope {
329                    continue;
330                }
331                collect_param_docblock_types(source, stmt.span.start, map);
332                for p in f.params.iter() {
333                    if let Some(hint) = &p.type_hint
334                        && let Some(class_str) = type_hint_to_class_string(hint, None, Some(doc))
335                    {
336                        map.insert(format!("${}", p.name), class_str);
337                    }
338                }
339                collect_types_stmts(source, &f.body.stmts, map, meta, cursor_byte, doc);
340            }
341            StmtKind::Class(c) => {
342                let class_name = c.name.map(|n| n.to_string());
343                for member in c.body.members.iter() {
344                    if let ClassMemberKind::Method(m) = &member.kind {
345                        let in_scope = cursor_byte
346                            .is_none_or(|cb| member.span.start <= cb && cb <= member.span.end);
347                        if !in_scope {
348                            continue;
349                        }
350                        collect_param_docblock_types(source, member.span.start, map);
351                        for p in m.params.iter() {
352                            if let Some(hint) = &p.type_hint
353                                && let Some(class_str) = type_hint_to_class_string(
354                                    hint,
355                                    class_name.as_deref(),
356                                    Some(doc),
357                                )
358                            {
359                                map.insert(format!("${}", p.name), class_str);
360                            }
361                        }
362                        if !m.is_static
363                            && let Some(ref cname) = class_name
364                        {
365                            map.insert("$this".to_string(), cname.clone());
366                        }
367                        if let Some(body) = &m.body {
368                            collect_types_stmts(source, &body.stmts, map, meta, cursor_byte, doc);
369                        }
370                    }
371                }
372            }
373            StmtKind::Trait(t) => {
374                for member in t.body.members.iter() {
375                    if let ClassMemberKind::Method(m) = &member.kind {
376                        let in_scope = cursor_byte
377                            .is_none_or(|cb| member.span.start <= cb && cb <= member.span.end);
378                        if !in_scope {
379                            continue;
380                        }
381                        for p in m.params.iter() {
382                            if let Some(hint) = &p.type_hint
383                                && let Some(class_str) =
384                                    type_hint_to_class_string(hint, None, Some(doc))
385                            {
386                                map.insert(format!("${}", p.name), class_str);
387                            }
388                        }
389                        if let Some(body) = &m.body {
390                            collect_types_stmts(source, &body.stmts, map, meta, cursor_byte, doc);
391                        }
392                    }
393                }
394            }
395            StmtKind::Enum(e) => {
396                for member in e.body.members.iter() {
397                    if let EnumMemberKind::Method(m) = &member.kind {
398                        let in_scope = cursor_byte
399                            .is_none_or(|cb| member.span.start <= cb && cb <= member.span.end);
400                        if !in_scope {
401                            continue;
402                        }
403                        for p in m.params.iter() {
404                            if let Some(hint) = &p.type_hint
405                                && let Some(class_str) =
406                                    type_hint_to_class_string(hint, None, Some(doc))
407                            {
408                                map.insert(format!("${}", p.name), class_str);
409                            }
410                        }
411                        if let Some(body) = &m.body {
412                            collect_types_stmts(source, &body.stmts, map, meta, cursor_byte, doc);
413                        }
414                    }
415                }
416            }
417            StmtKind::Namespace(ns) => {
418                if let NamespaceBody::Braced(inner) = &ns.body {
419                    collect_types_stmts(source, &inner.stmts, map, meta, cursor_byte, doc);
420                }
421            }
422            // `instanceof` narrowing is handled by mir's flow-sensitive analysis; we only recurse into the branches.
423            StmtKind::If(if_stmt) => {
424                collect_types_stmts(
425                    source,
426                    std::slice::from_ref(if_stmt.then_branch),
427                    map,
428                    meta,
429                    cursor_byte,
430                    doc,
431                );
432                for elseif in if_stmt.elseif_branches.iter() {
433                    collect_types_stmts(
434                        source,
435                        std::slice::from_ref(&elseif.body),
436                        map,
437                        meta,
438                        cursor_byte,
439                        doc,
440                    );
441                }
442                if let Some(else_branch) = if_stmt.else_branch {
443                    collect_types_stmts(
444                        source,
445                        std::slice::from_ref(else_branch),
446                        map,
447                        meta,
448                        cursor_byte,
449                        doc,
450                    );
451                }
452            }
453
454            StmtKind::Foreach(f) => {
455                // Propagate element type: if the iterable is a variable with a
456                // known TypeMap type (e.g. from `@var list<Widget> $items`), map
457                // the foreach value variable to that same type so completions
458                // work inside the loop body in the TypeMap fallback path.
459                if let ExprKind::Variable(arr_name) = &f.expr.kind
460                    && let ExprKind::Variable(val_name) = &f.value.kind
461                {
462                    let arr_key = format!("${}", arr_name.as_str());
463                    if let Some(elem_type) = map.get(&arr_key).cloned() {
464                        map.entry(format!("${}", val_name.as_str()))
465                            .or_insert(elem_type);
466                    }
467                }
468                collect_types_stmts(
469                    source,
470                    std::slice::from_ref(f.body),
471                    map,
472                    meta,
473                    cursor_byte,
474                    doc,
475                );
476            }
477            StmtKind::TryCatch(t) => {
478                collect_types_stmts(source, &t.body.stmts, map, meta, cursor_byte, doc);
479                for catch in t.catches.iter() {
480                    collect_types_stmts(source, &catch.body.stmts, map, meta, cursor_byte, doc);
481                }
482                if let Some(finally) = &t.finally {
483                    collect_types_stmts(source, &finally.stmts, map, meta, cursor_byte, doc);
484                }
485            }
486
487            StmtKind::StaticVar(vars) => {
488                for var in vars.iter() {
489                    let var_key = format!("${}", &var.name.to_string());
490                    if let Some(default) = &var.default {
491                        if let ExprKind::New(new_expr) = &default.kind
492                            && let Some(class_name) = extract_class_name(new_expr.class)
493                        {
494                            map.insert(var_key.clone(), class_name);
495                        }
496                        if let ExprKind::Array(_) = &default.kind {
497                            map.insert(var_key, "array".to_string());
498                        }
499                    }
500                }
501            }
502
503            _ => {}
504        }
505    }
506}
507
508fn collect_types_expr(
509    source: &str,
510    expr: &php_ast::Expr<'_, '_>,
511    map: &mut HashMap<String, String>,
512    meta: Option<&PhpStormMeta>,
513    cursor_byte: Option<u32>,
514    doc: &ParsedDoc,
515) {
516    match &expr.kind {
517        ExprKind::Assign(assign) => {
518            if let ExprKind::Variable(var_name) = &assign.target.kind {
519                if let ExprKind::New(new_expr) = &assign.value.kind
520                    && let Some(class_name) = extract_class_name(new_expr.class)
521                {
522                    map.insert(format!("${}", var_name.as_str()), class_name);
523                }
524                // `clone($obj, [...])` (PHP 8.5 clone-with) preserves the object's
525                // type; mir resolves this directly, so no TypeMap branch is needed.
526                // PHPStorm meta: `$var = $obj->make(SomeClass::class)`
527                if let Some(meta) = meta
528                    && let Some(inferred) = infer_from_meta_method_call(assign.value, map, meta)
529                {
530                    map.insert(format!("${}", var_name.as_str()), inferred);
531                }
532                // First-class callable syntax (`strlen(...)`, `$obj->method(...)`)
533                // produces a `Closure` instance (PHP 8.1+).  Register the variable
534                // as Closure so hover and member-completion recognise it.
535                if let ExprKind::CallableCreate(_) = &assign.value.kind {
536                    map.entry(format!("${}", var_name.as_str()))
537                        .or_insert_with(|| "Closure".to_string());
538                }
539            }
540            collect_types_expr(source, assign.value, map, meta, cursor_byte, doc);
541        }
542
543        ExprKind::Closure(c) => {
544            for p in c.params.iter() {
545                if let Some(hint) = &p.type_hint
546                    && let TypeHintKind::Named(name) = &hint.kind
547                {
548                    map.insert(format!("${}", p.name), name.to_string_repr().to_string());
549                }
550            }
551            // Snapshot captured `use` variable types from the outer scope so they
552            // remain resolvable inside the closure body even if the body walk
553            // encounters assignments that would shadow them.
554            let use_var_snapshot: Vec<(String, String)> = c
555                .use_vars
556                .iter()
557                .filter_map(|uv| {
558                    let key = format!("${}", &uv.name.to_string());
559                    map.get(&key).map(|ty| (key, ty.clone()))
560                })
561                .collect();
562            collect_types_stmts(source, &c.body.stmts, map, meta, cursor_byte, doc);
563            // Restore captured variable types: inner assignments inside the closure
564            // body should not affect the outer scope's type for completions.
565            for (key, ty) in use_var_snapshot {
566                map.insert(key, ty);
567            }
568        }
569
570        ExprKind::ArrowFunction(af) => {
571            for p in af.params.iter() {
572                if let Some(hint) = &p.type_hint
573                    && let TypeHintKind::Named(name) = &hint.kind
574                {
575                    map.insert(format!("${}", p.name), name.to_string_repr().to_string());
576                }
577            }
578            collect_types_expr(source, af.body, map, meta, cursor_byte, doc);
579        }
580
581        _ => {}
582    }
583}
584
585fn extract_class_name(expr: &php_ast::Expr<'_, '_>) -> Option<String> {
586    match &expr.kind {
587        ExprKind::Identifier(name) => Some(name.as_str().to_string()),
588        _ => None,
589    }
590}
591
592/// Try to infer the return type of `$obj->method(SomeClass::class)` using the
593/// PHPStorm meta map.  `map` is consulted to resolve `$obj`'s class.
594fn infer_from_meta_method_call(
595    expr: &php_ast::Expr<'_, '_>,
596    var_map: &HashMap<String, String>,
597    meta: &PhpStormMeta,
598) -> Option<String> {
599    let ExprKind::MethodCall(m) = &expr.kind else {
600        return None;
601    };
602    // Resolve the receiver's type.
603    let receiver_class = match &m.object.kind {
604        ExprKind::Variable(v) => {
605            let key = format!("${}", v.as_str());
606            var_map.get(&key)?.clone()
607        }
608        _ => return None,
609    };
610    // Get the method name.
611    let method_name = match &m.method.kind {
612        ExprKind::Identifier(n) => n.to_string(),
613        _ => return None,
614    };
615    // Get the first argument as a class name string.
616    let arg = m.args.first()?;
617    let arg_str = match &arg.value.kind {
618        ExprKind::String(s) => s.trim_start_matches('\\').to_string(),
619        ExprKind::ClassConstAccess(c) if c.member.name_str() == Some("class") => {
620            match &c.class.kind {
621                ExprKind::Identifier(n) => n
622                    .trim_start_matches('\\')
623                    .rsplit('\\')
624                    .next()
625                    .unwrap_or(n)
626                    .to_string(),
627                _ => return None,
628            }
629        }
630        _ => return None,
631    };
632    meta.resolve_return_type(&receiver_class, &method_name, &arg_str)
633        .map(|s| s.to_string())
634}
635
636/// Return the direct parent class name of `class_name` in `doc`, if any.
637pub fn parent_class_name(doc: &ParsedDoc, class_name: &str) -> Option<String> {
638    parent_in_stmts(&doc.program().stmts, class_name)
639}
640
641fn parent_in_stmts(stmts: &[Stmt<'_, '_>], class_name: &str) -> Option<String> {
642    for stmt in stmts {
643        match &stmt.kind {
644            StmtKind::Class(c)
645                if c.name.as_ref().map(|n| n.to_string()) == Some(class_name.to_string()) =>
646            {
647                return c.extends.as_ref().map(|n| n.to_string_repr().to_string());
648            }
649            StmtKind::Namespace(ns) => {
650                if let NamespaceBody::Braced(inner) = &ns.body
651                    && let found @ Some(_) = parent_in_stmts(&inner.stmts, class_name)
652                {
653                    return found;
654                }
655            }
656            _ => {}
657        }
658    }
659    None
660}
661
662/// All members of a named class split by kind and static-ness.
663#[derive(Debug, Default)]
664pub struct ClassMembers {
665    /// (name, is_static)
666    pub methods: Vec<(String, bool)>,
667    /// (name, is_static)
668    pub properties: Vec<(String, bool)>,
669    /// Names of readonly properties (PHP 8.1+).
670    pub readonly_properties: Vec<String>,
671    pub constants: Vec<String>,
672    /// Direct parent class name, if any.
673    pub parent: Option<String>,
674    /// Trait names used by this class (`use Foo, Bar;`).
675    pub trait_uses: Vec<String>,
676    /// True when a class/enum/trait with this name was found in the doc.
677    /// Lets workspace-wide loops short-circuit once the defining doc is hit
678    /// instead of continuing to scan every file.
679    pub found: bool,
680}
681
682/// Return all members (methods, properties, constants) of `class_name`.
683/// Also returns the direct parent class name via `ClassMembers::parent`.
684pub fn members_of_class(doc: &ParsedDoc, class_name: &str) -> ClassMembers {
685    let mut out = ClassMembers::default();
686    out.parent = collect_members_stmts(doc.source(), &doc.program().stmts, class_name, &mut out);
687    out
688}
689
690fn collect_members_stmts(
691    source: &str,
692    stmts: &[Stmt<'_, '_>],
693    class_name: &str,
694    out: &mut ClassMembers,
695) -> Option<String> {
696    for stmt in stmts {
697        match &stmt.kind {
698            StmtKind::Class(c)
699                if c.name.as_ref().map(|n| n.to_string()) == Some(class_name.to_string()) =>
700            {
701                out.found = true;
702                // Check docblock for @property and @method tags
703                if let Some(raw) = docblock_before(source, stmt.span.start) {
704                    let db = parse_docblock(&raw);
705                    for prop in &db.properties {
706                        out.properties.push((prop.name.clone(), false));
707                    }
708                    for method in &db.methods {
709                        out.methods.push((method.name.clone(), method.is_static));
710                    }
711                }
712                for member in c.body.members.iter() {
713                    match &member.kind {
714                        ClassMemberKind::Method(m) => {
715                            out.methods.push((m.name.to_string(), m.is_static));
716                            if m.name == "__construct" {
717                                for p in m.params.iter() {
718                                    if p.visibility.is_some() {
719                                        out.properties.push((p.name.to_string(), false));
720                                        // AST does not expose the `readonly` flag on Param; scan the raw span text.
721                                        let param_src =
722                                            &source[p.span.start as usize..p.span.end as usize];
723                                        if param_src.contains("readonly") {
724                                            out.readonly_properties.push(p.name.to_string());
725                                        }
726                                    }
727                                }
728                            }
729                        }
730                        ClassMemberKind::Property(p) => {
731                            out.properties.push((p.name.to_string(), p.is_static));
732                            if p.is_readonly {
733                                out.readonly_properties.push(p.name.to_string());
734                            }
735                        }
736                        ClassMemberKind::ClassConst(c) => {
737                            out.constants.push(c.name.to_string());
738                        }
739                        ClassMemberKind::TraitUse(t) => {
740                            for name in t.traits.iter() {
741                                out.trait_uses.push(name.to_string_repr().to_string());
742                            }
743                        }
744                    }
745                }
746                return c.extends.as_ref().map(|n| n.to_string_repr().to_string());
747            }
748            StmtKind::Enum(e) if e.name == class_name => {
749                out.found = true;
750                let is_backed = e.scalar_type.is_some();
751                out.properties.push(("name".to_string(), false));
752                if is_backed {
753                    out.properties.push(("value".to_string(), false));
754                }
755                out.methods.push(("cases".to_string(), true));
756                if is_backed {
757                    out.methods.push(("from".to_string(), true));
758                    out.methods.push(("tryFrom".to_string(), true));
759                }
760                for member in e.body.members.iter() {
761                    match &member.kind {
762                        EnumMemberKind::Case(c) => {
763                            out.constants.push(c.name.to_string());
764                        }
765                        EnumMemberKind::Method(m) => {
766                            out.methods.push((m.name.to_string(), m.is_static));
767                        }
768                        EnumMemberKind::ClassConst(c) => {
769                            out.constants.push(c.name.to_string());
770                        }
771                        _ => {}
772                    }
773                }
774                return None; // enums have no parent class
775            }
776            StmtKind::Trait(t) if t.name == class_name => {
777                out.found = true;
778                for member in t.body.members.iter() {
779                    match &member.kind {
780                        ClassMemberKind::Method(m) => {
781                            out.methods.push((m.name.to_string(), m.is_static));
782                        }
783                        ClassMemberKind::Property(p) => {
784                            out.properties.push((p.name.to_string(), p.is_static));
785                        }
786                        ClassMemberKind::ClassConst(c) => {
787                            out.constants.push(c.name.to_string());
788                        }
789                        ClassMemberKind::TraitUse(t) => {
790                            for name in t.traits.iter() {
791                                out.trait_uses.push(name.to_string_repr().to_string());
792                            }
793                        }
794                    }
795                }
796                return None; // traits have no parent
797            }
798            StmtKind::Namespace(ns) => {
799                if let NamespaceBody::Braced(inner) = &ns.body {
800                    let result = collect_members_stmts(source, &inner.stmts, class_name, out);
801                    if result.is_some() {
802                        return result;
803                    }
804                }
805            }
806            _ => {}
807        }
808    }
809    None
810}
811
812/// Return the `@mixin` class names declared in `class_name`'s docblock.
813pub fn mixin_classes_of(doc: &ParsedDoc, class_name: &str) -> Vec<String> {
814    let source = doc.source();
815    mixin_classes_in_stmts(source, &doc.program().stmts, class_name)
816}
817
818fn mixin_classes_in_stmts(source: &str, stmts: &[Stmt<'_, '_>], class_name: &str) -> Vec<String> {
819    for stmt in stmts {
820        match &stmt.kind {
821            StmtKind::Class(c)
822                if c.name.as_ref().map(|n| n.to_string()) == Some(class_name.to_string()) =>
823            {
824                if let Some(raw) = docblock_before(source, stmt.span.start) {
825                    return parse_docblock(&raw).mixins;
826                }
827                return vec![];
828            }
829            StmtKind::Namespace(ns) => {
830                if let NamespaceBody::Braced(inner) = &ns.body {
831                    let found = mixin_classes_in_stmts(source, &inner.stmts, class_name);
832                    if !found.is_empty() {
833                        return found;
834                    }
835                }
836            }
837            _ => {}
838        }
839    }
840    vec![]
841}
842
843/// Return the name of the class whose body contains `position`, or `None`.
844pub fn enclosing_class_at(_source: &str, doc: &ParsedDoc, position: Position) -> Option<String> {
845    let sv = doc.view();
846    enclosing_class_in_stmts(sv, &doc.program().stmts, position)
847}
848
849/// Return the LSP range of the class/interface/trait/enum declaration
850/// whose body contains `position`, or `None` if the cursor is outside any.
851/// Used by linked-editing to scope same-name member rewrites to the
852/// enclosing class instead of every class in the file.
853pub fn enclosing_class_range_at(
854    doc: &ParsedDoc,
855    position: Position,
856) -> Option<tower_lsp::lsp_types::Range> {
857    let sv = doc.view();
858    enclosing_class_range_in_stmts(sv, &doc.program().stmts, position)
859}
860
861/// Return the LSP range of every class/interface/trait/enum declaration in
862/// the file (recursing into braced-namespace bodies). Used by linked-editing
863/// to drop highlights that fall inside an *other* class than the cursor's.
864pub fn collect_all_class_ranges(doc: &ParsedDoc) -> Vec<tower_lsp::lsp_types::Range> {
865    let sv = doc.view();
866    let mut out = Vec::new();
867    collect_class_ranges_in_stmts(sv, &doc.program().stmts, &mut out);
868    out
869}
870
871fn collect_class_ranges_in_stmts(
872    sv: SourceView<'_>,
873    stmts: &[Stmt<'_, '_>],
874    out: &mut Vec<tower_lsp::lsp_types::Range>,
875) {
876    for stmt in stmts {
877        match &stmt.kind {
878            StmtKind::Class(_)
879            | StmtKind::Interface(_)
880            | StmtKind::Trait(_)
881            | StmtKind::Enum(_) => {
882                out.push(sv.range_of(stmt.span));
883            }
884            StmtKind::Namespace(ns) => {
885                if let NamespaceBody::Braced(inner) = &ns.body {
886                    collect_class_ranges_in_stmts(sv, &inner.stmts, out);
887                }
888            }
889            _ => {}
890        }
891    }
892}
893
894fn enclosing_class_range_in_stmts(
895    sv: SourceView<'_>,
896    stmts: &[Stmt<'_, '_>],
897    pos: Position,
898) -> Option<tower_lsp::lsp_types::Range> {
899    for stmt in stmts {
900        match &stmt.kind {
901            StmtKind::Class(_)
902            | StmtKind::Interface(_)
903            | StmtKind::Trait(_)
904            | StmtKind::Enum(_) => {
905                let r = sv.range_of(stmt.span);
906                if pos.line >= r.start.line && pos.line <= r.end.line {
907                    return Some(r);
908                }
909            }
910            StmtKind::Namespace(ns) => {
911                if let NamespaceBody::Braced(inner) = &ns.body
912                    && let Some(r) = enclosing_class_range_in_stmts(sv, &inner.stmts, pos)
913                {
914                    return Some(r);
915                }
916            }
917            _ => {}
918        }
919    }
920    None
921}
922
923fn enclosing_class_in_stmts(
924    sv: SourceView<'_>,
925    stmts: &[Stmt<'_, '_>],
926    pos: Position,
927) -> Option<String> {
928    for stmt in stmts {
929        match &stmt.kind {
930            StmtKind::Class(c) => {
931                let start = sv.position_of(stmt.span.start).line;
932                let end = sv.position_of(stmt.span.end).line;
933                if pos.line >= start && pos.line <= end {
934                    return c.name.map(|n| n.to_string());
935                }
936            }
937            StmtKind::Interface(i) => {
938                let start = sv.position_of(stmt.span.start).line;
939                let end = sv.position_of(stmt.span.end).line;
940                if pos.line >= start && pos.line <= end {
941                    return Some(i.name.to_string());
942                }
943            }
944            StmtKind::Trait(t) => {
945                let start = sv.position_of(stmt.span.start).line;
946                let end = sv.position_of(stmt.span.end).line;
947                if pos.line >= start && pos.line <= end {
948                    return Some(t.name.to_string());
949                }
950            }
951            StmtKind::Enum(e) => {
952                let start = sv.position_of(stmt.span.start).line;
953                let end = sv.position_of(stmt.span.end).line;
954                if pos.line >= start && pos.line <= end {
955                    return Some(e.name.to_string());
956                }
957            }
958            StmtKind::Namespace(ns) => {
959                if let NamespaceBody::Braced(inner) = &ns.body
960                    && let Some(found) = enclosing_class_in_stmts(sv, &inner.stmts, pos)
961                {
962                    return Some(found);
963                }
964            }
965            _ => {}
966        }
967    }
968    None
969}
970
971/// Return the parameter names of the function or method named `func_name`.
972pub fn params_of_function(doc: &ParsedDoc, func_name: &str) -> Vec<String> {
973    let mut out = Vec::new();
974    collect_params_stmts(&doc.program().stmts, func_name, &mut out);
975    out
976}
977
978/// Return the parameter names of `method_name` on class `class_name`.
979/// Primarily used to offer named-argument completions for attribute constructors.
980pub fn params_of_method(doc: &ParsedDoc, class_name: &str, method_name: &str) -> Vec<String> {
981    let mut out = Vec::new();
982    collect_method_params_stmts(&doc.program().stmts, class_name, method_name, &mut out);
983    out
984}
985
986fn collect_method_params_stmts(
987    stmts: &[php_ast::Stmt<'_, '_>],
988    class_name: &str,
989    method_name: &str,
990    out: &mut Vec<String>,
991) {
992    for stmt in stmts {
993        match &stmt.kind {
994            StmtKind::Class(c)
995                if c.name.as_ref().map(|n| n.to_string()) == Some(class_name.to_string()) =>
996            {
997                for member in c.body.members.iter() {
998                    if let ClassMemberKind::Method(m) = &member.kind
999                        && m.name == method_name
1000                    {
1001                        for p in m.params.iter() {
1002                            out.push(p.name.to_string());
1003                        }
1004                        return;
1005                    }
1006                }
1007            }
1008            StmtKind::Namespace(ns) => {
1009                if let NamespaceBody::Braced(inner) = &ns.body {
1010                    collect_method_params_stmts(&inner.stmts, class_name, method_name, out);
1011                }
1012            }
1013            _ => {}
1014        }
1015    }
1016}
1017
1018/// Returns `true` if `class_name` is declared as an `enum` in `doc`.
1019pub fn is_enum(doc: &ParsedDoc, class_name: &str) -> bool {
1020    is_enum_in_stmts(&doc.program().stmts, class_name)
1021}
1022
1023fn is_enum_in_stmts(stmts: &[Stmt<'_, '_>], name: &str) -> bool {
1024    for stmt in stmts {
1025        match &stmt.kind {
1026            StmtKind::Enum(e) if e.name == name => return true,
1027            StmtKind::Namespace(ns) => {
1028                if let NamespaceBody::Braced(inner) = &ns.body
1029                    && is_enum_in_stmts(&inner.stmts, name)
1030                {
1031                    return true;
1032                }
1033            }
1034            _ => {}
1035        }
1036    }
1037    false
1038}
1039
1040/// Returns `true` if `class_name` is a *backed* enum (`enum Foo: string` /
1041/// `enum Foo: int`) in `doc`.  Backed enums have a `->value` property.
1042pub fn is_backed_enum(doc: &ParsedDoc, class_name: &str) -> bool {
1043    is_backed_enum_in_stmts(&doc.program().stmts, class_name)
1044}
1045
1046fn is_backed_enum_in_stmts(stmts: &[Stmt<'_, '_>], name: &str) -> bool {
1047    for stmt in stmts {
1048        match &stmt.kind {
1049            StmtKind::Enum(e) if e.name == name => return e.scalar_type.is_some(),
1050            StmtKind::Namespace(ns) => {
1051                if let NamespaceBody::Braced(inner) = &ns.body
1052                    && is_backed_enum_in_stmts(&inner.stmts, name)
1053                {
1054                    return true;
1055                }
1056            }
1057            _ => {}
1058        }
1059    }
1060    false
1061}
1062
1063fn collect_params_stmts(stmts: &[Stmt<'_, '_>], func_name: &str, out: &mut Vec<String>) {
1064    for stmt in stmts {
1065        match &stmt.kind {
1066            StmtKind::Function(f) if f.name == func_name => {
1067                for p in f.params.iter() {
1068                    out.push(p.name.to_string());
1069                }
1070                return;
1071            }
1072            StmtKind::Class(c) => {
1073                for member in c.body.members.iter() {
1074                    if let ClassMemberKind::Method(m) = &member.kind
1075                        && m.name == func_name
1076                    {
1077                        for p in m.params.iter() {
1078                            out.push(p.name.to_string());
1079                        }
1080                        return;
1081                    }
1082                }
1083            }
1084            StmtKind::Namespace(ns) => {
1085                if let NamespaceBody::Braced(inner) = &ns.body {
1086                    collect_params_stmts(&inner.stmts, func_name, out);
1087                }
1088            }
1089            _ => {}
1090        }
1091    }
1092}
1093
1094#[cfg(test)]
1095mod tests {
1096    use super::*;
1097
1098    #[test]
1099    fn infers_type_from_new_expression() {
1100        let src = "<?php\n$obj = new Foo();";
1101        let doc = ParsedDoc::parse(src.to_string());
1102        let tm = TypeMap::from_doc(&doc);
1103        assert_eq!(tm.get("$obj"), Some("Foo"));
1104    }
1105
1106    #[test]
1107    fn unknown_variable_returns_none() {
1108        let src = "<?php\n$obj = new Foo();";
1109        let doc = ParsedDoc::parse(src.to_string());
1110        let tm = TypeMap::from_doc(&doc);
1111        assert!(tm.get("$other").is_none());
1112    }
1113
1114    #[test]
1115    fn multiple_assignments() {
1116        let src = "<?php\n$a = new Foo();\n$b = new Bar();";
1117        let doc = ParsedDoc::parse(src.to_string());
1118        let tm = TypeMap::from_doc(&doc);
1119        assert_eq!(tm.get("$a"), Some("Foo"));
1120        assert_eq!(tm.get("$b"), Some("Bar"));
1121    }
1122
1123    #[test]
1124    fn later_assignment_overwrites() {
1125        let src = "<?php\n$a = new Foo();\n$a = new Bar();";
1126        let doc = ParsedDoc::parse(src.to_string());
1127        let tm = TypeMap::from_doc(&doc);
1128        assert_eq!(tm.get("$a"), Some("Bar"));
1129    }
1130
1131    #[test]
1132    fn infers_type_from_typed_param() {
1133        let src = "<?php\nfunction process(Mailer $mailer): void { $mailer-> }";
1134        let doc = ParsedDoc::parse(src.to_string());
1135        let tm = TypeMap::from_doc(&doc);
1136        assert_eq!(tm.get("$mailer"), Some("Mailer"));
1137    }
1138
1139    #[test]
1140    fn parent_class_name_finds_parent() {
1141        let src = "<?php\nclass Base {}\nclass Child extends Base {}";
1142        let doc = ParsedDoc::parse(src.to_string());
1143        assert_eq!(parent_class_name(&doc, "Child"), Some("Base".to_string()));
1144    }
1145
1146    #[test]
1147    fn parent_class_name_returns_none_for_top_level() {
1148        let src = "<?php\nclass Base {}";
1149        let doc = ParsedDoc::parse(src.to_string());
1150        assert!(parent_class_name(&doc, "Base").is_none());
1151    }
1152
1153    #[test]
1154    fn members_of_class_includes_parent_field() {
1155        let src = "<?php\nclass Base {}\nclass Child extends Base {}";
1156        let doc = ParsedDoc::parse(src.to_string());
1157        let m = members_of_class(&doc, "Child");
1158        assert_eq!(m.parent.as_deref(), Some("Base"));
1159    }
1160
1161    #[test]
1162    fn members_of_class_finds_methods() {
1163        let src = "<?php\nclass Calc { public function add() {} public function sub() {} }";
1164        let doc = ParsedDoc::parse(src.to_string());
1165        let members = members_of_class(&doc, "Calc");
1166        let names: Vec<&str> = members.methods.iter().map(|(n, _)| n.as_str()).collect();
1167        assert!(names.contains(&"add"), "missing 'add'");
1168        assert!(names.contains(&"sub"), "missing 'sub'");
1169    }
1170
1171    #[test]
1172    fn members_of_unknown_class_is_empty() {
1173        let src = "<?php\nclass Calc { public function add() {} }";
1174        let doc = ParsedDoc::parse(src.to_string());
1175        let members = members_of_class(&doc, "Unknown");
1176        assert!(members.methods.is_empty());
1177    }
1178
1179    #[test]
1180    fn constructor_promoted_params_appear_as_properties() {
1181        let src = "<?php\nclass Point {\n    public function __construct(\n        public float $x,\n        public float $y,\n    ) {}\n}";
1182        let doc = ParsedDoc::parse(src.to_string());
1183        let members = members_of_class(&doc, "Point");
1184        let prop_names: Vec<&str> = members.properties.iter().map(|(n, _)| n.as_str()).collect();
1185        assert!(
1186            prop_names.contains(&"x"),
1187            "promoted param x should be a property"
1188        );
1189        assert!(
1190            prop_names.contains(&"y"),
1191            "promoted param y should be a property"
1192        );
1193    }
1194
1195    #[test]
1196    fn promoted_readonly_params_appear_in_readonly_properties() {
1197        let src = "<?php\nclass User {\n    public function __construct(\n        public readonly string $name,\n        public int $age,\n    ) {}\n}";
1198        let doc = ParsedDoc::parse(src.to_string());
1199        let members = members_of_class(&doc, "User");
1200        let prop_names: Vec<&str> = members.properties.iter().map(|(n, _)| n.as_str()).collect();
1201        assert!(
1202            prop_names.contains(&"name"),
1203            "promoted param name should be a property"
1204        );
1205        assert!(
1206            prop_names.contains(&"age"),
1207            "promoted param age should be a property"
1208        );
1209        assert!(
1210            members.readonly_properties.contains(&"name".to_string()),
1211            "readonly promoted param name should be in readonly_properties"
1212        );
1213        assert!(
1214            !members.readonly_properties.contains(&"age".to_string()),
1215            "non-readonly promoted param age should not be in readonly_properties"
1216        );
1217    }
1218
1219    #[test]
1220    fn enum_instance_members_include_name() {
1221        let src = "<?php\nenum Status { case Active; case Inactive; }";
1222        let doc = ParsedDoc::parse(src.to_string());
1223        let members = members_of_class(&doc, "Status");
1224        let prop_names: Vec<&str> = members.properties.iter().map(|(n, _)| n.as_str()).collect();
1225        assert!(
1226            prop_names.contains(&"name"),
1227            "pure enum should expose ->name"
1228        );
1229        assert!(
1230            !prop_names.contains(&"value"),
1231            "pure enum should not expose ->value"
1232        );
1233    }
1234
1235    #[test]
1236    fn backed_enum_exposes_value_and_factory_methods() {
1237        let src = "<?php\nenum Color: string { case Red = 'red'; }";
1238        let doc = ParsedDoc::parse(src.to_string());
1239        let members = members_of_class(&doc, "Color");
1240        let prop_names: Vec<&str> = members.properties.iter().map(|(n, _)| n.as_str()).collect();
1241        let method_names: Vec<&str> = members.methods.iter().map(|(n, _)| n.as_str()).collect();
1242        assert!(
1243            prop_names.contains(&"value"),
1244            "backed enum should expose ->value"
1245        );
1246        assert!(
1247            method_names.contains(&"from"),
1248            "backed enum should have ::from()"
1249        );
1250        assert!(
1251            method_names.contains(&"tryFrom"),
1252            "backed enum should have ::tryFrom()"
1253        );
1254        assert!(
1255            method_names.contains(&"cases"),
1256            "enum should have ::cases()"
1257        );
1258    }
1259
1260    #[test]
1261    fn enum_cases_appear_as_constants() {
1262        let src = "<?php\nenum Status { case Active; case Inactive; }";
1263        let doc = ParsedDoc::parse(src.to_string());
1264        let members = members_of_class(&doc, "Status");
1265        assert!(members.constants.contains(&"Active".to_string()));
1266        assert!(members.constants.contains(&"Inactive".to_string()));
1267    }
1268
1269    #[test]
1270    fn trait_members_are_collected() {
1271        let src = "<?php\ntrait Logging { public function log() {} public string $logFile; }";
1272        let doc = ParsedDoc::parse(src.to_string());
1273        let members = members_of_class(&doc, "Logging");
1274        let method_names: Vec<&str> = members.methods.iter().map(|(n, _)| n.as_str()).collect();
1275        let prop_names: Vec<&str> = members.properties.iter().map(|(n, _)| n.as_str()).collect();
1276        assert!(
1277            method_names.contains(&"log"),
1278            "trait method log should be collected"
1279        );
1280        assert!(
1281            prop_names.contains(&"logFile"),
1282            "trait property logFile should be collected"
1283        );
1284    }
1285
1286    #[test]
1287    fn class_with_trait_use_lists_trait() {
1288        let src = "<?php\ntrait Logging { public function log() {} }\nclass App { use Logging; }";
1289        let doc = ParsedDoc::parse(src.to_string());
1290        let members = members_of_class(&doc, "App");
1291        assert!(
1292            members.trait_uses.contains(&"Logging".to_string()),
1293            "should list used trait"
1294        );
1295    }
1296
1297    #[test]
1298    fn var_docblock_with_explicit_varname_infers_type() {
1299        let src = "<?php\n/** @var Mailer $mailer */\n$mailer = $container->get('mailer');";
1300        let doc = ParsedDoc::parse(src.to_string());
1301        let tm = TypeMap::from_doc(&doc);
1302        assert_eq!(
1303            tm.get("$mailer"),
1304            Some("Mailer"),
1305            "@var with explicit name should map the variable"
1306        );
1307    }
1308
1309    #[test]
1310    fn var_docblock_without_varname_infers_from_assignment() {
1311        let src = "<?php\n/** @var Repository */\n$repo = $this->getRepository();";
1312        let doc = ParsedDoc::parse(src.to_string());
1313        let tm = TypeMap::from_doc(&doc);
1314        assert_eq!(
1315            tm.get("$repo"),
1316            Some("Repository"),
1317            "@var without name should use assignment LHS"
1318        );
1319    }
1320
1321    #[test]
1322    fn var_docblock_does_not_map_primitive_types() {
1323        let src = "<?php\n/** @var string */\n$name = 'hello';";
1324        let doc = ParsedDoc::parse(src.to_string());
1325        let tm = TypeMap::from_doc(&doc);
1326        // Primitives (lowercase) should not be mapped as class names.
1327        assert!(
1328            tm.get("$name").is_none(),
1329            "primitive @var should not produce a class mapping"
1330        );
1331    }
1332
1333    #[test]
1334    fn var_nullable_docblock_maps_to_class() {
1335        // `@var ?Foo $x` is now normalised to `Foo|null` by the mir parser.
1336        // The type_map must still infer the class name `Foo`, not `Foo|null`.
1337        let src = "<?php\n/** @var ?Mailer $mailer */\n$mailer = null;";
1338        let doc = ParsedDoc::parse(src.to_string());
1339        let tm = TypeMap::from_doc(&doc);
1340        assert_eq!(
1341            tm.get("$mailer"),
1342            Some("Mailer"),
1343            "@var ?Foo should map to 'Foo', not 'Foo|null'"
1344        );
1345    }
1346
1347    #[test]
1348    fn var_union_docblock_maps_first_class() {
1349        // `@var Foo|null $x` — first class-type component should be used.
1350        let src = "<?php\n/** @var Repository|null $repo */\n$repo = null;";
1351        let doc = ParsedDoc::parse(src.to_string());
1352        let tm = TypeMap::from_doc(&doc);
1353        assert_eq!(
1354            tm.get("$repo"),
1355            Some("Repository"),
1356            "@var Foo|null should map to 'Foo', not 'Foo|null'"
1357        );
1358    }
1359
1360    #[test]
1361    fn is_enum_pure() {
1362        let src = "<?php\nenum Suit { case Hearts; case Clubs; }";
1363        let doc = ParsedDoc::parse(src.to_string());
1364        assert!(is_enum(&doc, "Suit"));
1365        assert!(!is_backed_enum(&doc, "Suit"));
1366    }
1367
1368    #[test]
1369    fn is_backed_enum_string() {
1370        let src = "<?php\nenum Status: string { case Active = 'active'; }";
1371        let doc = ParsedDoc::parse(src.to_string());
1372        assert!(is_enum(&doc, "Status"));
1373        assert!(is_backed_enum(&doc, "Status"));
1374    }
1375
1376    #[test]
1377    fn is_enum_false_for_class() {
1378        let src = "<?php\nclass Foo {}";
1379        let doc = ParsedDoc::parse(src.to_string());
1380        assert!(!is_enum(&doc, "Foo"));
1381        assert!(!is_backed_enum(&doc, "Foo"));
1382    }
1383
1384    #[test]
1385    fn closure_use_var_type_is_available_inside_body() {
1386        let src = "<?php\n$svc = new PaymentService();\n$fn = function() use ($svc) { $svc->process(); };";
1387        let doc = ParsedDoc::parse(src.to_string());
1388        let tm = TypeMap::from_doc(&doc);
1389        assert_eq!(
1390            tm.get("$svc"),
1391            Some("PaymentService"),
1392            "captured use variable should retain its outer type inside closure body"
1393        );
1394    }
1395
1396    #[test]
1397    fn closure_use_var_inner_assignment_does_not_override_outer_type() {
1398        // If inside a closure we assign $svc = new Other(), the outer $svc type
1399        // should be restored after walking the closure body (or_insert semantics).
1400        let src = "<?php\n$svc = new PaymentService();\n$fn = function() use ($svc) { $svc = new OtherService(); };";
1401        let doc = ParsedDoc::parse(src.to_string());
1402        let tm = TypeMap::from_doc(&doc);
1403        // The snapshot restore ensures $svc retains PaymentService for the outer scope.
1404        assert_eq!(
1405            tm.get("$svc"),
1406            Some("PaymentService"),
1407            "outer type should not be overwritten by inner assignment in closure"
1408        );
1409    }
1410
1411    #[test]
1412    fn param_docblock_type_inferred() {
1413        let src = "<?php\n/**\n * @param Mailer $mailer\n */\nfunction send($mailer) { $mailer-> }";
1414        let doc = ParsedDoc::parse(src.to_string());
1415        let tm = TypeMap::from_doc(&doc);
1416        assert_eq!(tm.get("$mailer"), Some("Mailer"));
1417    }
1418
1419    #[test]
1420    fn param_docblock_does_not_override_ast_hint() {
1421        let src = "<?php\n/**\n * @param OtherClass $x\n */\nfunction foo(Foo $x) {}";
1422        let doc = ParsedDoc::parse(src.to_string());
1423        let tm = TypeMap::from_doc(&doc);
1424        // AST type hint takes precedence over docblock (AST processed after, overwrites)
1425        assert_eq!(tm.get("$x"), Some("Foo"));
1426    }
1427
1428    #[test]
1429    fn not_null_check_preserves_existing_type() {
1430        let src = "<?php\n$x = new Foo();\nif ($x !== null) { $x-> }";
1431        let doc = ParsedDoc::parse(src.to_string());
1432        let tm = TypeMap::from_doc(&doc);
1433        assert_eq!(tm.get("$x"), Some("Foo"));
1434    }
1435
1436    #[test]
1437    fn docblock_property_appears_in_members() {
1438        let src =
1439            "<?php\n/**\n * @property string $email\n * @property-read int $id\n */\nclass User {}";
1440        let doc = ParsedDoc::parse(src.to_string());
1441        let members = members_of_class(&doc, "User");
1442        let props: Vec<&str> = members.properties.iter().map(|(n, _)| n.as_str()).collect();
1443        assert!(props.contains(&"email"));
1444        assert!(props.contains(&"id"));
1445    }
1446
1447    #[test]
1448    fn docblock_method_appears_in_members() {
1449        let src = "<?php\n/**\n * @method User find(int $id)\n * @method static Builder where(string $col, mixed $val)\n */\nclass Model {}";
1450        let doc = ParsedDoc::parse(src.to_string());
1451        let members = members_of_class(&doc, "Model");
1452        let method_names: Vec<&str> = members.methods.iter().map(|(n, _)| n.as_str()).collect();
1453        assert!(method_names.contains(&"find"));
1454        assert!(method_names.contains(&"where"));
1455        let where_static = members
1456            .methods
1457            .iter()
1458            .find(|(n, _)| n == "where")
1459            .map(|(_, s)| *s);
1460        assert_eq!(where_static, Some(true));
1461    }
1462
1463    #[test]
1464    fn union_type_param_maps_both_classes() {
1465        // function f(Foo|Bar $x) — both Foo and Bar should be in the union type string
1466        let src = "<?php\nfunction f(Foo|Bar $x) {}";
1467        let doc = ParsedDoc::parse(src.to_string());
1468        let tm = TypeMap::from_doc(&doc);
1469        let val = tm.get("$x").expect("$x should be in the type map");
1470        assert!(
1471            val.contains("Foo"),
1472            "union type should contain 'Foo', got: {}",
1473            val
1474        );
1475        assert!(
1476            val.contains("Bar"),
1477            "union type should contain 'Bar', got: {}",
1478            val
1479        );
1480    }
1481
1482    #[test]
1483    fn nullable_param_resolves_to_class() {
1484        // function f(?Foo $x) — $x should map to Foo (nullable stripped)
1485        let src = "<?php\nfunction f(?Foo $x) {}";
1486        let doc = ParsedDoc::parse(src.to_string());
1487        let tm = TypeMap::from_doc(&doc);
1488        assert_eq!(
1489            tm.get("$x"),
1490            Some("Foo"),
1491            "nullable type hint ?Foo should map $x to Foo"
1492        );
1493    }
1494
1495    #[test]
1496    fn null_assignment_does_not_overwrite_class() {
1497        // $x = new Foo(); $x = null; — $x type should stay Foo because
1498        // assigning null does not overwrite a known class type in the single-pass map.
1499        let src = "<?php\n$x = new Foo();\n$x = null;\n";
1500        let doc = ParsedDoc::parse(src.to_string());
1501        let tm = TypeMap::from_doc(&doc);
1502        // The single-pass type map does not treat null as a class, so the last
1503        // successful class assignment (Foo) persists.
1504        assert_eq!(
1505            tm.get("$x"),
1506            Some("Foo"),
1507            "$x should retain its Foo type after being assigned null"
1508        );
1509    }
1510
1511    #[test]
1512    fn infers_type_from_assignment_inside_trait_method() {
1513        let src = "<?php\ntrait Builder { public function make(): void { $obj = new Widget(); } }";
1514        let doc = ParsedDoc::parse(src.to_string());
1515        let tm = TypeMap::from_doc(&doc);
1516        assert_eq!(
1517            tm.get("$obj"),
1518            Some("Widget"),
1519            "type map should walk into trait method bodies"
1520        );
1521    }
1522
1523    #[test]
1524    fn infers_type_from_assignment_inside_enum_method() {
1525        let src = "<?php\nenum Color { case Red; public function make(): void { $obj = new Palette(); } }";
1526        let doc = ParsedDoc::parse(src.to_string());
1527        let tm = TypeMap::from_doc(&doc);
1528        assert_eq!(
1529            tm.get("$obj"),
1530            Some("Palette"),
1531            "type map should walk into enum method bodies"
1532        );
1533    }
1534
1535    // ── Gap 1: generic params stripped from class types ───────────────────────
1536
1537    #[test]
1538    fn generic_type_annotation_stripped_to_base_class() {
1539        // `@var Collection<User> $coll` — the TypeMap fallback should store
1540        // "Collection", not "Collection<User>", so member lookups succeed.
1541        let src = "<?php\n/** @var Collection<User> $coll */\n$coll = get();";
1542        let doc = ParsedDoc::parse(src.to_string());
1543        let tm = TypeMap::from_doc(&doc);
1544        assert_eq!(
1545            tm.get("$coll"),
1546            Some("Collection"),
1547            "generic param should be stripped; expected Collection, not Collection<User>"
1548        );
1549    }
1550
1551    // ── Gap 4: list<T> element type propagated through foreach ────────────────
1552
1553    #[test]
1554    fn list_var_annotation_maps_to_element_type() {
1555        // `@var list<Widget> $items` — TypeMap should store "Widget" for $items
1556        // so the foreach propagation step has a type to forward.
1557        let src = "<?php\n/** @var list<Widget> $items */\n$items = get();";
1558        let doc = ParsedDoc::parse(src.to_string());
1559        let tm = TypeMap::from_doc(&doc);
1560        assert_eq!(
1561            tm.get("$items"),
1562            Some("Widget"),
1563            "element type Widget should be extracted from list<Widget>"
1564        );
1565    }
1566
1567    #[test]
1568    fn array_element_annotation_maps_to_element_type() {
1569        // `@var array<Widget> $map` (single-param form) — TypeMap should store "Widget".
1570        // Note: `array<int, Widget>` with a space inside the generics is not handled
1571        // by the whitespace-split @var body extractor in docblock.rs; use the
1572        // single-param form or list<T> for this fallback path.
1573        let src = "<?php\n/** @var array<Widget> $map */\n$map = get();";
1574        let doc = ParsedDoc::parse(src.to_string());
1575        let tm = TypeMap::from_doc(&doc);
1576        assert_eq!(
1577            tm.get("$map"),
1578            Some("Widget"),
1579            "element type Widget should be extracted from array<Widget>"
1580        );
1581    }
1582
1583    #[test]
1584    fn foreach_value_var_inherits_element_type() {
1585        // `@var list<Widget> $items` followed by `foreach ($items as $w)` —
1586        // TypeMap should map `$w` to "Widget" in the fallback path.
1587        let src = "<?php\n/** @var list<Widget> $items */\n$items = get();\nforeach ($items as $w) { $w; }";
1588        let doc = ParsedDoc::parse(src.to_string());
1589        let tm = TypeMap::from_doc(&doc);
1590        assert_eq!(
1591            tm.get("$w"),
1592            Some("Widget"),
1593            "$w should inherit Widget element type inside foreach"
1594        );
1595    }
1596
1597    // ── Gap 2: @psalm-type alias expansion ────────────────────────────────────
1598
1599    #[test]
1600    fn psalm_type_alias_expanded_for_param() {
1601        // Class docblock declares `@psalm-type Result = Success|Failure`.
1602        // The method `@param Result $r` should expand to both classes.
1603        let src = r#"<?php
1604/** @psalm-type Result = Success|Failure */
1605class Processor {
1606    /** @param Result $r */
1607    public function handle($r): void {}
1608}"#;
1609        let doc = ParsedDoc::parse(src.to_string());
1610        let tm = TypeMap::from_doc(&doc);
1611        let val = tm.get("$r").expect("$r should be in the type map");
1612        assert!(
1613            val.contains("Success"),
1614            "alias should expand to include Success; got: {val}"
1615        );
1616        assert!(
1617            val.contains("Failure"),
1618            "alias should expand to include Failure; got: {val}"
1619        );
1620    }
1621
1622    // ── Gap 6: first-class callable mapped to Closure ────────────────────────
1623
1624    #[test]
1625    fn first_class_callable_maps_to_closure() {
1626        let src = "<?php\n$fn = strlen(...);";
1627        let doc = ParsedDoc::parse(src.to_string());
1628        let tm = TypeMap::from_doc(&doc);
1629        assert_eq!(
1630            tm.get("$fn"),
1631            Some("Closure"),
1632            "first-class callable should be mapped to Closure"
1633        );
1634    }
1635
1636    #[test]
1637    fn first_class_method_callable_maps_to_closure() {
1638        let src = "<?php\n$obj = new Foo();\n$m = $obj->bar(...);";
1639        let doc = ParsedDoc::parse(src.to_string());
1640        let tm = TypeMap::from_doc(&doc);
1641        assert_eq!(
1642            tm.get("$m"),
1643            Some("Closure"),
1644            "method first-class callable should be mapped to Closure"
1645        );
1646    }
1647}