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
use crate::common::create_test_backend;
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![],
    }
}

/// Collect the filter_text values from completion items.
fn filter_texts(items: &[CompletionItem]) -> Vec<&str> {
    items
        .iter()
        .filter_map(|i| i.filter_text.as_deref())
        .collect()
}

// ─── Basic: method on same-file class ───────────────────────────────────────

/// Named arg completion for `$this->method(|)` inside the same class.
#[tokio::test]
async fn test_named_args_this_method() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_this.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Greeter {\n",
        "    public function greet(string $name, int $age): string {\n",
        "        return '';\n",
        "    }\n",
        "    public function test() {\n",
        "        $this->greet(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 6, 22).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"name"),
        "Should suggest 'name' param. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"age"),
        "Should suggest 'age' param. Got: {:?}",
        tags
    );
}

/// Named arg items should have VARIABLE kind.
#[tokio::test]
async fn test_named_args_have_variable_kind() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_kind.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function run(int $count): void {}\n",
        "    public function test() {\n",
        "        $this->run(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 4, 20).await;
    for item in &items {
        assert_eq!(
            item.kind,
            Some(CompletionItemKind::VARIABLE),
            "Named arg '{:?}' should use VARIABLE kind",
            item.label
        );
    }
}

/// The insert text should be `name: ` (with colon and trailing space).
#[tokio::test]
async fn test_named_args_insert_text_format() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_insert.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function run(int $count): void {}\n",
        "    public function test() {\n",
        "        $this->run(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 4, 20).await;
    let count_item = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("count"));
    assert!(count_item.is_some(), "Should have 'count' completion");
    assert_eq!(
        count_item.unwrap().insert_text.as_deref(),
        Some("count: "),
        "Insert text should be 'name: '"
    );
}

/// The label should show the type when available.
#[tokio::test]
async fn test_named_args_label_with_type() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_label.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function create(string $name, ?int $priority): void {}\n",
        "    public function test() {\n",
        "        $this->create(\n",
        "    }\n",
        "}\n",
    );

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

    let name_item = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("name"));
    assert!(name_item.is_some(), "Should have 'name' completion");
    assert_eq!(name_item.unwrap().label, "name: string");

    let priority_item = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("priority"));
    assert!(priority_item.is_some(), "Should have 'priority' completion");
    assert_eq!(priority_item.unwrap().label, "priority: ?int");
}

/// Label without type hint should be `name:` (no space before colon).
#[tokio::test]
async fn test_named_args_label_without_type() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_notype.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function process($data): void {}\n",
        "    public function test() {\n",
        "        $this->process(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 4, 24).await;
    let data_item = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("data"));
    assert!(data_item.is_some(), "Should have 'data' completion");
    assert_eq!(data_item.unwrap().label, "data:");
}

// ─── Skipping already-used named args ───────────────────────────────────────

/// Already-specified named args should not appear in suggestions.
#[tokio::test]
async fn test_named_args_skip_already_used() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_skip.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function greet(string $name, int $age, bool $formal): string {\n",
        "        return '';\n",
        "    }\n",
        "    public function test() {\n",
        "        $this->greet(name: 'Alice', \n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 6, 36).await;
    let tags = filter_texts(&items);

    assert!(
        !tags.contains(&"name"),
        "Should NOT suggest 'name' (already used). Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"age"),
        "Should suggest 'age'. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"formal"),
        "Should suggest 'formal'. Got: {:?}",
        tags
    );
}

// ─── Positional arguments ───────────────────────────────────────────────────

/// Positional arguments before the cursor should exclude leading params.
#[tokio::test]
async fn test_named_args_skip_positional() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_pos.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function setup(string $host, int $port, bool $ssl): void {}\n",
        "    public function test() {\n",
        "        $this->setup('localhost', \n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 4, 34).await;
    let tags = filter_texts(&items);

    assert!(
        !tags.contains(&"host"),
        "Should NOT suggest 'host' (covered by positional). Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"port"),
        "Should suggest 'port'. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"ssl"),
        "Should suggest 'ssl'. Got: {:?}",
        tags
    );
}

// ─── Prefix filtering ──────────────────────────────────────────────────────

/// Typing a partial prefix should filter the suggestions.
#[tokio::test]
async fn test_named_args_prefix_filter() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_prefix.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function config(string $name, int $notify, bool $narrow): void {}\n",
        "    public function test() {\n",
        "        $this->config(na\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 4, 24).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"name"),
        "Should suggest 'name' (matches 'na'). Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"narrow"),
        "Should suggest 'narrow' (matches 'na'). Got: {:?}",
        tags
    );
    assert!(
        !tags.contains(&"notify"),
        "Should NOT suggest 'notify' (doesn't match 'na'). Got: {:?}",
        tags
    );
}

