phpantom_lsp 0.7.0

Fast PHP language server with deep type intelligence. Generics, Laravel, PHPStan annotations. Ready in an instant.
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
//! Tests for namespace-aware function completion.
//!
//! Covers:
//! - Namespaced functions show FQN in `use function` context
//! - Namespaced functions get `use function FQN;` auto-import in inline context
//! - Global functions do NOT get auto-import
//! - Same-namespace functions do NOT get auto-import
//! - Detail shows namespace for namespaced functions
//! - Deduplication uses FQN (no short-name collisions)
//! - Short-name fallback entries are no longer inserted
//! - `filter_current_file_functions` works with FQN-based filtering

use crate::common::{
    create_psr4_workspace, create_test_backend, create_test_backend_with_function_stubs,
};
use phpantom_lsp::php_type::PhpType;
use phpantom_lsp::types::FunctionInfo;
use tower_lsp::LanguageServer;
use tower_lsp::lsp_types::*;

/// Helper: open a file and request completion at the given line/character.
async fn complete_at(
    backend: &phpantom_lsp::Backend,
    uri: &Url,
    text: &str,
    line: u32,
    character: u32,
) -> Vec<CompletionItem> {
    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri: uri.clone() },
            position: Position { line, character },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    match backend.completion(completion_params).await.unwrap() {
        Some(CompletionResponse::Array(items)) => items,
        Some(CompletionResponse::List(list)) => list.items,
        _ => vec![],
    }
}

fn labels(items: &[CompletionItem]) -> Vec<&str> {
    items.iter().map(|i| i.label.as_str()).collect()
}

/// Register a namespaced helper function in the global_functions map.
fn register_namespaced_function(
    backend: &phpantom_lsp::Backend,
    fqn: &str,
    name: &str,
    namespace: &str,
    uri: &str,
) {
    {
        let mut fmap = backend.global_functions().write();
        fmap.insert(
            fqn.to_string(),
            (
                uri.to_string(),
                FunctionInfo {
                    name: name.to_string(),
                    name_offset: 0,
                    parameters: vec![],
                    return_type: Some(PhpType::parse("mixed")),
                    native_return_type: None,
                    description: None,
                    return_description: None,
                    links: vec![],
                    see_refs: vec![],
                    namespace: Some(namespace.to_string()),
                    conditional_return: None,
                    type_assertions: vec![],
                    deprecation_message: None,
                    deprecated_replacement: None,
                    template_params: vec![],
                    template_bindings: vec![],
                    throws: vec![],
                    is_polyfill: false,
                },
            ),
        );
    }
}

/// Register a global (non-namespaced) function in the global_functions map.
fn register_global_function(backend: &phpantom_lsp::Backend, name: &str, uri: &str) {
    {
        let mut fmap = backend.global_functions().write();
        fmap.insert(
            name.to_string(),
            (
                uri.to_string(),
                FunctionInfo {
                    name: name.to_string(),
                    name_offset: 0,
                    parameters: vec![],
                    return_type: Some(PhpType::parse("string")),
                    native_return_type: None,
                    description: None,
                    return_description: None,
                    links: vec![],
                    see_refs: vec![],
                    namespace: None,
                    conditional_return: None,
                    type_assertions: vec![],
                    deprecation_message: None,
                    deprecated_replacement: None,
                    template_params: vec![],
                    template_bindings: vec![],
                    throws: vec![],
                    is_polyfill: false,
                },
            ),
        );
    }
}

// ─── `use function` context ─────────────────────────────────────────────────

/// In `use function` context, a namespaced function's insert_text should be
/// the FQN so the resulting statement reads `use function Ns\func;`.
#[tokio::test]
async fn test_use_function_namespaced_insert_text_is_fqn() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nuse function enum_val\n";

    let items = complete_at(&backend, &uri, text, 1, 21).await;

    let item = items.iter().find(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION)
            && i.insert_text.as_deref() == Some("Illuminate\\Support\\enum_value;")
    });
    assert!(
        item.is_some(),
        "use function should insert the FQN. Got insert_texts: {:?}",
        items
            .iter()
            .filter(|i| i.kind == Some(CompletionItemKind::FUNCTION))
            .map(|i| i.insert_text.as_deref())
            .collect::<Vec<_>>()
    );
}

