php-lsp 0.8.0

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

use super::*;

use expect_test::expect;

#[tokio::test]
async fn hover_abstract_class_shows_keyword() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
abstract class Bas$0eHandler {}
"#,
        expect![[r#"
            ```php
            abstract class BaseHandler
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_abstract_method_shows_modifiers() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
abstract class Base {
    abstract protected function pro$0cess(string $input): string;
}
"#,
        expect![[r#"
            ```php
            protected abstract function process(string $input): string
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_arrow_function_keyword() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php $f = f$0n(int $a): string => 'hello';"#,
        expect![[r#"
            ```php
            fn(int $a): string
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_attribute_class_name() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class MyAttribute {}

#[MyAttri$0bute]
class Foo {}
"#,
        expect![[r#"
            ```php
            class MyAttribute
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_attribute_via_use_alias() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Route {}
use Route as HttpRoute;

#[HttpRou$0te]
class Api {}
"#,
        // Resolves alias → Route
        expect![[r#"
            ```php
            class Route
            ```"#]],
    )
    .await;
}

// ── 2.2 Named argument hover ──────────────────────────────────────────────────

#[tokio::test]
async fn hover_attribute_with_args() {
    // Cursor on attribute class name when the attribute has constructor arguments.
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Route {
    public function __construct(string $path) {}
}

#[Rou$0te('/api')]
class Controller {}
"#,
        expect![[r#"
            ```php
            class Route
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_attribute_with_docblock() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
/** Marks a class as a service container. */
class Service {}

#[Servi$0ce]
class Mailer {}
"#,
        expect![[r#"
            ```php
            class Service
            ```

            ---

            Marks a class as a service container."#]],
    )
    .await;
}

#[tokio::test]
async fn hover_backed_int_enum_shows_backing_type() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
enum Priorit$0y: int { case Low = 1; case High = 2; }
"#,
        expect![[r#"
            ```php
            enum Priority: int
            ```"#]],
    )
    .await;
}

// ── Class modifiers ───────────────────────────────────────────────────────────

#[tokio::test]
async fn hover_class_const_in_static_access() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Config {
    const DEBUG = true;
}
if (Config::DEB$0UG) { }
"#,
        expect![[r#"
            ```php
            const bool DEBUG = true
            ```"#]],
    )
    .await;
}

/// Hovering on backed enum case in a match arm should show the value.
#[tokio::test]
async fn hover_closure_as_argument() {
    // Cursor on `function` keyword passed as a callback argument.
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
function apply(callable $fn): void {}
apply(fun$0ction(int $n): int { return $n * 2; });
"#,
        expect![[r#"
            ```php
            function(int $n): int
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_closure_inside_if_body() {
    // Closure nested inside an if body — the walker must recurse into if branches.
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
if (true) {
    $fn = fun$0ction(int $x): string { return (string) $x; };
}
"#,
        expect![[r#"
            ```php
            function(int $x): string
            ```"#]],
    )
    .await;
}

/// Hovering on `new Foo()` (the constructor call) must resolve to the class definition.
#[tokio::test]
async fn hover_closure_keyword() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php $fn = fun$0ction(int $x, string $y): bool { return true; };"#,
        expect![[r#"
            ```php
            function(int $x, string $y): bool
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_closure_no_params_no_return() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php $fn = fun$0ction() { return 1; };"#,
        expect![[r#"
            ```php
            function()
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_deprecated_function_shows_banner() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
/** @deprecated Use newGreet() instead */
function ol$0dGreet(): void {}
"#,
        expect![[r#"
            ```php
            function oldGreet(): void
            ```

            ---

            > **Deprecated**: Use newGreet() instead"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_docblock_annotated_function() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
/**
 * Greets the user.
 * @param string $name the name
 * @return string
 */
function gr$0eet(string $name): string { return $name; }
"#,
        expect![[r#"
            ```php
            function greet(string $name): string
            ```

            ---

            Greets the user.

            **@return** `string`
            **@param** `string` `$name` — the name"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_enum_case_in_match_arm() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
enum Status { case Active; case Inactive; }
$status = Status::Active;
match ($status) {
    Status::Act$0ive => echo 'active',
}
"#,
        expect![[r#"
            ```php
            case Status::Active
            ```"#]],
    )
    .await;
}

/// Hovering on class constant in static access should show the constant.
#[tokio::test]
async fn hover_final_class_shows_keyword() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
final class Concret$0eService {}
"#,
        expect![[r#"
            ```php
            final class ConcreteService
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_final_method_shows_modifiers() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Locked {
    final public function sea$0l(): void {}
}
"#,
        expect![[r#"
            ```php
            public final function seal(): void
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_first_class_callable_builtin() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php $fn = str$0len(...);"#,
        expect![[r#"
            ```php
            function strlen()
            ```

            [php.net documentation](https://www.php.net/function.strlen)"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_first_class_callable_user_function() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php function double(int $n): int {} $fn = dou$0ble(...);"#,
        expect![[r#"
            ```php
            function double(int $n): int
            ```"#]],
    )
    .await;
}

// ── 1.1 @inheritDoc resolution ───────────────────────────────────────────────

#[tokio::test]
async fn hover_keyword_false() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php $x = fal$0se;"#,
        expect![["`false` — boolean false"]],
    )
    .await;
}

#[tokio::test]
async fn hover_keyword_match() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php $x = mat$0ch($y) {};"#,
        expect![["`match` — evaluates an expression against a set of arms (PHP 8.0)"]],
    )
    .await;
}

#[tokio::test]
async fn hover_keyword_never() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php function fail(): nev$0er { throw new \Exception(); }"#,
        expect![["`never` — return type indicating the function always throws or exits (PHP 8.1)"]],
    )
    .await;
}

#[tokio::test]
async fn hover_keyword_null() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php $x = nu$0ll;"#,
        expect![["`null` — the null value; a variable has no value"]],
    )
    .await;
}

#[tokio::test]
async fn hover_keyword_readonly() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php class Foo { readon$0ly string $x; }"#,
        expect![["`readonly` — property or class that can only be initialised once (PHP 8.1)"]],
    )
    .await;
}

#[tokio::test]
async fn hover_keyword_true() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(r#"<?php $x = tr$0ue;"#, expect![["`true` — boolean true"]])
        .await;
}

#[tokio::test]
async fn hover_named_arg_builtin_function() {
    // PHP 8.0 named arg on a user-defined function matching a known param name.
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
function greet(string $name, int $count = 1): string { return $name; }
greet(coun$0t: 3);
"#,
        expect![[r#"
            ```php
            (parameter) int $count = 1
            ```"#]],
    )
    .await;
}

/// Named-arg hover where the receiver is `$this`. Resolves the enclosing class
/// via `enclosing_class_at` (no TypeMap `$this` fallback needed).
#[tokio::test]
async fn hover_named_arg_this_method_call() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Notifier {
    public function send(string $to, string $subject): bool { return true; }
    public function notify(): void {
        $this->send(subje$0ct: 'Hi', to: 'a@b.com');
    }
}
"#,
        expect![[r#"
            ```php
            (parameter) string $subject
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_named_arg_method_call() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Mailer {
    public function send(string $to, string $subject): bool { return true; }
}
$m = new Mailer();
$m->send(subje$0ct: 'Hello', to: 'a@b.com');
"#,
        expect![[r#"
            ```php
            (parameter) string $subject
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_named_arg_nested_call() {
    // Named arg inside a nested function call — cursor on inner call's arg.
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
function outer(string $a): string { return $a; }
function inner(int $x): int { return $x; }
outer(a: inner(x$0: 1));
"#,
        expect![[r#"
            ```php
            (parameter) int $x
            ```"#]],
    )
    .await;
}

// ── 2.3 Closure / arrow function hover ───────────────────────────────────────

#[tokio::test]
async fn hover_named_arg_static_method() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class DB {
    public static function query(string $sql, int $limit = 100): array { return []; }
}
DB::query(lim$0it: 10);
"#,
        expect![[r#"
            ```php
            (parameter) int $limit = 100
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_named_arg_with_docblock() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
/**
 * @param string $name The user's name.
 * @param int $age  The user's age.
 */
function register(string $name, int $age): void {}
register(na$0me: 'Alice', age: 30);
"#,
        expect![[r#"
            ```php
            (parameter) string $name
            ```

            ---

            The user's name."#]],
    )
    .await;
}

#[tokio::test]
async fn hover_named_function_keyword_not_intercepted() {
    // Hovering the `function` keyword in a named declaration (not a closure)
    // should not trigger the closure hover — returns nothing for the keyword itself.
    // Hover on the function *name* (not keyword) to get the signature.
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php fun$0ction greet(): void {}"#,
        expect!["<no hover>"],
    )
    .await;
}

#[tokio::test]
async fn hover_on_constructor_call() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Service {
    public function __construct(private string $dsn) {}
}
$svc = new Serv$0ice('db://localhost');
"#,
        expect![[r#"
            ```php
            class Service
            ```"#]],
    )
    .await;
}

/// Hovering on a property access with union type should show the union.
#[tokio::test]
async fn hover_readonly_class_shows_keyword() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
readonly class Poi$0nt { public function __construct(public float $x, public float $y) {} }
"#,
        expect![[r#"
            ```php
            readonly class Point
            ```"#]],
    )
    .await;
}

// ── Use-alias resolution ──────────────────────────────────────────────────────

/// Hovering on `Bar` where `use Foo as Bar` is in scope must show the `Foo`
/// class declaration.
#[tokio::test]
async fn hover_readonly_property_shows_modifier() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Point {
    public readonly float $x;
}
$p = new Point();
echo $p->$0x;
"#,
        expect![[r#"
            ```php
            (property) public readonly Point::$x: float
            ```"#]],
    )
    .await;
}

#[tokio::test]
async fn hover_real_docblock_not_overwritten_by_inheritdoc() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Base {
    /** Parent description. */
    public function run(): void {}
}
class Child extends Base {
    /** Child's own description. */
    public function run(): void {}
}
$c = new Child();
$c->ru$0n();
"#,
        expect![[r#"
            ```php
            Child::run(): void
            ```

            ---

            Child's own description."#]],
    )
    .await;
}

// ── 1.2 Keyword hover ────────────────────────────────────────────────────────

#[tokio::test]
async fn hover_second_method_call_on_same_line_picks_correct_receiver() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class A { public function handle(string $x): bool {} }
class B { public function handle(int $n): void {} }
$a = new A(); $b = new B();
$a->handle('x'); $b->hand$0le(1);
"#,
        // Must show B::handle (int $n), not A::handle (string $x).
        expect![[r#"
            ```php
            B::handle(int $n): void
            ```"#]],
    )
    .await;
}

// ── Trait inheritance correctness ─────────────────────────────────────────────

/// Two classes each define a method named `ping`; only `Server` uses the
/// `Pingable` trait that actually has the implementation.  Hovering on
/// `$server->ping()` must show `Server::ping`, not `Client::ping`.
#[tokio::test]
async fn hover_self_static_call_resolves_enclosing_class() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Builder {
    public static function create(): static { return new static(); }
    public function run(): void { self::crea$0te(); }
}
"#,
        expect![[r#"
            ```php
            Builder::create(): static
            ```"#]],
    )
    .await;
}

// ── Correct receiver on multi-call line ───────────────────────────────────────

/// Two different `->process()` calls on the same line — cursor on the second
/// one must pick the second receiver, not the first.
#[tokio::test]
async fn hover_static_call_resolves_correct_class() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    s.check_hover_annotated(
        r#"<?php
class Worker { public static function run(int $jobs): void {} }
class Scheduler { public static function run(string $cron): bool { return true; } }
Worker::ru$0n(4);
"#,
        expect![[r#"
            ```php
            Worker::run(int $jobs): void
            ```"#]],
    )
    .await;
}

/// `self::method()` at a call site resolves to the enclosing class.
#[tokio::test]
async fn hover_static_keyword_in_static_call_not_intercepted() {
    // `static::method()` — hovering `static` should NOT return the keyword doc,
    // it should fall through to the self/static class resolution.
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let v = s
        .check_hover(
            r#"<?php
class Base {
    public static function create(): static {}
    public static function build(): static {
        return stat$0ic::create();
    }
}
"#,
        )
        .await;
    // Should resolve to something about Base (static call), not the keyword doc.
    assert!(
        !v.contains("return type") && !v.contains("late static"),
        "static:: should not trigger keyword hover, got: {v}"
    );
}

// ── 2.4 PHP attribute hover ───────────────────────────────────────────────────