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
use super::*;
use expect_test::expect;

/// Member lines (methods/properties/constants) of a completion rendering.
fn members(out: &str) -> String {
    out.lines()
        .filter(|l| {
            l.starts_with("Method ") || l.starts_with("Property ") || l.starts_with("Constant ")
        })
        .collect::<Vec<_>>()
        .join("\n")
}

#[tokio::test]
async fn interface_receiver_completes_its_own_methods() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
interface Shape { public function area(): float; }
class Other { public function unrelated(): void {} }
function f(Shape $x) { $x->$0 }
"#,
        )
        .await;
    expect!["Method      area"].assert_eq(&members(&out));
}

#[tokio::test]
async fn interface_receiver_includes_parent_interface_methods() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
namespace App;
interface Named { public function name(): string; }
interface Shape extends Named { public function area(): float; }
function f(Shape $x) { $x->$0 }
"#,
        )
        .await;
    expect![[r#"
        Method      area
        Method      name"#]]
    .assert_eq(&members(&out));
}

#[tokio::test]
async fn inherited_private_members_are_not_offered() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
class Base {
    private $hidden;
    protected $guarded;
    private function secret(): void {}
    protected function prot(): void {}
    public function pub(): void {}
}
class Child extends Base {}
$c = new Child();
$c->$0
"#,
        )
        .await;
    expect!["Method      pub"].assert_eq(&members(&out));
}

#[tokio::test]
async fn inherited_protected_members_are_offered_inside_a_subclass() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
class Base {
    private function secret(): void {}
    protected function prot(): void {}
    public function pub(): void {}
}
class Child extends Base {
    public function run(): void { $this->$0 }
}
"#,
        )
        .await;
    expect![[r#"
        Method      prot
        Method      pub
        Method      run"#]]
    .assert_eq(&members(&out));
}

#[tokio::test]
async fn parent_resolves_against_the_declaring_files_imports() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"//- /a/Base.php
<?php
namespace A;
class Base { public function fromA(): void {} }

//- /b/Base.php
<?php
namespace B;
class Base { public function fromB(): void {} }

//- /a/Mid.php
<?php
namespace A;
class Mid extends Base {}

//- /main.php
<?php
use B\Base;
use A\Mid;
function f(Mid $m) { $m->$0 }
"#,
        )
        .await;
    expect!["Method      fromA"].assert_eq(&members(&out));
}

#[tokio::test]
async fn namespaced_enum_gets_match_arm_completion() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
namespace App;
enum Suit { case Hearts; case Spades; }
function f(Suit $s) {
    match ($s) {
        $0
    }
}
"#,
        )
        .await;
    let arms: Vec<&str> = out
        .lines()
        .filter(|l| l.starts_with("Constant    Suit::"))
        .collect();
    expect![[r#"
        Constant    Suit::Hearts
        Constant    Suit::Spades"#]]
    .assert_eq(&arms.join("\n"));
}

#[tokio::test]
async fn signature_help_scopes_method_to_receiver_class() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_signature_help(
            r#"<?php
class Logger { public function send(string $line): void {} }
class Mailer { public function send(string $to, string $body): void {} }
$mailer = new Mailer();
$mailer->send($0);
"#,
        )
        .await;
    expect!["â–¶ send(string $to, string $body)  @param0"].assert_eq(&out);
}

#[tokio::test]
async fn namespaced_receiver_gets_named_argument_completion() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
namespace App;
class Mailer { public function send(string $to, string $body): void {} }
function f(Mailer $m) { $m->send($0); }
"#,
        )
        .await;
    let named: Vec<&str> = out
        .lines()
        .filter(|l| l.contains("to:") || l.contains("body:"))
        .collect();
    expect!["Method      send(to:, body:) | named args"].assert_eq(&named.join("\n"));
}

#[tokio::test]
async fn imported_static_receiver_gets_named_argument_completion() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"//- /Mailer.php
<?php
namespace Lib;
class Mailer { public static function send(string $to, string $body): void {} }

//- /main.php
<?php
use Lib\Mailer;
Mailer::send($0);
"#,
        )
        .await;
    let named: Vec<&str> = out
        .lines()
        .filter(|l| l.contains("to:") || l.contains("body:"))
        .collect();
    expect!["Method      send(to:, body:) | named args"].assert_eq(&named.join("\n"));
}

