php-lsp 0.4.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
//! Call hierarchy + type hierarchy — all tests go through the LSP wire protocol.

mod common;

use common::TestServer;
use expect_test::expect;

// ── call hierarchy: prepare ────────────────────────────────────────────────────

#[tokio::test]
async fn prepare_function_returns_function_item() {
    let mut s = TestServer::new().await;
    let out = s
        .check_prepare_call_hierarchy(
            r#"<?php
function gree$0t(): void {}
"#,
        )
        .await;
    expect!["greet (Function) @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn prepare_class_method_returns_method_item() {
    let mut s = TestServer::new().await;
    let out = s
        .check_prepare_call_hierarchy(
            r#"<?php
class Mailer {
    public function sen$0d(): void {}
}
"#,
        )
        .await;
    expect!["send (Method) [Mailer] @ main.php:2"].assert_eq(&out);
}

#[tokio::test]
async fn prepare_trait_method_returns_method_item() {
    let mut s = TestServer::new().await;
    let out = s
        .check_prepare_call_hierarchy(
            r#"<?php
trait Timestampable {
    public function touc$0h(): void {}
}
"#,
        )
        .await;
    expect!["touch (Method) [Timestampable] @ main.php:2"].assert_eq(&out);
}

#[tokio::test]
async fn prepare_enum_method_returns_method_item() {
    let mut s = TestServer::new().await;
    let out = s
        .check_prepare_call_hierarchy(
            r#"<?php
enum Suit {
    case Hearts;
    public function lab$0el(): string { return 'x'; }
}
"#,
        )
        .await;
    expect!["label (Method) [Suit] @ main.php:3"].assert_eq(&out);
}

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

// ── call hierarchy: incoming ───────────────────────────────────────────────────

#[tokio::test]
async fn incoming_calls_lists_callers() {
    let mut s = TestServer::new().await;
    let out = s
        .check_incoming_calls(
            r#"<?php
function leaf$0(): void {}
function caller(): void { leaf(); }
"#,
        )
        .await;
    expect!["caller @ main.php:2"].assert_eq(&out);
}

#[tokio::test]
async fn incoming_calls_empty_when_never_called() {
    let mut s = TestServer::new().await;
    let out = s
        .check_incoming_calls(
            r#"<?php
function unuse$0d(): void {}
"#,
        )
        .await;
    expect!["<no calls>"].assert_eq(&out);
}

#[tokio::test]
async fn incoming_calls_multiple_callers() {
    let mut s = TestServer::new().await;
    let out = s
        .check_incoming_calls(
            r#"<?php
function tar$0get(): void {}
function a(): void { target(); }
function b(): void { target(); }
"#,
        )
        .await;
    expect!["a @ main.php:2\nb @ main.php:3"].assert_eq(&out);
}

#[tokio::test]
async fn incoming_calls_cross_file() {
    let mut s = TestServer::new().await;
    let out = s
        .check_incoming_calls(
            r#"//- /Service.php
<?php function proces$0s(): void {}
//- /Controller.php
<?php function handle(): void { process(); }
"#,
        )
        .await;
    expect!["handle @ Controller.php:0"].assert_eq(&out);
}

#[tokio::test]
async fn incoming_calls_from_file_scope() {
    let mut s = TestServer::new().await;
    let out = s
        .check_incoming_calls(
            r#"<?php
function boota$0ble(): void {}
bootable();
"#,
        )
        .await;
    expect!["<file scope> @ main.php:2"].assert_eq(&out);
}

// ── call hierarchy: outgoing ───────────────────────────────────────────────────

#[tokio::test]
async fn outgoing_calls_lists_callees() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function leaf(): void {}
function caller$0(): void { leaf(); }
"#,
        )
        .await;
    expect!["leaf @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_empty_for_leaf_function() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function noo$0p(): void { $x = 1; }
"#,
        )
        .await;
    expect!["<no calls>"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_cross_file_callee() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"//- /main.php
<?php function orchest$0rate(): void { helper(); }
//- /helpers.php
<?php function helper(): void {}
"#,
        )
        .await;
    expect!["helper @ helpers.php:0"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_deduplicates_repeated_callee() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function helper(): void {}
function caller$0(): void { helper(); helper(); }
"#,
        )
        .await;
    expect!["helper @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_from_class_method() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function validate(): bool { return true; }
class Order {
    public function subm$0it(): void { validate(); }
}
"#,
        )
        .await;
    expect!["validate @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_from_enum_method() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function fmt(): string { return ''; }
enum Suit {
    public function lab$0el(): string { return fmt(); }
}
"#,
        )
        .await;
    expect!["fmt @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_includes_for_init_and_update() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function start(): int { return 0; }
function step(): void {}
function mai$0n(): void { for ($i = start(); $i < 10; step()) {} }
"#,
        )
        .await;
    expect!["start @ main.php:1\nstep @ main.php:2"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_includes_static_method_call() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
class Cache {
    public static function warm(): void {}
}
function bootstra$0p(): void { Cache::warm(); }
"#,
        )
        .await;
    expect!["warm @ main.php:2"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_inside_do_while() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function tick(): bool { return true; }
function pol$0l(): void { do {} while (tick()); }
"#,
        )
        .await;
    expect!["tick @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_inside_switch() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function action(): void {}
function dispa$0tch(int $x): void {
    switch ($x) {
        case 1: action(); break;
    }
}
"#,
        )
        .await;
    expect!["action @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_includes_args_of_new_expr() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function defaults(): array { return []; }
class Config {}
function boo$0t(): void { $c = new Config(defaults()); }
"#,
        )
        .await;
    expect!["defaults @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn outgoing_calls_inside_cast() {
    let mut s = TestServer::new().await;
    let out = s
        .check_outgoing_calls(
            r#"<?php
function measure(): float { return 1.5; }
function conv$0ert(): int { return (int) measure(); }
"#,
        )
        .await;
    expect!["measure @ main.php:1"].assert_eq(&out);
}

