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
//! Resolve provider tests for all LSP item types.
//! Tests verify that lazy-loaded item data (detail, documentation, edit computation)
//! is correctly resolved when requested via the LSP resolve protocol.

use super::*;
use expect_test::expect;
use serde_json::json;

// ============================================================================
// completionItem/resolve tests
// ============================================================================

#[tokio::test]
async fn completion_resolve_adds_documentation_to_function() {
    let mut s = TestServer::new().await;
    s.open("file.php", "<?php\narray_map$0").await;

    let resp = s.completion("file.php", 1, 9).await;
    let items: Vec<_> = resp["result"]
        .as_array()
        .or_else(|| resp["result"]["items"].as_array())
        .map(|a| a.to_vec())
        .unwrap_or_default();

    let item = items.iter().find(|i| {
        i["label"]
            .as_str()
            .map(|l| l == "array_map")
            .unwrap_or(false)
    });
    assert!(item.is_some(), "array_map not found");

    let resolved = s.completion_resolve(item.unwrap().clone()).await;
    let out = render_resolved_completion_item(&resolved);
    expect![[r#"
array_map (Function)
detail: <no detail>
docs: ```php
function array_map()
```

[php.net documentation](https://www.php.net/function.array-map)"#]]
    .assert_eq(&out);
}

#[tokio::test]
async fn completion_resolve_idempotent_when_resolved() {
    let mut s = TestServer::new().await;
    s.open("file.php", "<?php\nstrlen").await;

    let resp = s.completion("file.php", 1, 6).await;
    let items: Vec<_> = resp["result"]
        .as_array()
        .or_else(|| resp["result"]["items"].as_array())
        .map(|a| a.to_vec())
        .unwrap_or_default();

    assert!(
        !items.is_empty(),
        "completions must be available at position 1:6"
    );
    let item = items[0].clone();
    let resolved1 = s.completion_resolve(item).await;
    let resolved2 = s.completion_resolve(resolved1["result"].clone()).await;

    let out1 = render_resolved_completion_item(&resolved1);
    let out2 = render_resolved_completion_item(&resolved2);
    assert_eq!(out1, out2, "resolve should be idempotent");
    expect![[r#"
        strlen (Function)
        detail: <no detail>
        docs: ```php
        function strlen()
        ```

        [php.net documentation](https://www.php.net/function.strlen)"#]]
    .assert_eq(&out1);
}

/// Regression test for a confirmed bug: two unrelated classes declaring a
/// same-named method both live in the workspace index; completion_resolve
/// looked up detail/documentation by bare method name across the whole
/// workspace with no notion of which class the completion item actually
/// belonged to, so it could return the wrong class's docblock/signature.
#[tokio::test]
async fn completion_resolve_scopes_to_owning_class_on_name_collision() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Wanted.php"),
        "<?php\nnamespace App\\Wanted;\nclass Target {\n    /** The one we want. */\n    public static function save(): string { return 'w'; }\n}\n",
    )
    .unwrap();
    std::fs::write(
        tmp.path().join("Other.php"),
        "<?php\nnamespace App\\Other;\nclass Target {\n    /** Not this one. */\n    public static function save(int $x): void {}\n}\n",
    )
    .unwrap();
    let caller = "<?php\nuse App\\Wanted\\Target;\n\nfunction f() {\n    Target::save();\n}\n";
    std::fs::write(tmp.path().join("caller.php"), caller).unwrap();

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

    let (_, line, ch) = s.locate("caller.php", "save();", 0);
    let resp = s.completion("caller.php", line, ch).await;
    let items: Vec<_> = resp["result"]
        .as_array()
        .or_else(|| resp["result"]["items"].as_array())
        .map(|a| a.to_vec())
        .unwrap_or_default();
    let item = items
        .iter()
        .find(|i| i["label"].as_str() == Some("save"))
        .expect("save not in completions")
        .clone();

    let resolved = s.completion_resolve(item).await;
    let out = render_resolved_completion_item(&resolved);
    assert!(
        out.contains("The one we want."),
        "resolve must use the imported Target::save's own docblock, got: {out}"
    );
    assert!(
        !out.contains("Not this one."),
        "resolve must not pick up the unrelated same-named method's docblock, got: {out}"
    );
}

#[tokio::test]
async fn completion_resolve_fills_open_doc_namespaced_function() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);
    let src = "<?php\nnamespace App\\Util;\n/** Says hello. */\nfunction greetUser(string $name): string { return $name; }\ngreetU";
    s.open("file.php", src).await;
    let resp = s.completion("file.php", 4, 6).await;
    let items: Vec<_> = resp["result"]
        .as_array()
        .or_else(|| resp["result"]["items"].as_array())
        .map(|a| a.to_vec())
        .unwrap_or_default();
    let item = items
        .iter()
        .find(|i| i["label"].as_str() == Some("greetUser"))
        .expect("greetUser not in completions")
        .clone();
    assert_eq!(item["data"]["fqn"], "App\\Util\\greetUser");

    let resolved = s.completion_resolve(item).await;
    let out = render_resolved_completion_item(&resolved);
    assert!(
        out.contains("detail: function greetUser(string $name): string"),
        "got: {out}"
    );
    assert!(out.contains("Says hello."), "got: {out}");
}

// ============================================================================
// codeAction/resolve tests
// ============================================================================

#[tokio::test]
async fn code_action_resolve_defers_extract_method_edit() {
    let mut s = TestServer::new().await;
    let src = r#"<?php
class Math {
    public function add(): int {
        return $01 + 2$0;
    }
}
"#;
    let opened = s.open_fixture(src).await;
    let resp = if let Some(r) = opened.fixture.range.clone() {
        s.code_action_at(&r).await
    } else {
        let c = opened.cursor().clone();
        s.code_action(&c.path, c.line, c.character, c.line, c.character)
            .await
    };
    let actions: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    let extract_action = actions.iter().find(|a| {
        a["title"]
            .as_str()
            .map(|t| t.contains("Extract method"))
            .unwrap_or(false)
    });

    if let Some(action) = extract_action {
        // CRITICAL: Verify deferred behavior - action must have data, NO edit before resolve
        assert!(
            action["edit"].is_null(),
            "deferred action should NOT have edit before resolve"
        );
        assert!(
            action["data"].is_object(),
            "deferred action must have data with resolve metadata"
        );
        assert_eq!(
            action["data"]["php_lsp_resolve"].as_str(),
            Some("extract_method"),
            "data must have php_lsp_resolve tag"
        );

        let resolved = s.code_action_resolve(action.clone()).await;
        let out = render_resolved_code_action(&resolved, &s.uri(""));

        // Verify edit was computed during resolve
        expect![[r#"
Extract method (RefactorExtract)
edit: 1 file(s) modified"#]]
        .assert_eq(&out);
    }
}

#[tokio::test]
async fn code_action_resolve_handles_non_deferred_actions() {
    let mut s = TestServer::new().await;
    let src = r#"<?php
function $0greet() {
    echo "Hello";
}"#;

    let opened = s.open_fixture(src).await;
    let resp = if let Some(r) = opened.fixture.range.clone() {
        s.code_action_at(&r).await
    } else {
        let c = opened.cursor().clone();
        s.code_action(&c.path, c.line, c.character, c.line, c.character)
            .await
    };
    let actions: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    let mut snapshots: Vec<String> = Vec::new();
    for action in actions {
        let resolved = s.code_action_resolve(action.clone()).await;
        assert!(
            resolved["error"].is_null(),
            "all actions should resolve successfully"
        );
        snapshots.push(render_resolved_code_action(&resolved, &s.uri("")));
    }
    expect![[r#"
        Generate PHPDoc (refactor)
        edit: 1 file(s) modified
        ---
        Add return type `: void` (refactor)
        edit: 1 file(s) modified"#]]
    .assert_eq(&snapshots.join("\n---\n"));
}

// ============================================================================
// codeLens/resolve tests
// ============================================================================

#[tokio::test]
async fn code_lens_resolve_returns_populated_lens() {
    let mut s = TestServer::new().await;
    let src = r#"<?php
class TestCase {
    public function testExample(): void {}

    public function runIt(): void {
        $this->testExample();
    }
}"#;

    s.open("test.php", src).await;

    let resp = s.code_lens("test.php").await;
    let lenses: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    let mut results = Vec::new();
    for lens in lenses {
        let resolved = s.code_lens_resolve(lens.clone()).await;
        results.push(render_resolved_code_lens(&resolved));
    }

    expect![[r#"L1:6 0 references [editor.action.showReferences]
L2:20 1 reference [editor.action.showReferences]
L2:20 â–¶ Run test [php-lsp.runTest]
L4:20 0 references [editor.action.showReferences]"#]]
    .assert_eq(&results.join("\n"));
}

#[tokio::test]
async fn code_lens_resolve_preserves_position() {
    let mut s = TestServer::new().await;
    let src = r#"<?php
class Service {
    public function execute(): void {}
}
"#;

    s.open("service.php", src).await;

    let resp = s.code_lens("service.php").await;
    let lenses: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    let mut results = Vec::new();
    for lens in lenses {
        let range_before = lens["range"].clone();
        let resolved = s.code_lens_resolve(lens.clone()).await;
        let range_after = resolved["result"]["range"].clone();

        assert_eq!(range_before, range_after, "resolve should not modify range");
        results.push(render_resolved_code_lens(&resolved));
    }
    expect![[r#"
        L1:6 0 references [editor.action.showReferences]
        L2:20 0 references [editor.action.showReferences]"#]]
    .assert_eq(&results.join("\n"));
}

// ============================================================================
// documentLink/resolve tests
// ============================================================================

#[tokio::test]
async fn document_link_resolve_returns_target() {
    let mut s = TestServer::new().await;
    s.open(
        "links.php",
        "<?php\nrequire_once 'vendor/autoload.php';\nrequire 'lib/config.php';\n",
    )
    .await;

    let resp = s.document_link("links.php").await;
    let links: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    let mut results = Vec::new();
    for link in links {
        let resolved = s.document_link_resolve(link.clone()).await;
        results.push(render_resolved_document_link(&resolved, &s.uri("")));
    }

    expect![[r#"L1:14 -> vendor/autoload.php
L2:9 -> lib/config.php"#]]
    .assert_eq(&results.join("\n"));
}

#[tokio::test]
async fn document_link_resolve_handles_http_links() {
    let mut s = TestServer::new().await;
    s.open(
        "doc.php",
        "<?php\n/** @link https://php.net/manual */\nfunction helper() {}\n",
    )
    .await;

    let resp = s.document_link("doc.php").await;
    let links: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    for link in links {
        let resolved = s.document_link_resolve(link.clone()).await;
        let out = render_resolved_document_link(&resolved, &s.uri(""));

        expect![[r#"L1:10 -> https://php.net/manual"#]].assert_eq(&out);
    }
}

// ============================================================================
// inlayHint/resolve tests
// ============================================================================

#[tokio::test]
async fn inlay_hint_resolve_adds_tooltip() {
    let mut s = TestServer::new().await;
    let src = r#"<?php
function process(string $name, int $age): void {}
$0process("Alice", 30);$0
"#;
    let opened = s.open_fixture(src).await;
    let path = opened.fixture.files[0].path.clone();

    let resp = if let Some(r) = opened.fixture.range.clone() {
        s.inlay_hints(
            &r.path,
            r.start_line,
            r.start_character,
            r.end_line,
            r.end_character,
        )
        .await
    } else {
        s.inlay_hints(&path, 0, 0, u32::MAX, u32::MAX).await
    };
    let hints: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    let mut snapshots: Vec<String> = Vec::new();
    for hint in hints {
        let resolved = s.inlay_hint_resolve(hint.clone()).await;
        assert!(
            resolved["error"].is_null(),
            "hint should resolve without error"
        );
        snapshots.push(render_resolved_inlay_hint(&resolved));
    }
    expect![[r#"
        2:8 name:
        tooltip: ```php
        function process(string $name, int $age): void
        ```
        ---
        2:17 age:
        tooltip: ```php
        function process(string $name, int $age): void
        ```"#]]
    .assert_eq(&snapshots.join("\n---\n"));
}

#[tokio::test]
async fn inlay_hint_resolve_idempotent() {
    let mut s = TestServer::new().await;
    let src = r#"<?php
function getName(string $first, string $last): string { return $first; }
$0getName("Ada", "Lovelace");$0
"#;
    let opened = s.open_fixture(src).await;
    let path = opened.fixture.files[0].path.clone();

    let resp = if let Some(r) = opened.fixture.range.clone() {
        s.inlay_hints(
            &r.path,
            r.start_line,
            r.start_character,
            r.end_line,
            r.end_character,
        )
        .await
    } else {
        s.inlay_hints(&path, 0, 0, u32::MAX, u32::MAX).await
    };
    let hints: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    let mut snapshots = Vec::new();
    for hint in hints {
        let resolved1 = s.inlay_hint_resolve(hint.clone()).await;
        let resolved2 = s.inlay_hint_resolve(resolved1["result"].clone()).await;

        // Resolving twice should be idempotent
        let out1 = render_resolved_inlay_hint(&resolved1);
        let out2 = render_resolved_inlay_hint(&resolved2);
        assert_eq!(out1, out2, "resolve should be idempotent");
        snapshots.push(out1);
    }
    expect![[r#"
        2:8 first:
        tooltip: ```php
        function getName(string $first, string $last): string
        ```
        ---
        2:15 last:
        tooltip: ```php
        function getName(string $first, string $last): string
        ```"#]]
    .assert_eq(&snapshots.join("\n---\n"));
}

// ============================================================================
// workspaceSymbol/resolve tests
// ============================================================================

#[tokio::test]
async fn workspace_symbol_resolve_populates_location() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);

    // Open fixture and search for Database class
    let _ = s
        .open_fixture(
            r#"<?php
class Database {}
"#,
        )
        .await;

    let resp = s.workspace_symbols("Database").await;
    let symbols: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();
    assert!(!symbols.is_empty(), "Database symbol not found");

    let symbol = symbols[0].clone();
    let resolved = s.workspace_symbol_resolve(symbol).await;
    let out = render_resolved_workspace_symbol(&resolved, &s.uri(""));
    expect!["Database (Class) @ main.php:1:6-1:14"].assert_eq(&out);
}

#[tokio::test]
async fn workspace_symbol_resolve_multiple_symbols() {
    let mut s = TestServer::new().await;
    s.validate_syntax(false);

    let _ = s
        .open_fixture(
            r#"<?php
function test() {}
function testing() {}
function tested() {}
"#,
        )
        .await;

    let resp = s.workspace_symbols("test").await;
    let symbols: Vec<_> = resp["result"]
        .as_array()
        .map(|a| a.to_vec())
        .unwrap_or_default();

    assert!(!symbols.is_empty(), "should find symbols matching 'test'");

    let mut results = Vec::new();
    for symbol in symbols.iter().take(5) {
        let resolved = s.workspace_symbol_resolve(symbol.clone()).await;
        assert_eq!(
            symbol["name"], resolved["result"]["name"],
            "resolve should preserve name"
        );
        assert_eq!(
            symbol["kind"], resolved["result"]["kind"],
            "resolve should preserve kind"
        );
        results.push(render_resolved_workspace_symbol(&resolved, &s.uri("")));
    }
    expect![[r#"
        test (Function) @ main.php:1:9-1:13
        testing (Function) @ main.php:2:9-2:16
        tested (Function) @ main.php:3:9-3:15"#]]
    .assert_eq(&results.join("\n"));
}

// ============================================================================
// Error handling tests
// ============================================================================

#[tokio::test]
async fn resolve_handles_empty_items_gracefully() {
    let mut s = TestServer::new().await;

    let empty_item = json!({});

    let resolved = s.completion_resolve(empty_item).await;
    let out = render_resolved_completion_item(&resolved);

    expect![[r#"error: {"code":-32602,"message":"missing field `label`"}"#]].assert_eq(&out);
}