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
use crate::common::create_test_backend;
use tower_lsp::LanguageServer;
use tower_lsp::lsp_types::*;

/// Regression test: completion on `collect($var)->` resolves Collection members.
///
/// The `collect()` helper returns `Collection` and the cursor is right
/// after `->` — the subject is `collect($paymentOptions)` which is a
/// standalone function call whose return type is a class.
#[tokio::test]
async fn test_completion_on_function_call_arrow() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///collect_map.php").unwrap();
    let text = r#"<?php

class Collection {
    /** @return static */
    public function map(callable $callback): static {}

    /** @return static */
    public function values(): static {}

    /** @return mixed */
    public function first(): mixed {}
}

/**
 * @return Collection
 */
function collect($value = []): Collection
{
    return new Collection($value);
}

class PaymentOptionLocale {
    public bool $tokens_enabled;
}

class PaymentService {
    public function getOptions(array $paymentOptions): void {
        $formattedOptions = collect($paymentOptions)->map(function (PaymentOptionLocale $optionLocale) {
            return [
                'tokens_enabled' => $optionLocale->tokens_enabled,
            ];
        })->values();
        $formattedOptions->
    }
}
"#;

    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;

    // ── Test 1: completion after `$formattedOptions->` ──
    let target_line = text
        .lines()
        .enumerate()
        .find(|(_, l)| l.trim() == "$formattedOptions->")
        .map(|(i, _)| i)
        .expect("should find $formattedOptions-> line");

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

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

    let labels: Vec<&str> = items.iter().map(|i| i.label.as_str()).collect();
    assert!(
        labels.iter().any(|l| l.starts_with("map")),
        "Expected 'map' in completions for Collection, got: {:?}",
        labels
    );
    assert!(
        labels.iter().any(|l| l.starts_with("values")),
        "Expected 'values' in completions for Collection, got: {:?}",
        labels
    );

    // ── Test 2: completion after `collect($paymentOptions)->` ──
    // The cursor is right after `->` before `map` on the chained call line.
    let chain_line = text
        .lines()
        .enumerate()
        .find(|(_, l)| l.contains("collect($paymentOptions)->map("))
        .map(|(i, _)| i)
        .expect("should find collect()->map( line");

    let chain_line_text = text.lines().nth(chain_line).unwrap();
    let arrow_col = chain_line_text.find("->map(").unwrap() as u32 + 2; // after `->`

    let completion_params2 = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: chain_line as u32,
                character: arrow_col + 3, // after `->map`
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    // Must not crash and should offer Collection members
    let result2 = backend.completion(completion_params2).await.unwrap();
    let items2 = match result2 {
        Some(CompletionResponse::Array(items)) => items,
        Some(CompletionResponse::List(list)) => list.items,
        None => vec![],
    };
    let labels2: Vec<&str> = items2.iter().map(|i| i.label.as_str()).collect();
    assert!(
        labels2.iter().any(|l| l.starts_with("map")),
        "Expected 'map' in completions on chained collect()->, got: {:?}",
        labels2
    );
}

// ─── Method Insert Text with Parameters ─────────────────────────────────────

#[tokio::test]
async fn test_completion_method_insert_text_no_params() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///insert.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Widget {\n",
        "    function render() {}\n",
        "    function update() {}\n",
        "    function test() {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    )
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$this->` on line 5
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 5,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let method_items: Vec<&CompletionItem> = items
                .iter()
                .filter(|i| i.kind == Some(CompletionItemKind::METHOD))
                .collect();
            for item in &method_items {
                let insert = item.insert_text.as_deref().unwrap();
                let filter = item.filter_text.as_deref().unwrap();
                // insert_text should be a snippet: methodName()$0 (no required params)
                let expected = format!("{}()$0", filter);
                assert_eq!(
                    insert, expected,
                    "insert_text should be a snippet with parens for '{}'",
                    filter
                );
                assert_eq!(
                    item.insert_text_format,
                    Some(InsertTextFormat::SNIPPET),
                    "insert_text_format should be Snippet"
                );
                // label should be the full signature, e.g. "render()"
                assert!(
                    item.label.starts_with(filter),
                    "Label '{}' should start with method name '{}'",
                    item.label,
                    filter
                );
            }
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

#[tokio::test]
async fn test_completion_method_insert_text_with_required_params() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///params.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Editor {\n",
        "    function updateText(string $text, $frogs = false): void {}\n",
        "    function setTitle(string $title): void {}\n",
        "    function reset() {}\n",
        "    function test() {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    )
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$this->` on line 6
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 6,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let method_items: Vec<&CompletionItem> = items
                .iter()
                .filter(|i| i.kind == Some(CompletionItemKind::METHOD))
                .collect();
            assert_eq!(
                method_items.len(),
                4,
                "Should have 4 method completions (3 original + test)"
            );

            // Find specific methods by filter_text (method name)
            // updateText(string $text, $frogs = false) — one required param
            let update_text = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("updateText"))
                .expect("Should have updateText");
            assert_eq!(
                update_text.insert_text.as_deref(),
                Some("updateText(${1:\\$text})$0"),
                "insert_text should be a snippet with required param $text"
            );
            assert_eq!(
                update_text.insert_text_format,
                Some(InsertTextFormat::SNIPPET),
            );

            // setTitle(string $title) — one required param
            let set_title = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("setTitle"))
                .expect("Should have setTitle");
            assert_eq!(
                set_title.insert_text.as_deref(),
                Some("setTitle(${1:\\$title})$0"),
                "insert_text should be a snippet with required param $title"
            );
            assert_eq!(
                set_title.insert_text_format,
                Some(InsertTextFormat::SNIPPET),
            );

            // reset() — no params
            let reset = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("reset"))
                .expect("Should have reset");
            assert_eq!(
                reset.insert_text.as_deref(),
                Some("reset()$0"),
                "insert_text should be a snippet with empty parens"
            );
            assert_eq!(reset.insert_text_format, Some(InsertTextFormat::SNIPPET),);
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