/// In `use function` context, the label for a namespaced function should be
/// the FQN (not the short name).
#[tokio::test]
async fn test_use_function_namespaced_label_is_fqn() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nuse function enum_val\n";

    let items = complete_at(&backend, &uri, text, 1, 21).await;

    let item = items.iter().find(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION)
            && i.label.contains("Illuminate\\Support\\enum_value")
    });
    assert!(
        item.is_some(),
        "use function label should contain the FQN. Labels: {:?}",
        labels(&items)
    );
}

/// In `use function` context, a global function should still use the short
/// name as the insert text.
#[tokio::test]
async fn test_use_function_global_insert_text_is_short_name() {
    let backend = create_test_backend();

    register_global_function(&backend, "my_global_func", "file:///helpers.php");

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nuse function my_global\n";

    let items = complete_at(&backend, &uri, text, 1, 22).await;

    let item = items.iter().find(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION)
            && i.insert_text
                .as_deref()
                .is_some_and(|t| t.contains("my_global_func"))
    });
    assert!(
        item.is_some(),
        "use function for global func should insert the short name. Got: {:?}",
        items
            .iter()
            .filter(|i| i.kind == Some(CompletionItemKind::FUNCTION))
            .map(|i| (&i.label, &i.insert_text))
            .collect::<Vec<_>>()
    );
}

/// In `use function` context, the filter_text should be the FQN so the user
/// can type either the namespace or the short name to find the function.
#[tokio::test]
async fn test_use_function_filter_text_is_fqn() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nuse function Illuminate\\Support\\enum\n";

    let items = complete_at(&backend, &uri, text, 1, 40).await;

    let item = items.iter().find(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION)
            && i.filter_text
                .as_deref()
                .is_some_and(|ft| ft.contains("Illuminate\\Support\\enum_value"))
    });
    assert!(
        item.is_some(),
        "filter_text should contain the FQN. Got: {:?}",
        items
            .iter()
            .filter(|i| i.kind == Some(CompletionItemKind::FUNCTION))
            .map(|i| (&i.label, &i.filter_text))
            .collect::<Vec<_>>()
    );
}

// ─── Inline (non-use) context ───────────────────────────────────────────────

/// In inline context, a namespaced function should get a `use function FQN;`
/// auto-import via additional_text_edits.
#[tokio::test]
async fn test_inline_namespaced_function_gets_auto_import() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nenum_val\n";

    let items = complete_at(&backend, &uri, text, 1, 8).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("enum_value"));
    assert!(
        item.is_some(),
        "Should find enum_value in inline completions. Labels: {:?}",
        labels(&items)
    );

    let item = item.unwrap();
    let edits = item
        .additional_text_edits
        .as_ref()
        .expect("Namespaced function should have additional_text_edits for auto-import");
    assert!(
        !edits.is_empty(),
        "Should have at least one auto-import text edit"
    );
    let edit_text = &edits[0].new_text;
    assert!(
        edit_text.contains("use function Illuminate\\Support\\enum_value;"),
        "Auto-import should insert `use function FQN;`, got: {}",
        edit_text
    );
}

/// In inline context, the insert text should be the short name (with snippet),
/// not the FQN.
#[tokio::test]
async fn test_inline_namespaced_function_insert_text_is_short_name() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nenum_val\n";

    let items = complete_at(&backend, &uri, text, 1, 8).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("enum_value"));
    assert!(item.is_some(), "Should find enum_value");

    let insert = item.unwrap().insert_text.as_deref().unwrap();
    assert!(
        insert.starts_with("enum_value("),
        "Insert text should start with the short name, not the FQN. Got: {}",
        insert
    );
    assert!(
        !insert.contains("Illuminate"),
        "Insert text should NOT contain the namespace. Got: {}",
        insert
    );
}

