php-lsp 0.7.0

A PHP Language Server Protocol implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
use std::collections::{HashMap, HashSet};
use std::ops::ControlFlow;
use std::sync::Arc;

use php_ast::visitor::{Visitor, walk_stmt};
use php_ast::{
    ClassMemberKind, EnumMemberKind, ExprKind, NamespaceBody, Span, Stmt, StmtKind, UseKind,
};
use rayon::prelude::*;
use tower_lsp::lsp_types::{Location, Position, Range, Url};

use crate::ast::{ParsedDoc, str_offset_in_range};
use crate::util::utf16_code_units;
use crate::walk::{
    all_class_ref_names_in_stmts, class_refs_in_stmts, constant_refs_in_stmts,
    fqn_new_class_refs_in_stmts, function_refs_in_stmts, global_constant_refs_in_stmts,
    method_refs_in_stmts, new_refs_in_stmts, property_refs_in_stmts, refs_in_stmts,
    refs_in_stmts_with_use,
};

/// Callback signature for the mir-codebase reference-lookup fast path:
/// `(key) -> Vec<(file_uri, start_byte, end_byte)>`.
pub type RefLookup<'a> = dyn Fn(&str) -> Vec<(Arc<str>, u32, u16, u16)> + 'a;

/// What kind of symbol the cursor is on.  Used to dispatch to the
/// appropriate semantic walker so that, e.g., searching for `get` as a
/// *method* doesn't return free-function calls named `get`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SymbolKind {
    /// A free (top-level) function.
    Function,
    /// An instance or static method (`->name`, `?->name`, `::name`).
    Method,
    /// A class, interface, trait, or enum name used as a type.
    Class,
    /// A class / trait property (`->name`, `?->name`, promoted or declared).
    Property,
    /// A class, interface, enum, or trait constant (`Class::CONST`, `self::CONST`).
    Constant,
}

fn class_has_ancestor(
    codebase: &mir_analyzer::db::MirDbStorage,
    class_fqcn: &str,
    target_fqcn: &str,
) -> bool {
    mir_analyzer::db::extends_or_implements(codebase, class_fqcn, target_fqcn)
}

/// Find all locations where `word` is referenced across the given documents.
/// If `include_declaration` is true, also includes the declaration site.
/// Pass `kind` to restrict results to a particular symbol category; `None`
/// falls back to the original word-based walker (better some results than none).
pub fn find_references(
    word: &str,
    all_docs: &[(Url, Arc<ParsedDoc>)],
    include_declaration: bool,
    kind: Option<SymbolKind>,
) -> Vec<Location> {
    find_references_inner(word, all_docs, include_declaration, false, kind, None)
}

/// Like [`find_references`] but narrows scanning to docs whose namespace +
/// `use` imports would resolve `word` to `target_fqn`. Used by
/// `textDocument/references` for the AST fallback so it doesn't match
/// same-short-name symbols in unrelated namespaces.
pub fn find_references_with_target(
    word: &str,
    all_docs: &[(Url, Arc<ParsedDoc>)],
    include_declaration: bool,
    kind: Option<SymbolKind>,
    target_fqn: &str,
) -> Vec<Location> {
    // Default: include `use` statement spans so callers that pass
    // `kind=None` (notably the rename handler) get their use-import edits.
    // For typed kinds we want the kind-specific walker (so a Method search
    // doesn't pick up free functions sharing the name); the general walker
    // would falsely widen those results.
    let include_use = kind.is_none();
    find_references_inner(
        word,
        all_docs,
        include_declaration,
        include_use,
        kind,
        Some(target_fqn),
    )
}

/// Like `find_references` but also includes `use` statement spans.
/// Used by rename so that `use Foo;` statements are also updated.
/// Always uses the general walker (rename must update all occurrence kinds).
pub fn find_references_with_use(
    word: &str,
    all_docs: &[(Url, Arc<ParsedDoc>)],
    include_declaration: bool,
) -> Vec<Location> {
    find_references_inner(word, all_docs, include_declaration, true, None, None)
}

