php-lsp 0.29.0

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

use super::*;

use expect_test::expect;

// ── Interface method declarations ──────────────────────────────────────────

#[tokio::test]
async fn interface_method_from_concrete_impl() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
interface Logger {
    public function log(string $msg): void;
    //              ^^^ decl
}
class FileLogger implements Logger {
    public function log$0(string $msg): void {}
}
"#,
    )
    .await;
}

#[tokio::test]
async fn interface_method_from_call_site() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
interface Logger {
    public function log(string $msg): void;
    //              ^^^ decl
}
class FileLogger implements Logger {
    public function log(string $msg): void {}
}
$logger = new FileLogger();
$logger->log$0('hello');
"#,
    )
    .await;
}

#[tokio::test]
async fn interface_method_on_declaration_site() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
interface Logger {
    public function log$0(string $msg): void;
    //              ^^^ decl
}
class FileLogger implements Logger {
    public function log(string $msg): void {}
}
"#,
    )
    .await;
}

#[tokio::test]
async fn cross_file_interface_method() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"//- /Logger.php
<?php
interface Logger {
    public function log(string $msg): void;
    //              ^^^ decl
}

//- /FileLogger.php
<?php
class FileLogger implements Logger {
    public function log$0(string $msg): void {}
}
"#,
    )
    .await;
}

#[tokio::test]
async fn two_interfaces_same_method() {
    let mut s = TestServer::new().await;
    let out = s
        .check_declaration(
            r#"<?php
interface A {
    public function handle(): void;
}
interface B {
    public function handle(): void;
}
class Handler implements A, B {
    public function handle$0(): void {}
}
"#,
        )
        .await;
    expect![[r#"main.php:2:20-2:26"#]].assert_eq(&out);
}

#[tokio::test]
async fn interface_name_itself() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
interface Logger$0 {
//        ^^^^^^ decl
    public function log(): void;
}
"#,
    )
    .await;
}

// ── Abstract class method declarations ─────────────────────────────────────

#[tokio::test]
async fn abstract_method_from_concrete_subclass() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
abstract class Base {
    abstract public function build(): void;
    //                       ^^^^^ decl
}
class Impl extends Base {
    public function build$0(): void {}
}
"#,
    )
    .await;
}

#[tokio::test]
async fn abstract_method_on_declaration_site() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
abstract class Base {
    abstract public function build$0(): void;
    //                       ^^^^^ decl
}
class Impl extends Base {
    public function build(): void {}
}
"#,
    )
    .await;
}

#[tokio::test]
async fn cross_file_abstract_method() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"//- /Base.php
<?php
abstract class Base {
    abstract public function build(): void;
    //                       ^^^^^ decl
}

//- /Impl.php
<?php
class Impl extends Base {
    public function build$0(): void {}
}
"#,
    )
    .await;
}

// ── Abstract trait methods (bug case) ───────────────────────────────────────

#[tokio::test]
async fn abstract_trait_method_from_using_class() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
trait Renderable {
    abstract public function render(): string;
    //                       ^^^^^^ decl
}
class Page {
    use Renderable;
    public function render$0(): string { return ''; }
}
"#,
    )
    .await;
}

// ── Concrete fallback (declaration == definition) ──────────────────────────

#[tokio::test]
async fn concrete_function_falls_back_to_definition() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
function greet(): string { return 'hi'; }
//       ^^^^^ decl
greet$0();
"#,
    )
    .await;
}

#[tokio::test]
async fn concrete_class_name() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
class Widget {
//    ^^^^^^ decl
    public function show(): void {}
}
$w = new Widget$0();
"#,
    )
    .await;
}

#[tokio::test]
async fn trait_name_falls_back() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
trait Loggable$0 {
//    ^^^^^^^^ decl
    public function log(): void {}
}
class Page {
    use Loggable;
}
"#,
    )
    .await;
}

#[tokio::test]
async fn enum_name_falls_back() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
enum Suit$0 {
//   ^^^^ decl
    case Hearts;
    public function label(): string { return 'H'; }
}
"#,
    )
    .await;
}

// ── Cross-file fallback ────────────────────────────────────────────────────

#[tokio::test]
async fn cross_file_function_fallback() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"//- /helpers.php
<?php
function validate(string $x): bool { return true; }
//       ^^^^^^^^ decl

//- /main.php
<?php
$ok = validate$0('test');
"#,
    )
    .await;
}