// ─── Constructor ────────────────────────────────────────────────────────────

/// Named args should work for `new ClassName(|)`.
#[tokio::test]
async fn test_named_args_constructor() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_ctor.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class UserDTO {\n",
        "    public function __construct(\n",
        "        public string $name,\n",
        "        public int $age,\n",
        "        public string $email = '',\n",
        "    ) {}\n",
        "}\n",
        "$dto = new UserDTO(\n",
    );

    let items = complete_at(&backend, &uri, text, 8, 19).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"name"),
        "Should suggest 'name'. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"age"),
        "Should suggest 'age'. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"email"),
        "Should suggest 'email'. Got: {:?}",
        tags
    );
}

/// Constructor with some args already filled.
#[tokio::test]
async fn test_named_args_constructor_partial() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_ctor2.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Point {\n",
        "    public function __construct(public int $x, public int $y, public int $z = 0) {}\n",
        "}\n",
        "$p = new Point(x: 1, \n",
    );

    let items = complete_at(&backend, &uri, text, 4, 21).await;
    let tags = filter_texts(&items);

    assert!(
        !tags.contains(&"x"),
        "Should NOT suggest 'x' (already used). Got: {:?}",
        tags
    );
    assert!(tags.contains(&"y"), "Should suggest 'y'. Got: {:?}", tags);
    assert!(tags.contains(&"z"), "Should suggest 'z'. Got: {:?}", tags);
}

// ─── Static method ──────────────────────────────────────────────────────────

/// Named args should work for `ClassName::method(|)`.
#[tokio::test]
async fn test_named_args_static_method() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_static.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Factory {\n",
        "    public static function create(string $type, array $options = []): self {\n",
        "        return new self();\n",
        "    }\n",
        "}\n",
        "Factory::create(\n",
    );

    let items = complete_at(&backend, &uri, text, 6, 17).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"type"),
        "Should suggest 'type'. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"options"),
        "Should suggest 'options'. Got: {:?}",
        tags
    );
}

// ─── self:: / static:: / parent:: ──────────────────────────────────────────

/// Named args for `self::method(|)`.
#[tokio::test]
async fn test_named_args_self_static_method() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_self.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Builder {\n",
        "    public static function make(string $name, int $count = 1): self {\n",
        "        return new self();\n",
        "    }\n",
        "    public function test() {\n",
        "        self::make(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 6, 19).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"name"),
        "Should suggest 'name' via self::. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"count"),
        "Should suggest 'count' via self::. Got: {:?}",
        tags
    );
}

/// Named args for `parent::__construct(|)`.
#[tokio::test]
async fn test_named_args_parent_construct() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_parent.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Base {\n",
        "    public function __construct(public string $name, public int $id = 0) {}\n",
        "}\n",
        "class Child extends Base {\n",
        "    public function __construct() {\n",
        "        parent::__construct(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 6, 28).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"name"),
        "Should suggest 'name' via parent::. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"id"),
        "Should suggest 'id' via parent::. Got: {:?}",
        tags
    );
}

// ─── Variable method call ───────────────────────────────────────────────────

/// Named args for `$var->method(|)` where $var is typed.
#[tokio::test]
async fn test_named_args_variable_method() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_var.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Logger {\n",
        "    public function log(string $message, int $level = 0): void {}\n",
        "}\n",
        "class App {\n",
        "    public function run() {\n",
        "        $logger = new Logger();\n",
        "        $logger->log(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 7, 22).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"message"),
        "Should suggest 'message'. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"level"),
        "Should suggest 'level'. Got: {:?}",
        tags
    );
}

// ─── Not triggered in wrong contexts ────────────────────────────────────────

/// Should NOT trigger named args when typing a variable inside call parens.
#[tokio::test]
async fn test_named_args_not_triggered_for_variables() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_novar.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function greet(string $name): void {}\n",
        "    public function test() {\n",
        "        $val = 'hello';\n",
        "        $this->greet($va\n",
        "    }\n",
        "}\n",
    );

    // Cursor is after `$va` — variable completion should handle, not named args
    let items = complete_at(&backend, &uri, text, 5, 24).await;

    // Named arg items use VARIABLE kind with `name:` format.
    // Variable completions also use VARIABLE kind but start with `$`.
    let named_arg_items: Vec<_> = items
        .iter()
        .filter(|i| i.insert_text.as_deref().is_some_and(|t| t.ends_with(": ")))
        .collect();

    assert!(
        named_arg_items.is_empty(),
        "Should NOT suggest named args when typing a variable. Got: {:?}",
        named_arg_items.iter().map(|i| &i.label).collect::<Vec<_>>()
    );
}