/// In inline context, a global function should NOT get auto-import.
#[tokio::test]
async fn test_inline_global_function_no_auto_import() {
    let backend = create_test_backend();

    register_global_function(&backend, "my_global_func", "file:///helpers.php");

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nmy_global\n";

    let items = complete_at(&backend, &uri, text, 1, 9).await;

    let item = items.iter().find(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("my_global_func")
    });
    assert!(item.is_some(), "Should find my_global_func");

    assert!(
        item.unwrap().additional_text_edits.is_none(),
        "Global function should NOT have auto-import edits"
    );
}

/// In inline context, a function in the same namespace as the current file
/// should NOT get auto-import.
#[tokio::test]
async fn test_inline_same_namespace_function_no_auto_import() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "App\\Helpers\\my_helper",
        "my_helper",
        "App\\Helpers",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    // File declares the same namespace as the function.
    let text = "<?php\nnamespace App\\Helpers;\n\nmy_help\n";

    let items = complete_at(&backend, &uri, text, 3, 7).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("my_helper"));
    assert!(
        item.is_some(),
        "Should find my_helper. Labels: {:?}",
        labels(&items)
    );

    assert!(
        item.unwrap().additional_text_edits.is_none(),
        "Same-namespace function should NOT have auto-import edits"
    );
}

// ─── Detail field ───────────────────────────────────────────────────────────

/// In inline context, a namespaced function's detail should show the namespace.
#[tokio::test]
async fn test_inline_namespaced_function_detail_shows_namespace() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nenum_val\n";

    let items = complete_at(&backend, &uri, text, 1, 8).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("enum_value"));
    assert!(item.is_some(), "Should find enum_value");

    let item = item.unwrap();

    // detail now shows return type
    assert_eq!(
        item.detail.as_deref(),
        Some("mixed"),
        "Detail should show return type. Got: {:?}",
        item.detail
    );
}

/// In inline context, a global function's detail should just be "function".
#[tokio::test]
async fn test_inline_global_function_detail_is_plain() {
    let backend = create_test_backend();

    register_global_function(&backend, "my_global_func", "file:///helpers.php");

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nmy_global\n";

    let items = complete_at(&backend, &uri, text, 1, 9).await;

    let item = items.iter().find(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("my_global_func")
    });
    assert!(item.is_some(), "Should find my_global_func");

    let item = item.unwrap();

    // detail now shows return type
    assert_eq!(
        item.detail.as_deref(),
        Some("string"),
        "Global function detail should show return type. Got: {:?}",
        item.detail
    );
}

// ─── Deduplication (FQN-based) ──────────────────────────────────────────────

/// Two functions in different namespaces with the same short name should
/// both appear in completions (no short-name collision).
#[tokio::test]
async fn test_different_namespaces_same_short_name_both_appear() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///illuminate.php",
    );
    register_namespaced_function(
        &backend,
        "Symfony\\Component\\enum_value",
        "enum_value",
        "Symfony\\Component",
        "file:///symfony.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nenum_val\n";

    let items = complete_at(&backend, &uri, text, 1, 8).await;

    let matching: Vec<_> = items
        .iter()
        .filter(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("enum_value"))
        .collect();

    assert!(
        matching.len() >= 2,
        "Both namespaced functions should appear. Got {} matches: {:?}",
        matching.len(),
        matching
            .iter()
            .map(|i| (&i.label, &i.detail))
            .collect::<Vec<_>>()
    );
}

/// In `use function` context, two functions with the same short name in
/// different namespaces should both appear with FQN labels.
#[tokio::test]
async fn test_use_function_different_namespaces_both_appear_with_fqn() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///illuminate.php",
    );
    register_namespaced_function(
        &backend,
        "Symfony\\Component\\enum_value",
        "enum_value",
        "Symfony\\Component",
        "file:///symfony.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nuse function enum_val\n";

    let items = complete_at(&backend, &uri, text, 1, 21).await;

    let matching: Vec<_> = items
        .iter()
        .filter(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("enum_value"))
        .collect();

    assert!(
        matching.len() >= 2,
        "Both should appear in use function context. Got: {:?}",
        labels(&items)
    );

    let has_illuminate = matching
        .iter()
        .any(|i| i.label.contains("Illuminate\\Support"));
    let has_symfony = matching
        .iter()
        .any(|i| i.label.contains("Symfony\\Component"));
    assert!(
        has_illuminate && has_symfony,
        "Both FQNs should appear as labels. Labels: {:?}",
        matching.iter().map(|i| &i.label).collect::<Vec<_>>()
    );
}