/// Find only `new ClassName(...)` instantiation sites across all docs.
///
/// Used by the `__construct` references handler — `SymbolKind::Class` (the normal
/// class-kind path) is too broad because mir's `ClassReference` key covers type
/// hints, `instanceof`, `extends`, and `implements` in addition to `new` calls.
/// This function walks the AST using `new_refs_in_stmts` which only emits spans
/// for `ExprKind::New` nodes, giving the caller exactly the call sites.
///
/// `class_fqn` is the fully-qualified name (e.g. `"Alpha\\Widget"`) used to
/// filter files where the short name resolves to a different class. Pass `None`
/// for global-namespace classes.
pub fn find_constructor_references(
    short_name: &str,
    all_docs: &[(Url, Arc<ParsedDoc>)],
    class_fqn: Option<&str>,
) -> Vec<Location> {
    all_docs
        .par_iter()
        .flat_map_iter(|(uri, doc)| {
            // Skip files that can't reference the target unless they may use the FQN
            // directly (without a `use` statement). FQN-qualified identifiers in the
            // AST are disambiguated inside `new_refs_in_stmts` via `class_fqn`.
            if let Some(fqn) = class_fqn
                && !doc_can_reference_target(doc, short_name, fqn)
                && !doc.view().source().contains(fqn.trim_start_matches('\\'))
            {
                return Vec::new();
            }
            let mut spans = Vec::new();
            new_refs_in_stmts(&doc.program().stmts, short_name, class_fqn, &mut spans);
            let sv = doc.view();
            spans
                .into_iter()
                .map(|span| {
                    let start = sv.position_of(span.start);
                    let end = sv.position_of(span.end);
                    Location {
                        uri: uri.clone(),
                        range: Range { start, end },
                    }
                })
                .collect::<Vec<_>>()
        })
        .collect()
}

/// Fast path: look up pre-computed reference locations from the mir codebase index.
///
/// Handles `Function`, `Class`, and (partially) `Method` kinds.  For `Function` and
/// `Class` the mir analyzer records every call-site / instantiation via
/// `mark_*_referenced_at` and the index is authoritative.
///
/// For `Method`, the index is used as a pre-filter: only files that contain a tracked
/// call site for the method are scanned with the AST walker.  This fast path is
/// activated for two cases where the tracked set is reliably complete or narrows the
/// search scope without missing real references:
///   • `private` methods — PHP semantics guarantee that private methods are only
///     callable from within the class body, so mir always resolves the receiver type.
///   • methods on `final` classes — no subclassing means call sites on the concrete
///     type are unambiguous; the codebase set covers all statically-typed callers.
///
/// Returns `None` for public/protected methods on non-final classes and for `None`
/// kind (caller should use the general AST walker instead).  Also returns `None` when
/// no matching symbol is found in the codebase.
pub fn find_references_codebase(
    word: &str,
    all_docs: &[(Url, Arc<ParsedDoc>)],
    include_declaration: bool,
    kind: Option<SymbolKind>,
    codebase: &mir_analyzer::db::MirDbStorage,
    lookup_refs: &RefLookup<'_>,
) -> Option<Vec<Location>> {
    find_references_codebase_with_target(
        word,
        all_docs,
        include_declaration,
        kind,
        None,
        codebase,
        lookup_refs,
    )
}

/// Like [`find_references_codebase`] but accepts an exact FQN (for Function/Class)
/// or owning FQCN (for Method) to avoid short-name collisions across namespaces
/// and unrelated classes. When `target_fqn` is `None`, behaves identically to
/// `find_references_codebase`.
pub fn find_references_codebase_with_target(
    _word: &str,
    _all_docs: &[(Url, Arc<ParsedDoc>)],
    _include_declaration: bool,
    kind: Option<SymbolKind>,
    _target_fqn: Option<&str>,
    _codebase: &mir_analyzer::db::MirDbStorage,
    _lookup_refs: &RefLookup<'_>,
) -> Option<Vec<Location>> {
    match kind {
        Some(SymbolKind::Function) => {
            // For now, fall back to the AST walker for functions.
            // In the future, we could query the MirDb for function info.
            None
        }

        // The mir index records ClassReference only for `new Foo()` expressions, not
        // for type hints, `extends`, `implements`, or `instanceof`. Using the index
        // would silently drop those sites when any `new` call exists. Always fall
        // through to the AST walker (class_refs_in_stmts) which covers all sites.
        Some(SymbolKind::Class) => None,

        Some(SymbolKind::Method) => {
            // For now, fall back to the AST walker for methods.
            // In the future, we could use the MirDb to optimize this.
            None
        }

        // General walker already handles None kind; codebase index adds no value.
        None => None,

        // Properties and constants aren't tracked in the mir codebase index; fall
        // through to the AST walker.
        Some(SymbolKind::Property) | Some(SymbolKind::Constant) => None,
    }
}