// ── Constants and enum members ────────────────────────────────────────────

#[tokio::test]
async fn class_constant_declaration() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
class Config {
    const DEBUG$0 = true;
    //    ^^^^^ decl
}
"#,
    )
    .await;
}

#[tokio::test]
async fn enum_case_declaration() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
enum Status {
    case Active$0;
    //   ^^^^^^ decl
    case Inactive;
}
"#,
    )
    .await;
}

#[tokio::test]
async fn enum_constant_declaration() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
enum Suit {
    case Hearts;
    const MAX_VALUE$0 = 100;
    //    ^^^^^^^^^ decl
}
"#,
    )
    .await;
}

#[tokio::test]
async fn class_property_declaration() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
class User {
    public string $name$0;
    //             ^^^^ decl
    public function getName(): string { return $this->name; }
}
"#,
    )
    .await;
}

#[tokio::test]
async fn trait_property_declaration() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
trait Timestampable {
    protected string $created$0;
    //                ^^^^^^^ decl
}
class Post {
    use Timestampable;
}
"#,
    )
    .await;
}

#[tokio::test]
async fn property_cursor_on_usage_finds_declaration() {
    let mut s = TestServer::new().await;
    s.check_declaration_annotated(
        r#"<?php
class User {
    public string $email;
    //             ^^^^^ decl
}
$u = new User();
$u->email$0 = 'test@example.com';
"#,
    )
    .await;
}

// ── Edge cases ─────────────────────────────────────────────────────────────

#[tokio::test]
async fn unknown_word_returns_none() {
    let mut s = TestServer::new().await;
    let out = s
        .check_declaration(
            r#"<?php
class Foo {}
$x = new Undefined$0Class();
"#,
        )
        .await;
    expect![[r#"<none>"#]].assert_eq(&out);
}

#[tokio::test]
async fn variable_returns_none() {
    let mut s = TestServer::new().await;
    let out = s
        .check_declaration(
            r#"<?php
$x$0 = 42;
"#,
        )
        .await;
    expect![[r#"<none>"#]].assert_eq(&out);
}

#[tokio::test]
async fn empty_file_returns_none() {
    let mut s = TestServer::new().await;
    let out = s
        .check_declaration(
            r#"<?php
$0"#,
        )
        .await;
    expect![[r#"<none>"#]].assert_eq(&out);
}

// ── Keyword tokens (regression) ───────────────────────────────────────────
//
// A bare PHP keyword can never be a declaration name. Before the
// `is_bare_keyword_at` gate, `decls_by_name.get(&word)` always missed for
// these, so every keyword click fell through to the exhaustive
// `any_declaration_in_file` scan over every workspace file.

#[tokio::test]
async fn keyword_token_returns_none() {
    let mut s = TestServer::new().await;
    let out = s
        .check_declaration(
            r#"<?php
abstra$0ct class Foo {
    abstract public function build(): void;
}
"#,
        )
        .await;
    expect![[r#"<none>"#]].assert_eq(&out);
}

/// A semi-reserved word (`final`) is a valid method name, so a cross-file
/// method literally named `final` can sit in another open doc. The
/// open-docs pass (`goto_declaration`) ran before `goto_declaration_from_
/// index` and had no bare-keyword gate of its own, so it could resolve the
/// class-modifier `final` here to that unrelated method via
/// `resolve_declaration`'s bare-name search.
#[tokio::test]
async fn keyword_token_ignores_cross_file_name_collision() {
    let mut s = TestServer::new().await;
    let out = s
        .check_declaration(
            r#"//- /src/Other.php
<?php
class Other {
    public function final(): void {}
}

//- /src/main.php
<?php
fina$0l class Locked {}
"#,
        )
        .await;
    expect![[r#"<none>"#]].assert_eq(&out);
}

/// Same as `keyword_token_returns_none` but against unopened background
/// files, so the request must go through `goto_declaration_from_index`'s
/// exhaustive fallback scan rather than the open-doc AST pass.
#[tokio::test]
async fn keyword_in_unopened_workspace_returns_none() {
    let tmp = tempfile::tempdir().unwrap();
    for i in 0..10 {
        std::fs::write(
            tmp.path().join(format!("Noise{i}.php")),
            format!(
                "<?php\nabstract class Noise{i} {{\n    abstract public function run(): void;\n}}\n"
            ),
        )
        .unwrap();
    }
    let caller_src =
        "<?php\nabstract class Caller {\n    abstract public function run(): void;\n}\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "abstract class Caller", 0);
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["<none>"].assert_eq(&out);
}

// ── Stub-index fallback (unopened file) ──────────────────────────────────────
//
// Tests for declaration resolution through FileIndex entries (unopened files).
// These use tempdir to write files to disk, start a rooted server that scans
// them, then only open the caller file so the declaration target is index-only.

/// Abstract method in unopened parent class.
#[tokio::test]
async fn declaration_from_unopened_abstract_method() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Animal.php"),
        "<?php\nabstract class Animal {\n    abstract public function speak(): string;\n}\n",
    )
    .unwrap();
    let caller_src = "<?php\nfunction call(Animal $a): string { return $a->speak(); }\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "speak()", 0);
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["Animal.php:2:29-2:34"].assert_eq(&out);
}

/// Interface method in unopened interface.
#[tokio::test]
async fn declaration_from_unopened_interface_method() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Logger.php"),
        "<?php\ninterface Logger {\n    public function log(string $msg): void;\n}\n",
    )
    .unwrap();
    let caller_src = "<?php\nfunction emit(Logger $l, string $m): void { $l->log($m); }\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "log($m)", 0);
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["Logger.php:2:20-2:23"].assert_eq(&out);
}

/// Interface name in unopened interface.
#[tokio::test]
async fn declaration_from_unopened_interface_name() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Logger.php"),
        "<?php\ninterface Logger {\n    public function log(): void;\n}\n",
    )
    .unwrap();
    let caller_src = "<?php\nfunction emit(Logger $l): void { $l; }\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "Logger $l", 0);
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["Logger.php:1:10-1:16"].assert_eq(&out);
}

/// Free function in unopened file.
#[tokio::test]
async fn declaration_from_unopened_function() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("helpers.php"),
        "<?php\nfunction format_name(string $s): string { return $s; }\n",
    )
    .unwrap();
    let caller_src = "<?php\nfunction caller(): string { return format_name('x'); }\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "format_name('x')", 0);
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["helpers.php:1:9-1:20"].assert_eq(&out);
}

/// Class name in unopened class.
#[tokio::test]
async fn declaration_from_unopened_class_name() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Widget.php"),
        "<?php\nclass Widget {\n    public function render(): void {}\n}\n",
    )
    .unwrap();
    let caller_src = "<?php\nfunction make(): Widget { return new Widget(); }\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "new Widget", 0);
    let ch = ch + "new ".len() as u32;
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["Widget.php:1:6-1:12"].assert_eq(&out);
}