// ─── Short-name fallback removal ────────────────────────────────────────────

/// After a namespaced function is registered via file parsing, the
/// global_functions map should contain ONLY the FQN key, not a separate
/// short-name key.
#[tokio::test]
async fn test_no_short_name_fallback_in_global_functions() {
    let backend = create_test_backend();

    // Open a file that defines a namespaced function.
    let uri = Url::parse("file:///helpers.php").unwrap();
    let text = concat!(
        "<?php\n",
        "namespace Illuminate\\Support;\n",
        "\n",
        "function enum_value(mixed $value): mixed {\n",
        "    return $value;\n",
        "}\n",
    );
    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    let fmap = backend.global_functions().read();

    assert!(
        fmap.contains_key("Illuminate\\Support\\enum_value"),
        "Should have FQN key. Keys: {:?}",
        fmap.keys().collect::<Vec<_>>()
    );
    assert!(
        !fmap.contains_key("enum_value"),
        "Should NOT have short-name fallback key. Keys: {:?}",
        fmap.keys().collect::<Vec<_>>()
    );
}

// ─── Current-file filtering ─────────────────────────────────────────────────

/// In `use function` context, functions from the current file should be
/// filtered out even when they are namespaced (filter uses FQN matching).
#[tokio::test]
async fn test_use_function_filters_current_file_namespaced() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///current.php").unwrap();
    let text = concat!(
        "<?php\n",
        "namespace App\\Helpers;\n",
        "\n",
        "function my_current_helper(): void {}\n",
        "\n",
        "use function my_current\n",
    );

    let items = complete_at(&backend, &uri, text, 5, 24).await;

    let has_current = items.iter().any(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("my_current_helper")
    });
    assert!(
        !has_current,
        "Functions from the current file should be filtered out in use function context. Labels: {:?}",
        labels(&items)
    );
}

// ─── Cross-file PSR-4 namespaced functions ──────────────────────────────────

/// A namespaced function discovered from a PSR-4 autoload file should appear
/// with correct FQN and auto-import in inline completions.
#[tokio::test]
async fn test_psr4_namespaced_function_completion() {
    let (backend, _dir) = create_psr4_workspace(
        r#"{ "autoload": { "psr-4": { "App\\": "src/" } } }"#,
        &[(
            "src/Helpers/helpers.php",
            concat!(
                "<?php\n",
                "namespace App\\Helpers;\n",
                "\n",
                "function format_price(float $amount): string {\n",
                "    return '$' . number_format($amount, 2);\n",
                "}\n",
            ),
        )],
    );

    // Open the helper file so it gets parsed.
    let helpers_path = _dir.path().join("src/Helpers/helpers.php");
    let helpers_uri = Url::from_file_path(&helpers_path).unwrap();
    let helpers_text = std::fs::read_to_string(&helpers_path).unwrap();
    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: helpers_uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: helpers_text,
            },
        })
        .await;

    // Complete in a different file.
    let test_uri = Url::parse("file:///test.php").unwrap();
    let test_text = "<?php\nformat_pri\n";

    let items = complete_at(&backend, &test_uri, test_text, 1, 10).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("format_price"));
    assert!(
        item.is_some(),
        "Should find format_price from PSR-4 helper file. Labels: {:?}",
        labels(&items)
    );

    let item = item.unwrap();

    // detail now shows return type.
    assert_eq!(
        item.detail.as_deref(),
        Some("string"),
        "Detail should show return type. Got: {:?}",
        item.detail
    );

    // Auto-import should be present.
    let edits = item
        .additional_text_edits
        .as_ref()
        .expect("Cross-file namespaced function should have auto-import");
    assert!(
        edits[0]
            .new_text
            .contains("use function App\\Helpers\\format_price;"),
        "Auto-import should use FQN. Got: {}",
        edits[0].new_text
    );
}

// ─── Matching by namespace prefix ───────────────────────────────────────────