fn find_references_inner(
    word: &str,
    all_docs: &[(Url, Arc<ParsedDoc>)],
    include_declaration: bool,
    include_use: bool,
    kind: Option<SymbolKind>,
    target_fqn: Option<&str>,
) -> Vec<Location> {
    // Each document is scanned independently: substring pre-filter, AST walk,
    // then span → position translation. Rayon parallelizes across docs; the
    // per-doc work is CPU-bound and 100% independent, so this scales linearly
    // with cores on large workspaces (Laravel: ~1,600 files).
    // Per-file namespace pre-filter only applies to Function and Class kinds,
    // where the target FQN refers to the symbol itself. For methods the
    // target is the *owning* FQCN, which can't be compared against the
    // method name via namespace resolution.
    let namespace_filter_active =
        matches!(kind, Some(SymbolKind::Function) | Some(SymbolKind::Class));
    all_docs
        .par_iter()
        .flat_map_iter(|(uri, doc)| {
            if namespace_filter_active
                && let Some(target) = target_fqn
                && !doc_can_reference_target(doc, word, target)
            {
                return Vec::new();
            }
            scan_doc(
                word,
                uri,
                doc,
                include_declaration,
                include_use,
                kind,
                target_fqn,
            )
        })
        .collect()
}

/// Return true when this doc's namespace + `use` imports could plausibly
/// refer to `target_fqn` under the short name `word`.  Used as a pre-filter
/// so the AST walker doesn't emit refs in files whose namespace would resolve
/// `word` to a different FQN.
fn doc_can_reference_target(doc: &ParsedDoc, word: &str, target_fqn: &str) -> bool {
    let target = target_fqn.trim_start_matches('\\');
    let imports = collect_file_imports(doc);
    let resolved = crate::moniker::resolve_fqn(doc, word, &imports);
    // PHP falls back to the global namespace for unqualified *function* calls
    // when the namespaced version doesn't exist.  We don't know at this point
    // which symbol category the target is, so accept either an exact match
    // or a global-namespace fallback match.
    resolved == target
        || (resolved == word && !target.contains('\\'))
        || (resolved == word && target == format!("\\{word}"))
}

struct ImportsVisitor {
    only_kind: Option<UseKind>,
    out: HashMap<String, String>,
}

impl<'arena, 'src> Visitor<'arena, 'src> for ImportsVisitor {
    fn visit_stmt(&mut self, stmt: &Stmt<'arena, 'src>) -> ControlFlow<()> {
        match &stmt.kind {
            StmtKind::Use(u) if self.only_kind.is_none_or(|k| u.kind == k) => {
                for item in u.uses.iter() {
                    let fqn = item.name.to_string_repr().into_owned();
                    let short = item
                        .alias
                        .map(|a| a.to_string())
                        .unwrap_or_else(|| fqn.rsplit('\\').next().unwrap_or(&fqn).to_string());
                    self.out.insert(short, fqn);
                }
                ControlFlow::Continue(())
            }
            // walk_stmt recurses into NamespaceBody::Braced automatically.
            StmtKind::Namespace(_) => walk_stmt(self, stmt),
            _ => ControlFlow::Continue(()),
        }
    }
}

/// Build a local-name → FQN map from a doc's `use` statements.  Mirrors
/// `Backend::file_imports` but self-contained so the reference walker can
/// run without a persistent codebase. Includes all use kinds (class, function,
/// const) — callers that only want class imports should use `collect_class_imports`.
pub(crate) fn collect_file_imports(doc: &ParsedDoc) -> HashMap<String, String> {
    collect_imports_filtered(doc, None)
}