/// Should NOT trigger named args after `->` inside call parens.
#[tokio::test]
async fn test_named_args_not_triggered_after_arrow() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_noarrow.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public string $value = '';\n",
        "    public function greet(string $name): void {}\n",
        "    public function test() {\n",
        "        $this->greet($this->\n",
        "    }\n",
        "}\n",
    );

    // Cursor after `$this->` — member completion should handle this
    let items = complete_at(&backend, &uri, text, 5, 28).await;

    let named_arg_items: Vec<_> = items
        .iter()
        .filter(|i| i.insert_text.as_deref().is_some_and(|t| t.ends_with(": ")))
        .collect();

    assert!(
        named_arg_items.is_empty(),
        "Should NOT suggest named args after '->'. Got: {:?}",
        named_arg_items.iter().map(|i| &i.label).collect::<Vec<_>>()
    );
}

// ─── Multiline calls ────────────────────────────────────────────────────────

/// Named args should work across multiple lines.
#[tokio::test]
async fn test_named_args_multiline() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_multi.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Config {\n",
        "    public function set(string $key, string $value, bool $overwrite = true): void {}\n",
        "    public function test() {\n",
        "        $this->set(\n",
        "            key: 'app.name',\n",
        "            \n",
        "        );\n",
        "    }\n",
        "}\n",
    );

    // Cursor on the empty line (line 6, after indentation)
    let items = complete_at(&backend, &uri, text, 6, 12).await;
    let tags = filter_texts(&items);

    assert!(
        !tags.contains(&"key"),
        "Should NOT suggest 'key' (already used). Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"value"),
        "Should suggest 'value'. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"overwrite"),
        "Should suggest 'overwrite'. Got: {:?}",
        tags
    );
}

// ─── Detail text ────────────────────────────────────────────────────────────

/// Optional parameters should show "(optional)" in detail.
#[tokio::test]
async fn test_named_args_detail_optional() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_detail.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function config(string $host, int $port = 80): void {}\n",
        "    public function test() {\n",
        "        $this->config(\n",
        "    }\n",
        "}\n",
    );

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

    let host_item = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("host"));
    assert!(host_item.is_some(), "Should have 'host' completion");
    assert_eq!(host_item.unwrap().detail.as_deref(), Some("Named argument"),);

    let port_item = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("port"));
    assert!(port_item.is_some(), "Should have 'port' completion");
    assert_eq!(
        port_item.unwrap().detail.as_deref(),
        Some("Named argument (optional)"),
    );
}

// ─── No params function ─────────────────────────────────────────────────────

/// Functions with no parameters should produce no named arg completions.
#[tokio::test]
async fn test_named_args_no_params() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_noparams.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function noop(): void {}\n",
        "    public function test() {\n",
        "        $this->noop(\n",
        "    }\n",
        "}\n",
    );

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

    let named_arg_items: Vec<_> = items
        .iter()
        .filter(|i| i.insert_text.as_deref().is_some_and(|t| t.ends_with(": ")))
        .collect();

    assert!(
        named_arg_items.is_empty(),
        "Should not suggest named args for parameterless method. Got: {:?}",
        named_arg_items.iter().map(|i| &i.label).collect::<Vec<_>>()
    );
}

// ─── All params already used ────────────────────────────────────────────────

/// When all params are already specified, no named arg completions.
#[tokio::test]
async fn test_named_args_all_used() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_allused.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function pair(int $x, int $y): void {}\n",
        "    public function test() {\n",
        "        $this->pair(x: 1, y: 2, \n",
        "    }\n",
        "}\n",
    );

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

    let named_arg_items: Vec<_> = items
        .iter()
        .filter(|i| i.insert_text.as_deref().is_some_and(|t| t.ends_with(": ")))
        .collect();

    assert!(
        named_arg_items.is_empty(),
        "Should not suggest named args when all are used. Got: {:?}",
        named_arg_items.iter().map(|i| &i.label).collect::<Vec<_>>()
    );
}

// ─── Inherited method ───────────────────────────────────────────────────────

/// Named args should work for inherited methods.
#[tokio::test]
async fn test_named_args_inherited_method() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_inherit.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Base {\n",
        "    public function save(string $filename, bool $overwrite = false): void {}\n",
        "}\n",
        "class Child extends Base {\n",
        "    public function test() {\n",
        "        $this->save(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 6, 20).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"filename"),
        "Should suggest 'filename' from inherited method. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"overwrite"),
        "Should suggest 'overwrite' from inherited method. Got: {:?}",
        tags
    );
}

// ─── Symbol-map primary path tests ──────────────────────────────────────────