/// Typing the namespace prefix (e.g. `Illuminate\`) should match
/// namespaced functions.
#[tokio::test]
async fn test_use_function_matches_by_namespace_prefix() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nuse function Illuminate\\\n";

    let items = complete_at(&backend, &uri, text, 1, 28).await;

    let item = items.iter().find(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION)
            && i.insert_text
                .as_deref()
                .is_some_and(|t| t.contains("Illuminate\\Support\\enum_value"))
    });
    assert!(
        item.is_some(),
        "Typing namespace prefix should match namespaced functions. Got: {:?}",
        items
            .iter()
            .filter(|i| i.kind == Some(CompletionItemKind::FUNCTION))
            .map(|i| (&i.label, &i.insert_text))
            .collect::<Vec<_>>()
    );
}

// ─── Stub functions with namespaces ─────────────────────────────────────────

/// Stub functions that are namespaced (e.g. those with backslashes in the
/// stub_function_index key) should show the short name in inline context
/// and the FQN in use function context.
#[tokio::test]
async fn test_stub_global_function_no_auto_import() {
    let backend = create_test_backend_with_function_stubs();

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\narray_ma\n";

    let items = complete_at(&backend, &uri, text, 1, 8).await;

    let item = items.iter().find(|i| {
        i.kind == Some(CompletionItemKind::FUNCTION)
            && i.filter_text.as_deref() == Some("array_map")
    });
    assert!(item.is_some(), "Should find array_map from stubs");

    assert!(
        item.unwrap().additional_text_edits.is_none(),
        "Global stub function should NOT have auto-import edits"
    );
}

// ─── Auto-import placement ──────────────────────────────────────────────────

/// The `use function` auto-import should be inserted at the correct
/// alphabetical position among existing use statements.
#[tokio::test]
async fn test_auto_import_alphabetical_placement() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = concat!(
        "<?php\n",
        "use App\\Models\\User;\n",
        "use Symfony\\Component\\HttpKernel;\n",
        "\n",
        "enum_val\n",
    );

    let items = complete_at(&backend, &uri, text, 4, 8).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("enum_value"));
    assert!(item.is_some(), "Should find enum_value");

    let edits = item
        .unwrap()
        .additional_text_edits
        .as_ref()
        .expect("Should have auto-import");

    // Function imports form their own group AFTER all class imports,
    // separated by a blank line.
    let edit = &edits[0];
    assert_eq!(
        edit.new_text, "\nuse function Illuminate\\Support\\enum_value;\n",
        "Should have blank-line separator before the first function import"
    );
    // Line 2 is `use Symfony\...` (the last class import).
    // The function import goes after it → line 3.
    assert_eq!(
        edit.range.start.line, 3,
        "Should insert after all class imports (line 3). Got line: {}",
        edit.range.start.line
    );
}

// ─── `use function` in `use function` context for namespaced function detail ────

/// In `use function` context, a namespaced user function's detail should
/// show the full signature.
#[tokio::test]
async fn test_use_function_namespaced_detail_shows_signature() {
    let backend = create_test_backend();

    {
        let mut fmap = backend.global_functions().write();
        fmap.insert(
            "Illuminate\\Support\\enum_value".to_string(),
            (
                "file:///helpers.php".to_string(),
                FunctionInfo {
                    name: "enum_value".to_string(),
                    name_offset: 0,
                    parameters: vec![phpantom_lsp::types::ParameterInfo {
                        name: "$value".to_string(),
                        is_required: true,
                        type_hint: Some(PhpType::parse("mixed")),
                        native_type_hint: Some(PhpType::parse("mixed")),
                        description: None,
                        default_value: None,
                        is_variadic: false,
                        is_reference: false,
                        closure_this_type: None,
                    }],
                    return_type: Some(PhpType::parse("mixed")),
                    native_return_type: None,
                    description: None,
                    return_description: None,
                    links: vec![],
                    see_refs: vec![],
                    namespace: Some("Illuminate\\Support".to_string()),
                    conditional_return: None,
                    type_assertions: vec![],
                    deprecation_message: None,
                    deprecated_replacement: None,
                    template_params: vec![],
                    template_bindings: vec![],
                    throws: vec![],
                    is_polyfill: false,
                },
            ),
        );
    }

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nuse function enum_val\n";

    let items = complete_at(&backend, &uri, text, 1, 21).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("Illuminate"));
    assert!(item.is_some(), "Should find the function");

    let item = item.unwrap();

    // In use-import context, detail is None (build_use_import_item does not set it).
    assert_eq!(
        item.detail, None,
        "Use-import items should have no detail (no return type shown). Got: {:?}",
        item.detail
    );
}