/// Like `collect_file_imports` but restricted to `use ClassName` statements
/// (`UseKind::Normal`). Use this wherever the import map is fed into class
/// resolution — mixing in `use function` / `use const` entries causes the
/// resolver to map a function/const short name to the wrong FQN when the same
/// short name appears as a type hint or class reference.
///
/// TODO: upstream fix — have mir's FileAnalyzer auto-load via its ClassResolver
/// so lsp no longer needs to pre-collect class dependencies manually.
pub(crate) fn collect_class_imports(doc: &ParsedDoc) -> HashMap<String, String> {
    collect_imports_filtered(doc, Some(UseKind::Normal))
}

fn collect_imports_filtered(
    doc: &ParsedDoc,
    only_kind: Option<UseKind>,
) -> HashMap<String, String> {
    let mut v = ImportsVisitor {
        only_kind,
        out: HashMap::new(),
    };
    for stmt in doc.program().stmts.iter() {
        let _ = v.visit_stmt(stmt);
    }
    v.out
}

/// Collect every FQN class name (e.g. `\App\Model\Entity`) referenced in a
/// `new` expression that has no corresponding `use` import (i.e. written with
/// a leading `\`).  Returns de-duplicated strings with the leading `\` stripped,
/// ready for `session.lazy_load_class`.
pub(crate) fn collect_fqn_new_class_refs(doc: &ParsedDoc) -> Vec<String> {
    fqn_new_class_refs_in_stmts(&doc.program().stmts)
}

/// Collect every class-typed reference in `doc` (extends, implements, new,
/// instanceof, type hints, static calls, catch types), resolved to an FQN via
/// the current namespace and `use` imports. Used to lazy-load same-namespace
/// dependencies that have no explicit `use` statement (and so are missed by
/// `collect_file_imports`) before semantic analysis runs.
///
/// Returns de-duplicated FQNs with any leading `\` stripped.
pub(crate) fn collect_referenced_class_fqns(doc: &ParsedDoc) -> Vec<String> {
    let imports = collect_class_imports(doc);
    let names = all_class_ref_names_in_stmts(&doc.program().stmts);
    let locals = collect_local_type_decl_fqns(doc);
    let mut out: Vec<String> = names
        .into_iter()
        .map(|name| {
            // A leading `\` marks an already-fully-qualified reference like
            // `new \App\Model\Entity()` — strip the slash and use as-is.
            // `resolve_fqn` would otherwise prepend the current namespace.
            if let Some(stripped) = name.strip_prefix('\\') {
                return stripped.to_string();
            }
            let fqn = crate::moniker::resolve_fqn(doc, &name, &imports);
            fqn.trim_start_matches('\\').to_string()
        })
        // Skip references that resolve to a type declared in this very file —
        // mir already has them via `session.ingest_file`, and asking it to
        // lazy-load them can recurse back through analysis.
        .filter(|fqn| !locals.contains(fqn))
        .collect();
    out.sort_unstable();
    out.dedup();
    out
}

/// FQNs of every top-level type declared in `doc` (class, interface, trait,
/// enum), applying the file's `namespace` declaration. Used to suppress
/// self-references in the lazy-load list.
fn collect_local_type_decl_fqns(doc: &ParsedDoc) -> HashSet<String> {
    use php_ast::NamespaceBody;
    let mut out = HashSet::new();
    fn name_of(kind: &StmtKind<'_, '_>) -> Option<String> {
        match kind {
            StmtKind::Class(c) => c.name.as_ref().map(|n| n.to_string()),
            StmtKind::Interface(i) => Some(i.name.to_string()),
            StmtKind::Trait(t) => Some(t.name.to_string()),
            StmtKind::Enum(e) => Some(e.name.to_string()),
            _ => None,
        }
    }
    let mut current_ns: Option<String> = None;
    for stmt in doc.program().stmts.iter() {
        match &stmt.kind {
            StmtKind::Namespace(ns) => {
                let ns_name = ns.name.as_ref().map(|n| n.to_string_repr().to_string());
                match &ns.body {
                    NamespaceBody::Braced(inner) => {
                        let prefix = ns_name
                            .as_deref()
                            .map(|n| format!("{n}\\"))
                            .unwrap_or_default();
                        for s in inner.stmts.iter() {
                            if let Some(n) = name_of(&s.kind) {
                                out.insert(format!("{prefix}{n}"));
                            }
                        }
                    }
                    NamespaceBody::Simple => {
                        current_ns = ns_name;
                    }
                }
            }
            k => {
                if let Some(n) = name_of(k) {
                    let fqn = match &current_ns {
                        Some(ns) => format!("{ns}\\{n}"),
                        None => n,
                    };
                    out.insert(fqn);
                }
            }
        }
    }
    out
}