// ── type hierarchy: prepare ───────────────────────────────────────────────────

#[tokio::test]
async fn prepare_class_returns_class_item() {
    let mut s = TestServer::new().await;
    let out = s
        .check_prepare_type_hierarchy(
            r#"<?php
class My$0Class {}
"#,
        )
        .await;
    expect!["MyClass (Class) @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn prepare_interface_returns_interface_item() {
    let mut s = TestServer::new().await;
    let out = s
        .check_prepare_type_hierarchy(
            r#"<?php
interface Conta$0inable {}
"#,
        )
        .await;
    expect!["Containable (Interface) @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn prepare_enum_returns_enum_item() {
    let mut s = TestServer::new().await;
    let out = s
        .check_prepare_type_hierarchy(
            r#"<?php
enum Suit$0 { case Hearts; }
"#,
        )
        .await;
    expect!["Suit (Enum) @ main.php:1"].assert_eq(&out);
}

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

// ── type hierarchy: supertypes ────────────────────────────────────────────────

#[tokio::test]
async fn supertypes_class_extends_parent() {
    let mut s = TestServer::new().await;
    let out = s
        .check_supertypes(
            r#"<?php
class Animal {}
class D$0og extends Animal {}
"#,
        )
        .await;
    expect!["Animal (Class) @ main.php:1"].assert_eq(&out);
}

#[tokio::test]
async fn supertypes_implements_multiple_interfaces() {
    let mut s = TestServer::new().await;
    let out = s
        .check_supertypes(
            r#"//- /Circle.php
<?php class Circle$0 implements Drawable, Serializable {}
//- /Drawable.php
<?php interface Drawable {}
//- /Serializable.php
<?php interface Serializable {}
"#,
        )
        .await;
    expect!["Drawable (Interface) @ Drawable.php:0\nSerializable (Interface) @ Serializable.php:0"]
        .assert_eq(&out);
}

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

#[tokio::test]
async fn supertypes_multi_level_returns_direct_parent_only() {
    let mut s = TestServer::new().await;
    let out = s
        .check_supertypes(
            r#"//- /A.php
<?php class A {}
//- /B.php
<?php class B extends A {}
//- /C.php
<?php class C$0 extends B {}
"#,
        )
        .await;
    expect!["B (Class) @ B.php:0"].assert_eq(&out);
}

// ── type hierarchy: subtypes ──────────────────────────────────────────────────

#[tokio::test]
async fn subtypes_interface_returns_implementing_classes() {
    let mut s = TestServer::new().await;
    let out = s
        .check_subtypes(
            r#"//- /Loggable.php
<?php interface Loggable$0 {}
//- /Service.php
<?php class Service implements Loggable {}
"#,
        )
        .await;
    expect!["Service (Class) @ Service.php:0"].assert_eq(&out);
}

#[tokio::test]
async fn subtypes_class_returns_extending_subclasses() {
    let mut s = TestServer::new().await;
    let out = s
        .check_subtypes(
            r#"//- /Base.php
<?php class Base$0 {}
//- /ChildA.php
<?php class ChildA extends Base {}
//- /ChildB.php
<?php class ChildB extends Base {}
"#,
        )
        .await;
    expect!["ChildA (Class) @ ChildA.php:0\nChildB (Class) @ ChildB.php:0"].assert_eq(&out);
}

#[tokio::test]
async fn subtypes_leaf_class_returns_empty() {
    let mut s = TestServer::new().await;
    let out = s
        .check_subtypes(
            r#"<?php
class Leaf$0 extends Base {}
"#,
        )
        .await;
    expect!["<empty>"].assert_eq(&out);
}

#[tokio::test]
async fn subtypes_abstract_class_returns_concrete_impl() {
    let mut s = TestServer::new().await;
    let out = s
        .check_subtypes(
            r#"//- /AbstractRepo.php
<?php abstract class AbstractRepo$0 {}
//- /UserRepo.php
<?php class UserRepo extends AbstractRepo {}
"#,
        )
        .await;
    expect!["UserRepo (Class) @ UserRepo.php:0"].assert_eq(&out);
}

#[tokio::test]
async fn subtypes_trait_returns_using_classes() {
    let mut s = TestServer::new().await;
    let out = s
        .check_subtypes(
            r#"//- /Timestamps.php
<?php trait Timestamps$0 {}
//- /Post.php
<?php class Post { use Timestamps; }
"#,
        )
        .await;
    expect!["Post (Class) @ Post.php:0"].assert_eq(&out);
}