// ─── User function shadows stub ─────────────────────────────────────────────

/// A user-defined function with the same FQN as a stub should shadow the
/// stub (user version wins).
#[tokio::test]
async fn test_user_function_shadows_stub_same_fqn() {
    let backend = create_test_backend_with_function_stubs();

    // Register a user-defined `array_map` that shadows the stub.
    register_global_function(&backend, "array_map", "file:///custom.php");

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\narray_ma\n";

    let items = complete_at(&backend, &uri, text, 1, 8).await;

    let matching: Vec<_> = items
        .iter()
        .filter(|i| {
            i.kind == Some(CompletionItemKind::FUNCTION)
                && i.filter_text.as_deref() == Some("array_map")
        })
        .collect();

    assert_eq!(
        matching.len(),
        1,
        "Should have exactly one array_map (user version shadows stub). Got: {:?}",
        matching
            .iter()
            .map(|i| (&i.label, &i.detail))
            .collect::<Vec<_>>()
    );
    // detail now shows return type.
    assert_eq!(
        matching[0].detail.as_deref(),
        Some("string"),
        "Should show return type in detail. Got: {:?}",
        matching[0].detail
    );
}

// ─── Deprecated namespaced function ─────────────────────────────────────────

/// A deprecated namespaced function should have the deprecated flag set.
#[tokio::test]
async fn test_deprecated_namespaced_function() {
    let backend = create_test_backend();

    {
        let mut fmap = backend.global_functions().write();
        fmap.insert(
            "Legacy\\old_helper".to_string(),
            (
                "file:///legacy.php".to_string(),
                FunctionInfo {
                    name: "old_helper".to_string(),
                    name_offset: 0,
                    parameters: vec![],
                    return_type: None,
                    native_return_type: None,
                    description: None,
                    return_description: None,
                    links: vec![],
                    see_refs: vec![],
                    namespace: Some("Legacy".to_string()),
                    conditional_return: None,
                    type_assertions: vec![],
                    deprecation_message: Some("Use newFunc() instead".into()),
                    deprecated_replacement: None,
                    template_params: vec![],
                    template_bindings: vec![],
                    throws: vec![],
                    is_polyfill: false,
                },
            ),
        );
    }

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nold_hel\n";

    let items = complete_at(&backend, &uri, text, 1, 7).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("old_helper"));
    assert!(item.is_some(), "Should find old_helper");
    let tags = item.unwrap().tags.as_deref().unwrap_or(&[]);
    assert!(
        tags.contains(&CompletionItemTag::DEPRECATED),
        "Tags should contain DEPRECATED"
    );
}

// ─── Inline context with namespace ──────────────────────────────────────────

/// When the file has a namespace and the function is in a DIFFERENT
/// namespace, auto-import should still be generated.
#[tokio::test]
async fn test_inline_different_namespace_gets_auto_import() {
    let backend = create_test_backend();

    register_namespaced_function(
        &backend,
        "Illuminate\\Support\\enum_value",
        "enum_value",
        "Illuminate\\Support",
        "file:///helpers.php",
    );

    let uri = Url::parse("file:///test.php").unwrap();
    let text = "<?php\nnamespace App\\Services;\n\nenum_val\n";

    let items = complete_at(&backend, &uri, text, 3, 8).await;

    let item = items
        .iter()
        .find(|i| i.kind == Some(CompletionItemKind::FUNCTION) && i.label.contains("enum_value"));
    assert!(item.is_some(), "Should find enum_value");

    let edits = item
        .unwrap()
        .additional_text_edits
        .as_ref()
        .expect("Different-namespace function should get auto-import");
    assert!(
        edits[0]
            .new_text
            .contains("use function Illuminate\\Support\\enum_value;"),
        "Auto-import text should be correct. Got: {}",
        edits[0].new_text
    );
}