/// Named arg completion for a property-chain subject (`$this->prop->method()`).
///
/// The text scanner historically fails on this because it can't resolve
/// through property chains.  The symbol map provides the correct
/// `call_expression` from the AST.  The call is syntactically closed so
/// the parser can produce a `CallSite`.
#[tokio::test]
async fn test_named_args_property_chain_subject() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_prop_chain.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Formatter {\n",
        "    public function format(string $template, bool $escape = true): string {\n",
        "        return '';\n",
        "    }\n",
        "}\n",
        "class Service {\n",
        "    /** @var Formatter */\n",
        "    public Formatter $formatter;\n",
        "    public function run() {\n",
        "        $this->formatter->format();\n",
        "    }\n",
        "}\n",
    );

    // Cursor inside the parens of format() — col 33 is between `(` and `)`
    let items = complete_at(&backend, &uri, text, 10, 33).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"template"),
        "Should suggest 'template' from property chain. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"escape"),
        "Should suggest 'escape' from property chain. Got: {:?}",
        tags
    );
}

/// Named arg completion for a bare class name subject (`ClassName::method(`).
///
/// Before the fix, `resolve_named_arg_params` returned empty for static
/// method calls when the class name was not a keyword (self/static/parent).
/// Now all static subjects are routed through `resolve_target_classes` as
/// a fallback.
#[tokio::test]
async fn test_named_args_class_name_static_method() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_class_static.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Builder {\n",
        "    public static function create(string $name, int $priority = 0): static {\n",
        "        return new static();\n",
        "    }\n",
        "}\n",
        "class Client {\n",
        "    public function test() {\n",
        "        Builder::create(\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 8, 25).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"name"),
        "Should suggest 'name' from static method on class name. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"priority"),
        "Should suggest 'priority' from static method on class name. Got: {:?}",
        tags
    );
}

/// Named arg completion when the call is on a method return result chain.
///
/// `$this->getLogger()->log()` — the symbol map resolves the chain through
/// the return type, while the text scanner would fail on the `)` before `->`.
/// The call is syntactically closed so the parser can produce a `CallSite`.
#[tokio::test]
async fn test_named_args_method_return_chain() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_chain.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Logger {\n",
        "    public function log(string $message, int $level = 0): void {}\n",
        "}\n",
        "class App {\n",
        "    public function getLogger(): Logger {\n",
        "        return new Logger();\n",
        "    }\n",
        "    public function run() {\n",
        "        $this->getLogger()->log();\n",
        "    }\n",
        "}\n",
    );

    // Cursor right after the opening `(` of log() — col 32
    let items = complete_at(&backend, &uri, text, 9, 32).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"message"),
        "Should suggest 'message' from chained method call. Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"level"),
        "Should suggest 'level' from chained method call. Got: {:?}",
        tags
    );
}

/// Named arg completion with a prefix filter on a symbol-map-detected call.
///
/// Verifies that the prefix extraction works correctly when the detection
/// comes from the symbol map path rather than the text scanner.
#[tokio::test]
async fn test_named_args_symbol_map_with_prefix() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_sm_prefix.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Greeter {\n",
        "    public function greet(string $name, int $age): string {\n",
        "        return '';\n",
        "    }\n",
        "    public function test() {\n",
        "        $this->greet(na\n",
        "    }\n",
        "}\n",
    );

    // cursor after "na" — should filter to only "name"
    let items = complete_at(&backend, &uri, text, 6, 24).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"name"),
        "Should suggest 'name' matching prefix 'na'. Got: {:?}",
        tags
    );
    assert!(
        !tags.contains(&"age"),
        "Should NOT suggest 'age' (does not match prefix 'na'). Got: {:?}",
        tags
    );
}

/// Named arg completion for `(new ClassName)->method()` via symbol map.
///
/// The text scanner's `extract_call_expression` returns `None` when it
/// encounters `)` before `->` (chain through constructor result).  The
/// symbol map handles this correctly.  The call is syntactically closed
/// so the parser can produce a `CallSite`.
#[tokio::test]
async fn test_named_args_new_expression_chain() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///na_new_chain.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Renderer {\n",
        "    public function render(string $view, array $data = []): string {\n",
        "        return '';\n",
        "    }\n",
        "}\n",
        "function test() {\n",
        "    (new Renderer())->render();\n",
        "}\n",
    );

    // Cursor inside the parens of render() — col 29 is between `(` and `)`
    let items = complete_at(&backend, &uri, text, 7, 29).await;
    let tags = filter_texts(&items);

    assert!(
        tags.contains(&"view"),
        "Should suggest 'view' from (new Renderer)->render(). Got: {:?}",
        tags
    );
    assert!(
        tags.contains(&"data"),
        "Should suggest 'data' from (new Renderer)->render(). Got: {:?}",
        tags
    );
}