/// Method in unopened class.
#[tokio::test]
async fn declaration_from_unopened_method() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Service.php"),
        "<?php\nclass Service {\n    public function execute(): void {}\n}\n",
    )
    .unwrap();
    let caller_src = "<?php\nfunction run(Service $s): void { $s->execute(); }\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "execute()", 0);
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["Service.php:2:20-2:27"].assert_eq(&out);
}

/// Property in unopened class (previously unimplemented for index path).
#[tokio::test]
async fn declaration_from_unopened_property() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Entity.php"),
        "<?php\nclass Entity {\n    public string $name = '';\n}\n",
    )
    .unwrap();
    let caller_src = "<?php\nfunction get(Entity $e): string { return $e->name; }\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "->name", 0);
    let ch = ch + "->".len() as u32; // move cursor to start of property name
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["Entity.php:2:19-2:23"].assert_eq(&out);
}

/// Abstract method in unopened trait.
#[tokio::test]
async fn declaration_from_unopened_trait_abstract_method() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Loggable.php"),
        "<?php\ntrait Loggable {\n    abstract public function record(): void;\n}\n",
    )
    .unwrap();
    let caller_src = "<?php\nfunction output(): void { $x->record(); }\n";
    std::fs::write(tmp.path().join("caller.php"), caller_src).unwrap();

    let mut s = TestServer::with_root(tmp.path()).await;
    s.wait_for_index_ready().await;
    s.open("caller.php", caller_src).await;

    let (_, line, ch) = s.locate("caller.php", "record()", 0);
    let resp = s.declaration("caller.php", line, ch).await;
    let out = common::render_locations(&resp, &s.uri(""));
    expect!["Loggable.php:2:29-2:35"].assert_eq(&out);
}