#[tokio::test]
async fn test_completion_method_insert_text_multiple_required_params() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///multi_params.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Calculator {\n",
        "    function add(int $a, int $b): int {}\n",
        "    function addWithLabel(int $a, int $b, string $label = 'sum'): int {}\n",
        "    function test() {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    )
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$this->` on line 5
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 5,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let method_items: Vec<&CompletionItem> = items
                .iter()
                .filter(|i| i.kind == Some(CompletionItemKind::METHOD))
                .collect();

            // add(int $a, int $b) — two required params
            let add = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("add"))
                .expect("Should have add");
            assert_eq!(
                add.insert_text.as_deref(),
                Some("add(${1:\\$a}, ${2:\\$b})$0"),
                "insert_text should be a snippet with two required params"
            );
            assert_eq!(add.insert_text_format, Some(InsertTextFormat::SNIPPET),);

            // addWithLabel(int $a, int $b, string $label = 'sum') — two required, one optional
            let add_with_label = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("addWithLabel"))
                .expect("Should have addWithLabel");
            assert_eq!(
                add_with_label.insert_text.as_deref(),
                Some("addWithLabel(${1:\\$a}, ${2:\\$b})$0"),
                "insert_text should include only the two required params"
            );
            assert_eq!(
                add_with_label.insert_text_format,
                Some(InsertTextFormat::SNIPPET),
            );
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

#[tokio::test]
async fn test_completion_method_insert_text_variadic_param() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///variadic.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Logger {\n",
        "    function log(string $message, ...$context): void {}\n",
        "    function logAll(...$messages): void {}\n",
        "    function test() {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    )
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$this->` on line 5
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 5,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let method_items: Vec<&CompletionItem> = items
                .iter()
                .filter(|i| i.kind == Some(CompletionItemKind::METHOD))
                .collect();

            // log(string $message, ...$context) — one required, one variadic
            let log = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("log"))
                .expect("Should have log");
            assert_eq!(
                log.insert_text.as_deref(),
                Some("log(${1:\\$message})$0"),
                "insert_text should include the required param but not the variadic"
            );
            assert_eq!(log.insert_text_format, Some(InsertTextFormat::SNIPPET),);

            // logAll(...$messages) — only variadic, no required
            let log_all = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("logAll"))
                .expect("Should have logAll");
            assert_eq!(
                log_all.insert_text.as_deref(),
                Some("logAll()$0"),
                "insert_text should be empty parens (variadic is not required)"
            );
            assert_eq!(log_all.insert_text_format, Some(InsertTextFormat::SNIPPET),);
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

#[tokio::test]
async fn test_completion_method_all_optional_params() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///optional.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Config {\n",
        "    function setup($debug = false, $verbose = false): void {}\n",
        "    function test() {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    )
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$this->` on line 4
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 4,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    match result.unwrap() {
        CompletionResponse::Array(items) => {
            // setup($debug = false, $verbose = false) — all optional
            let setup = items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("setup"))
                .expect("Should have setup");
            assert_eq!(
                setup.insert_text.as_deref(),
                Some("setup()$0"),
                "insert_text should be empty parens (all params are optional)"
            );
            assert_eq!(setup.insert_text_format, Some(InsertTextFormat::SNIPPET),);
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

#[tokio::test]
async fn test_completion_method_detail_shows_signature() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///detail.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Editor {\n",
        "    function updateText(string $text, $frogs = false): void {}\n",
        "    function test() {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    )
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$this->` on line 4
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 4,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let update = items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("updateText"))
                .expect("Should have updateText");

            // Label should show the full signature
            assert_eq!(
                update.label, "updateText($text, $frogs = ...)",
                "Label should show method name and parameter names"
            );

            // Detail shows the return type.
            assert_eq!(
                update.detail.as_deref(),
                Some("void"),
                "Method detail should show the return type"
            );
            // label_details.detail is None (detail field handles the
            // return type display).
            assert_eq!(
                update
                    .label_details
                    .as_ref()
                    .and_then(|ld| ld.detail.as_deref()),
                None,
                "label_details.detail should be None"
            );
            // label_details.description shows the owning class name
            // (visible in VS Code, ignored by Zed).
            assert_eq!(
                update
                    .label_details
                    .as_ref()
                    .and_then(|ld| ld.description.as_deref()),
                Some("Editor"),
                "label_details.description should show the class name"
            );

            // insert_text should be a snippet with the required param
            assert_eq!(
                update.insert_text.as_deref(),
                Some("updateText(${1:\\$text})$0"),
                "insert_text should be a snippet with required param $text"
            );
            assert_eq!(update.insert_text_format, Some(InsertTextFormat::SNIPPET),);
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