fn scan_doc(
    word: &str,
    uri: &Url,
    doc: &Arc<ParsedDoc>,
    include_declaration: bool,
    include_use: bool,
    kind: Option<SymbolKind>,
    target_fqn: Option<&str>,
) -> Vec<Location> {
    let source = doc.source();
    // Substring pre-filter: every walker below pushes a span only when an
    // identifier's bytes equal `word`, so if `word` does not appear in the
    // source it cannot produce any reference. `str::contains` is memchr-fast
    // and skips the full AST traversal for the vast majority of files.
    if !source.contains(word) {
        return Vec::new();
    }
    let stmts = &doc.program().stmts;
    let mut spans = Vec::new();

    if include_use {
        // Rename path: general walker covers call sites, `use` imports, and declarations.
        refs_in_stmts_with_use(source, stmts, word, &mut spans);
        if !include_declaration {
            let mut decl_spans = Vec::new();
            collect_declaration_spans(source, stmts, word, None, &mut decl_spans);
            let decl_set: HashSet<(u32, u32)> =
                decl_spans.iter().map(|s| (s.start, s.end)).collect();
            spans.retain(|span| !decl_set.contains(&(span.start, span.end)));
        }
    } else {
        match kind {
            Some(SymbolKind::Function) => function_refs_in_stmts(stmts, word, &mut spans),
            Some(SymbolKind::Method) => method_refs_in_stmts(stmts, word, &mut spans),
            Some(SymbolKind::Class) => class_refs_in_stmts(stmts, word, &mut spans),
            // Property walker emits both access sites *and* declaration spans
            // (used by rename). Strip decls here when the caller doesn't want them.
            Some(SymbolKind::Property) => {
                property_refs_in_stmts(source, stmts, word, &mut spans);
                if !include_declaration {
                    let mut decl_spans = Vec::new();
                    collect_declaration_spans(
                        source,
                        stmts,
                        word,
                        Some(SymbolKind::Property),
                        &mut decl_spans,
                    );
                    let decl_set: HashSet<(u32, u32)> =
                        decl_spans.iter().map(|s| (s.start, s.end)).collect();
                    spans.retain(|span| !decl_set.contains(&(span.start, span.end)));
                }
            }
            // Constant walker emits both declaration spans and access spans.
            Some(SymbolKind::Constant) => {
                // Class constants: target_fqn = owning class short name (no backslash).
                // Global/namespace constants: target_fqn = None (root) or
                //   "Namespace\\ConstName" (namespaced, has backslash). Route to the
                //   bare-identifier walker instead of the `::` class-const walker.
                let is_global = target_fqn.is_none_or(|fqn| fqn.contains('\\'));
                if is_global {
                    global_constant_refs_in_stmts(source, stmts, word, target_fqn, &mut spans);
                } else {
                    // target_fqn = class short name for class constants.
                    constant_refs_in_stmts(source, stmts, word, target_fqn, &mut spans);
                }
                if !include_declaration {
                    let mut decl_spans = Vec::new();
                    collect_declaration_spans(
                        source,
                        stmts,
                        word,
                        Some(SymbolKind::Constant),
                        &mut decl_spans,
                    );
                    let decl_set: HashSet<(u32, u32)> =
                        decl_spans.iter().map(|s| (s.start, s.end)).collect();
                    spans.retain(|span| !decl_set.contains(&(span.start, span.end)));
                }
            }
            // General walker already includes declarations; filter them out if unwanted.
            None => {
                refs_in_stmts(source, stmts, word, &mut spans);
                if !include_declaration {
                    let mut decl_spans = Vec::new();
                    collect_declaration_spans(source, stmts, word, None, &mut decl_spans);
                    let decl_set: HashSet<(u32, u32)> =
                        decl_spans.iter().map(|s| (s.start, s.end)).collect();
                    spans.retain(|span| !decl_set.contains(&(span.start, span.end)));
                }
            }
        }
        // Typed walkers (except Property, which already includes decls) don't emit
        // declaration spans, so add them separately when wanted. Pass `kind` so only
        // declarations of the matching category are appended — a Method search must
        // not return a free-function declaration with the same name.
        if include_declaration
            && matches!(
                kind,
                Some(SymbolKind::Function) | Some(SymbolKind::Method) | Some(SymbolKind::Class)
            )
        {
            collect_declaration_spans(source, stmts, word, kind, &mut spans);
        }
    }

    let sv = doc.view();
    let word_utf16_len: u32 = utf16_code_units(word);
    spans
        .into_iter()
        .map(|span| {
            let start = sv.position_of(span.start);
            let end = Position {
                line: start.line,
                character: start.character + word_utf16_len,
            };
            Location {
                uri: uri.clone(),
                range: Range { start, end },
            }
        })
        .collect()
}