#[tokio::test]
async fn docblock_property_and_method_tags_are_offered() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
/**
 * @property string $email
 * @property-read int $id
 * @method int total()
 * @method static self make()
 */
class User {}
function f(User $u) { $u->$0 }
"#,
        )
        .await;
    expect![[r#"
        Property    $email
        Property    $id
        Method      total"#]]
    .assert_eq(&members(&out));
}

#[tokio::test]
async fn mixin_public_members_are_offered() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
class Builder {
    public function where(): static { return $this; }
    protected function guarded(): void {}
    public static function query(): void {}
}
/** @mixin Builder */
class Model { public function save(): void {} }
function f(Model $m) { $m->$0 }
"#,
        )
        .await;
    expect![[r#"
        Method      save
        Method      where"#]]
    .assert_eq(&members(&out));
}

#[tokio::test]
async fn private_trait_members_are_offered_inside_the_using_class() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
trait Helpers {
    private function helper(): void {}
}
class Base {
    use Helpers;
}
class Child extends Base {
    use Helpers2;
    public function run(): void { $this->$0 }
}
trait Helpers2 {
    private function own(): void {}
}
"#,
        )
        .await;
    expect![[r#"
        Method      own
        Method      run"#]]
    .assert_eq(&members(&out));
}

#[tokio::test]
async fn interface_constants_are_offered_on_implementing_class() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
namespace App;
interface HasVersion { const VERSION = 1; }
class Release implements HasVersion { const NAME = 'x'; }
Release::$0
"#,
        )
        .await;
    expect![[r#"
        Constant    NAME
        Constant    VERSION"#]]
    .assert_eq(&members(&out));
}

#[tokio::test]
async fn backed_enum_static_access_offers_cases_and_builtins() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = s
        .check_completion_ordered(
            r#"<?php
namespace App;
enum Suit: string {
    case Hearts = 'h';
    const DEFAULT = self::Hearts;
    public static function random(): self { return self::Hearts; }
}
Suit::$0
"#,
        )
        .await;
    expect![[r#"
        Constant    DEFAULT
        Constant    Hearts
        Method      cases
        Method      from
        Method      random
        Method      tryFrom"#]]
    .assert_eq(&members(&out));
}

/// Unopened PSR-4 vendor classes: traits and the grandparent of a vendor
/// parent must all contribute members.
#[tokio::test]
async fn vendor_parent_traits_and_grandparent_members_are_offered() {
    let tmp = tempfile::tempdir().unwrap();
    let src = tmp.path().join("vendor/acme/console/src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(
        tmp.path().join("composer.json"),
        r#"{"autoload":{"psr-4":{"Acme\\Console\\":"vendor/acme/console/src/"}}}"#,
    )
    .unwrap();
    std::fs::write(
        src.join("BaseCommand.php"),
        "<?php\nnamespace Acme\\Console;\nclass BaseCommand { public function run(): int { return 0; } }\n",
    )
    .unwrap();
    std::fs::write(
        src.join("InteractsWithIO.php"),
        "<?php\nnamespace Acme\\Console;\ntrait InteractsWithIO { public function line(): void {} }\n",
    )
    .unwrap();
    std::fs::write(
        src.join("CallsCommands.php"),
        "<?php\nnamespace Acme\\Console;\ntrait CallsCommands { protected function call(): void {} }\n",
    )
    .unwrap();
    std::fs::write(
        src.join("Command.php"),
        "<?php\nnamespace Acme\\Console;\nclass Command extends BaseCommand {\n    use InteractsWithIO, CallsCommands;\n    private function secret(): void {}\n}\n",
    )
    .unwrap();
    let caller = "<?php\nnamespace App;\nuse Acme\\Console\\Command;\nclass Sync extends Command {\n    public function handle(): void {\n        $this->\n    }\n}\n";
    std::fs::write(tmp.path().join("Sync.php"), caller).unwrap();

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

    let (_, line, ch) = s.locate("Sync.php", "$this->", 0);
    let resp = s
        .completion("Sync.php", line, ch + "$this->".len() as u32)
        .await;
    let out = render_completion_ordered(&resp);
    expect![[r#"
        Method      call
        Method      handle
        Method      line
        Method      run"#]]
    .assert_eq(&members(&out));
}

/// Completion at `$0` triggered by `(`, the only trigger that offers named arguments.
async fn paren_completion(s: &mut TestServer, src: &str) -> String {
    let opened = s.open_fixture(src).await;
    let c = opened.cursor().clone();
    let uri = s.uri(&c.path);
    let resp = s
        .client()
        .request(
            "textDocument/completion",
            serde_json::json!({
                "textDocument": { "uri": uri },
                "position": { "line": c.line, "character": c.character },
                "context": { "triggerKind": 2, "triggerCharacter": "(" },
            }),
        )
        .await;
    render_completion(&resp)
}

#[tokio::test]
async fn inherited_method_gets_named_argument_completion() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = paren_completion(
        &mut s,
        r#"<?php
namespace App;
class Base { public function send(string $to, string $body): void {} }
class Mailer extends Base {}
function f(Mailer $m) { $m->send($0); }
"#,
    )
    .await;
    expect![[r#"
        Variable    body:
        Variable    to:"#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn trait_and_mixin_methods_get_named_argument_completion() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = paren_completion(
        &mut s,
        r#"<?php
class Builder { public function where(string $column, mixed $value): static { return $this; } }
trait Logs { public function log(string $message): void {} }
/** @mixin Builder */
class Model { use Logs; }
function f(Model $m) { $m->where($0); }
"#,
    )
    .await;
    expect![[r#"
        Variable    column:
        Variable    value:"#]]
    .assert_eq(&out);

    let out = paren_completion(
        &mut s,
        r#"<?php
class Builder { public function where(string $column, mixed $value): static { return $this; } }
trait Logs { public function log(string $message): void {} }
/** @mixin Builder */
class Model { use Logs; }
function f(Model $m) { $m->log($0); }
"#,
    )
    .await;
    expect!["Variable    message:"].assert_eq(&out);
}

#[tokio::test]
async fn inherited_static_method_gets_named_argument_completion() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let out = paren_completion(
        &mut s,
        r#"<?php
namespace App;
class Factory { public static function make(string $name, int $size): static { return new static(); } }
class Widget extends Factory {}
Widget::make($0);
"#,
    )
    .await;
    expect![[r#"
        Variable    name:
        Variable    size:"#]]
    .assert_eq(&out);
}

/// Attribute classes usually live in unopened vendor files.
#[tokio::test]
async fn unopened_vendor_attribute_gets_constructor_named_arguments() {
    let tmp = tempfile::tempdir().unwrap();
    let src = tmp.path().join("vendor/acme/routing/src");
    std::fs::create_dir_all(&src).unwrap();
    std::fs::write(
        tmp.path().join("composer.json"),
        r#"{"autoload":{"psr-4":{"Acme\\Routing\\":"vendor/acme/routing/src/"}}}"#,
    )
    .unwrap();
    std::fs::write(
        src.join("Route.php"),
        "<?php\nnamespace Acme\\Routing;\n#[\\Attribute]\nclass Route {\n    public function __construct(string $path, array $methods = []) {}\n}\n",
    )
    .unwrap();
    let caller = "<?php\nnamespace App;\nuse Acme\\Routing\\Route;\nclass Home {\n    #[Route(\n    public function index(): void {}\n}\n";
    std::fs::write(tmp.path().join("Home.php"), caller).unwrap();

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

    let (_, line, ch) = s.locate("Home.php", "#[Route(", 0);
    let uri = s.uri("Home.php");
    let resp = s
        .client()
        .request(
            "textDocument/completion",
            serde_json::json!({
                "textDocument": { "uri": uri },
                "position": { "line": line, "character": ch + "#[Route(".len() as u32 },
                "context": { "triggerKind": 2, "triggerCharacter": "(" },
            }),
        )
        .await;
    expect![[r#"
        Variable    methods: | #Route argument
        Variable    path: | #Route argument"#]]
    .assert_eq(&render_completion_ordered(&resp));
}