#[tokio::test]
async fn test_completion_resolve_union_member_shows_all_branches() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///union_resolve.php").unwrap();
    let text = r#"<?php
class Lamp {
    public function turnOff(): void {}
    public function dim(): void {}
}

class Faucet {
    public function turnOff(): void {}
    public function drip(): void {}
}

class Consumer {
    public function run(): void {
        if (rand(0, 1)) {
            $ambiguous = new Lamp();
        } else {
            $ambiguous = new Faucet();
        }
        $ambiguous->
    }
}
"#
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$ambiguous->` on line 18
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 18,
                character: 20,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    let items = match result.unwrap() {
        CompletionResponse::Array(items) => items,
        CompletionResponse::List(list) => list.items,
    };

    // Find the turnOff item (present on both branches).
    let turn_off = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("turnOff"))
        .expect("Should have turnOff in completions");

    // Resolve the item to get documentation.
    let resolved = backend.completion_resolve(turn_off.clone()).await.unwrap();
    let doc = match resolved.documentation {
        Some(Documentation::MarkupContent(mc)) => mc.value,
        other => panic!("Expected MarkupContent documentation, got: {:?}", other),
    };

    // The resolved documentation should show both classes.
    assert!(
        doc.contains("Lamp") && doc.contains("Faucet"),
        "resolved documentation should show both Lamp and Faucet, got: {}",
        doc
    );

    // The two branches should be separated by a horizontal rule.
    assert!(
        doc.contains("---"),
        "resolved documentation should use a horizontal rule separator, got: {}",
        doc
    );

    // Now resolve dim() — only on Lamp, should show a single class.
    let dim = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("dim"))
        .expect("Should have dim in completions");

    let resolved_dim = backend.completion_resolve(dim.clone()).await.unwrap();
    let dim_doc = match resolved_dim.documentation {
        Some(Documentation::MarkupContent(mc)) => mc.value,
        other => panic!("Expected MarkupContent documentation, got: {:?}", other),
    };

    assert!(
        dim_doc.contains("Lamp"),
        "dim documentation should show Lamp, got: {}",
        dim_doc
    );
    assert!(
        !dim_doc.contains("Faucet"),
        "dim documentation should NOT show Faucet, got: {}",
        dim_doc
    );
    assert!(
        !dim_doc.contains("---"),
        "single-branch member should not have separator, got: {}",
        dim_doc
    );
}

#[tokio::test]
async fn test_completion_resolve_union_deduplicates_common_ancestor() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///union_dedup.php").unwrap();
    let text = r#"<?php
class BaseDevice {
    public function turnOff(): void {}
}

class Lamp extends BaseDevice {
    public function dim(): void {}
}

class Faucet extends BaseDevice {
    public function drip(): void {}
}

class Consumer {
    public function run(): void {
        if (rand(0, 1)) {
            $ambiguous = new Lamp();
        } else {
            $ambiguous = new Faucet();
        }
        $ambiguous->
    }
}
"#
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$ambiguous->` on line 20
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 20,
                character: 20,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    let items = match result.unwrap() {
        CompletionResponse::Array(items) => items,
        CompletionResponse::List(list) => list.items,
    };

    let turn_off = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("turnOff"))
        .expect("Should have turnOff in completions");

    let resolved = backend.completion_resolve(turn_off.clone()).await.unwrap();
    let doc = match resolved.documentation {
        Some(Documentation::MarkupContent(mc)) => mc.value,
        other => panic!("Expected MarkupContent documentation, got: {:?}", other),
    };

    // Both Lamp and Faucet inherit turnOff from BaseDevice.  The
    // resolved hover should show BaseDevice (the declaring class)
    // and should NOT be duplicated.
    assert!(
        doc.contains("BaseDevice"),
        "documentation should show declaring class BaseDevice, got: {}",
        doc
    );
    assert!(
        !doc.contains("---"),
        "should not have separator when both branches resolve to same declaring class, got: {}",
        doc
    );
}