/// Build a span covering exactly the declared name (not the keyword before it).
/// Uses the stmt_span to search within the statement's context, avoiding false
/// matches from earlier occurrences of the same name in the file.
fn declaration_name_span(source: &str, name: &str, stmt_span: Span) -> Span {
    let start = str_offset_in_range(source, stmt_span, name).unwrap_or(stmt_span.start);
    Span {
        start,
        end: start + name.len() as u32,
    }
}

/// Collect every span where `word` is *declared* within `stmts`.
///
/// When `kind` is `Some`, only declarations of the matching category are collected:
/// - `Function` → free (`StmtKind::Function`) declarations only
/// - `Method`   → method declarations inside classes / traits / enums only
/// - `Class`    → class / interface / trait / enum type declarations only
///
/// `None` collects every declaration kind (used by `is_declaration_span`).
fn collect_declaration_spans(
    source: &str,
    stmts: &[Stmt<'_, '_>],
    word: &str,
    kind: Option<SymbolKind>,
    out: &mut Vec<Span>,
) {
    let want_free = matches!(kind, None | Some(SymbolKind::Function));
    let want_method = matches!(kind, None | Some(SymbolKind::Method));
    let want_type = matches!(kind, None | Some(SymbolKind::Class));
    let want_property = matches!(kind, None | Some(SymbolKind::Property));
    let want_constant = matches!(kind, None | Some(SymbolKind::Constant));

    for stmt in stmts {
        match &stmt.kind {
            StmtKind::Function(f) if want_free && f.name == word => {
                out.push(declaration_name_span(
                    source,
                    &f.name.to_string(),
                    stmt.span,
                ));
            }
            StmtKind::Class(c) => {
                if want_type
                    && let Some(name) = c.name
                    && name == word
                {
                    out.push(declaration_name_span(source, &name.to_string(), stmt.span));
                }
                if want_method || want_property || want_constant {
                    for member in c.body.members.iter() {
                        match &member.kind {
                            ClassMemberKind::Method(m) if want_method && m.name == word => {
                                // Scope the name search to the member span,
                                // not the whole class — otherwise a class
                                // named the same as one of its members
                                // (`class get { function get() {} }`) resolves
                                // both decls to the class name's position.
                                out.push(declaration_name_span(
                                    source,
                                    &m.name.to_string(),
                                    member.span,
                                ));
                            }
                            ClassMemberKind::Method(m)
                                if want_property && m.name == "__construct" =>
                            {
                                // Promoted constructor params act as property declarations.
                                for p in m.params.iter() {
                                    if p.visibility.is_some() && p.name == word {
                                        out.push(declaration_name_span(
                                            source,
                                            &p.name.to_string(),
                                            p.span,
                                        ));
                                    }
                                }
                            }
                            ClassMemberKind::Property(p) if want_property && p.name == word => {
                                out.push(declaration_name_span(
                                    source,
                                    &p.name.to_string(),
                                    member.span,
                                ));
                            }
                            ClassMemberKind::ClassConst(c) if want_constant && c.name == word => {
                                out.push(declaration_name_span(
                                    source,
                                    &c.name.to_string(),
                                    member.span,
                                ));
                            }
                            _ => {}
                        }
                    }
                }
            }
            StmtKind::Interface(i) => {
                if want_type && i.name == word {
                    out.push(declaration_name_span(
                        source,
                        &i.name.to_string(),
                        stmt.span,
                    ));
                }
                if want_method || want_constant {
                    for member in i.body.members.iter() {
                        match &member.kind {
                            ClassMemberKind::Method(m) if want_method && m.name == word => {
                                out.push(declaration_name_span(
                                    source,
                                    &m.name.to_string(),
                                    member.span,
                                ));
                            }
                            ClassMemberKind::ClassConst(c) if want_constant && c.name == word => {
                                out.push(declaration_name_span(
                                    source,
                                    &c.name.to_string(),
                                    member.span,
                                ));
                            }
                            _ => {}
                        }
                    }
                }
            }
            StmtKind::Trait(t) => {
                if want_type && t.name == word {
                    out.push(declaration_name_span(
                        source,
                        &t.name.to_string(),
                        stmt.span,
                    ));
                }
                if want_method || want_property || want_constant {
                    for member in t.body.members.iter() {
                        match &member.kind {
                            ClassMemberKind::Method(m) if want_method && m.name == word => {
                                out.push(declaration_name_span(
                                    source,
                                    &m.name.to_string(),
                                    member.span,
                                ));
                            }
                            ClassMemberKind::Property(p) if want_property && p.name == word => {
                                out.push(declaration_name_span(
                                    source,
                                    &p.name.to_string(),
                                    member.span,
                                ));
                            }
                            ClassMemberKind::ClassConst(c) if want_constant && c.name == word => {
                                out.push(declaration_name_span(
                                    source,
                                    &c.name.to_string(),
                                    member.span,
                                ));
                            }
                            _ => {}
                        }
                    }
                }
            }
            StmtKind::Enum(e) => {
                if want_type && e.name == word {
                    out.push(declaration_name_span(
                        source,
                        &e.name.to_string(),
                        stmt.span,
                    ));
                }
                for member in e.body.members.iter() {
                    match &member.kind {
                        EnumMemberKind::Method(m) if want_method && m.name == word => {
                            out.push(declaration_name_span(
                                source,
                                &m.name.to_string(),
                                member.span,
                            ));
                        }
                        EnumMemberKind::Case(c) if want_type && c.name == word => {
                            out.push(declaration_name_span(
                                source,
                                &c.name.to_string(),
                                member.span,
                            ));
                        }
                        EnumMemberKind::ClassConst(c) if want_constant && c.name == word => {
                            out.push(declaration_name_span(
                                source,
                                &c.name.to_string(),
                                member.span,
                            ));
                        }
                        _ => {}
                    }
                }
            }
            StmtKind::Const(items) if want_constant => {
                for item in items.iter() {
                    if item.name == word {
                        let name = item.name.to_string();
                        out.push(declaration_name_span(source, &name, item.span));
                    }
                }
            }
            StmtKind::Expression(expr) if want_constant => {
                // `define('NAME', value)` acts as a global constant declaration.
                if let ExprKind::FunctionCall(f) = &expr.kind
                    && let ExprKind::Identifier(id) = &f.name.kind
                    && id.as_str() == "define"
                    && let Some(first_arg) = f.args.first()
                    && let ExprKind::String(s) = &first_arg.value.kind
                    && *s == word
                {
                    let start = first_arg.value.span.start + 1;
                    out.push(Span {
                        start,
                        end: start + s.len() as u32,
                    });
                }
            }
            StmtKind::Namespace(ns) => {
                if let NamespaceBody::Braced(inner) = &ns.body {
                    collect_declaration_spans(source, &inner.stmts, word, kind, out);
                }
            }
            _ => {}
        }
    }
}