php-lsp 0.13.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
//! Document lifecycle: didClose, didSave, willSave, willSaveWaitUntil,
//! didChange, and basic endpoint wiring (documentLink, inlineValue).

use super::*;

use expect_test::expect;
use serde_json::Value;

use crate::common::render_text_edits;

// --- did_close ---

#[tokio::test]
async fn did_close_clears_diagnostics() {
    let mut server = TestServer::new().await;
    let uri = server.uri("close_test.php");

    let open_notif = server.open("close_test.php", "<?php function() {}\n").await;
    let open_rendered = render_diagnostics_notification(&open_notif);
    expect![[r#"
        0:6-0:19 [3] MissingClosureReturnType: Closure has no return type annotation
        1:0-1:1 [1] ?: expected ';' after expression"#]]
    .assert_eq(&open_rendered);

    server.close("close_test.php").await;
    let close_notif = server.client().wait_for_diagnostics(&uri).await;
    expect!["<empty>"].assert_eq(&render_diagnostics_notification(&close_notif));
}

#[tokio::test]
async fn did_close_unopened_does_not_crash() {
    let mut server = TestServer::new().await;
    let uri = server.uri("never_opened.php");

    server.close("never_opened.php").await;
    let notif = server.client().wait_for_diagnostics(&uri).await;
    expect!["<empty>"].assert_eq(&render_diagnostics_notification(&notif));
}

// --- did_save ---

#[tokio::test]
async fn did_save_republishes_empty_diagnostics_for_clean_file() {
    let mut server = TestServer::new().await;
    server.open("save_clean.php", "<?php\n").await;

    let save_notif = server.save("save_clean.php").await;
    expect!["<empty>"].assert_eq(&render_diagnostics_notification(&save_notif));
}

#[tokio::test]
async fn did_save_republishes_diagnostics_for_duplicate_functions() {
    let mut server = TestServer::new().await;
    let open_notif = server
        .open(
            "save_dup.php",
            "<?php\nfunction doWork() {}\nfunction doWork() {}\n",
        )
        .await;
    expect!["2:0-2:20 [1] DuplicateFunction: Function doWork() has already been defined"]
        .assert_eq(&render_diagnostics_notification(&open_notif));

    let save_notif = server.save("save_dup.php").await;
    expect!["2:0-2:20 [1] DuplicateFunction: Function doWork() has already been defined"]
        .assert_eq(&render_diagnostics_notification(&save_notif));
}

#[tokio::test]
async fn did_save_republishes_semantic_diagnostics() {
    // Regression: did_save was manually building parse+dup-decl diagnostics
    // and omitting the semantic pass. publishDiagnostics *replaces* the prior
    // set, so saving a file with semantic errors would silently clear them.
    let mut server = TestServer::new().await;
    let open_notif = server
        .open(
            "save_semantic.php",
            "<?php\nfunction _wrap(): void {\n    nonexistent_fn();\n}\n",
        )
        .await;
    expect!["2:4-2:20 [1] UndefinedFunction: Function nonexistent_fn() is not defined"]
        .assert_eq(&render_diagnostics_notification(&open_notif));

    let save_notif = server.save("save_semantic.php").await;
    expect!["2:4-2:20 [1] UndefinedFunction: Function nonexistent_fn() is not defined"]
        .assert_eq(&render_diagnostics_notification(&save_notif));
}

// --- willSave ---
//
// `willSave` is a void notification — the spec lets the server do nothing,
// and that's exactly what this server does (formatting on save is wired
// through `willSaveWaitUntil` instead). The tests below pin that behaviour:
// the handler must never crash, never mutate the buffer, never publish
// diagnostics, and never disturb adjacent lifecycle handlers.

#[tokio::test]
async fn will_save_keeps_document_state_unchanged() {
    // Open a file with a known semantic diagnostic, fire `willSave` for all
    // three `TextDocumentSaveReason` values (1=Manual, 2=AfterDelay,
    // 3=FocusOut), then trigger `didSave` and snapshot the diagnostics.
    // If `willSave` mutated the buffer or invalidated cached analysis the
    // post-save diagnostics would shift; identical-to-on-open proves they
    // didn't.
    let mut server = TestServer::new().await;
    let open_notif = server
        .open(
            "ws_state.php",
            "<?php\nfunction _wrap(): void {\n    nonexistent_fn();\n}\n",
        )
        .await;

    expect!["2:4-2:20 [1] UndefinedFunction: Function nonexistent_fn() is not defined"]
        .assert_eq(&render_diagnostics_notification(&open_notif));

    for reason in [1u32, 2, 3] {
        server.will_save("ws_state.php", reason).await;
    }

    let save_notif = server.save("ws_state.php").await;
    expect!["2:4-2:20 [1] UndefinedFunction: Function nonexistent_fn() is not defined"]
        .assert_eq(&render_diagnostics_notification(&save_notif));
}

#[tokio::test]
async fn will_save_does_not_publish_diagnostics() {
    // willSave must not trigger a publishDiagnostics — that's didSave's job.
    // If it did, editors that send willSave on every focus-out would see
    // diagnostic flicker.
    let mut server = TestServer::new().await;
    server
        .open("ws_nodiag.php", "<?php\nfunction foo() {}\n")
        .await;

    for reason in [1u32, 2, 3] {
        server.will_save("ws_nodiag.php", reason).await;
    }

    // Round-trip a request to ensure any notification willSave *might* have
    // produced has had a chance to traverse the channel before we drain.
    let hover = server.hover("ws_nodiag.php", 1, 10).await;
    assert!(hover["error"].is_null(), "hover errored: {hover:?}");

    let uris = server
        .client()
        .drain_publish_diagnostics_uris(tokio::time::Duration::from_millis(100))
        .await;
    expect!["[]"].assert_eq(&format!("{uris:?}"));
}

#[tokio::test]
async fn will_save_for_unopened_file_does_not_crash() {
    // The LSP spec only requires clients to send willSave for open documents,
    // but a misbehaving client (or a race against didClose) could send it
    // for an unknown URI. The handler must be tolerant — we verify by
    // confirming the server still produces correct diagnostics afterwards.
    let mut server = TestServer::new().await;

    server.will_save("ws_never_opened.php", 1).await;
    server.will_save("ws_never_opened.php", 2).await;
    server.will_save("ws_never_opened.php", 3).await;

    let open_notif = server
        .open(
            "ws_after.php",
            "<?php\nfunction _wrap(): void {\n    nonexistent_fn();\n}\n",
        )
        .await;
    expect!["2:4-2:20 [1] UndefinedFunction: Function nonexistent_fn() is not defined"]
        .assert_eq(&render_diagnostics_notification(&open_notif));
}

#[tokio::test]
async fn will_save_after_did_close_does_not_crash() {
    // Race: editor closes the file, then a queued willSave from the previous
    // save attempt arrives. The handler must not panic.
    let mut server = TestServer::new().await;
    server
        .open("ws_closed.php", "<?php\nfunction foo() {}\n")
        .await;
    server.close("ws_closed.php").await;
    let _ = server
        .client()
        .drain_publish_diagnostics_uris(tokio::time::Duration::from_millis(50))
        .await;

    server.will_save("ws_closed.php", 1).await;

    // Sanity: server still serves new opens correctly.
    let open_notif = server.open("ws_after_close.php", "<?php\n").await;
    expect!["<empty>"].assert_eq(&render_diagnostics_notification(&open_notif));
}

#[tokio::test]
async fn will_save_does_not_disturb_pending_did_change() {
    // willSave between didChange and the resulting diagnostic publish must
    // not cancel or alter the pending parse — the editor relies on the
    // diagnostic for the latest version landing.
    let mut server = TestServer::new().await;
    server.open("ws_change.php", "<?php\n").await;

    // didChange schedules a debounced re-parse; willSave fires while it's
    // in-flight.
    server
        .change(
            "ws_change.php",
            2,
            "<?php\nfunction _wrap(): void {\n    nonexistent_fn();\n}\n",
        )
        .await;
    server.will_save("ws_change.php", 1).await;

    let save_notif = server.save("ws_change.php").await;
    expect!["2:4-2:20 [1] UndefinedFunction: Function nonexistent_fn() is not defined"]
        .assert_eq(&render_diagnostics_notification(&save_notif));
}

// --- willSaveWaitUntil ---
//
// `willSaveWaitUntil` is a request that returns formatting edits to be applied
// before save. If no formatter is available, it returns null. Otherwise it returns
// a TextEdit array with the formatting changes.

#[tokio::test]
async fn will_save_wait_until_returns_null_or_empty_for_formatted_file() {
    let mut server = TestServer::new().await;
    server.open("wswu_clean.php", "<?php\n").await;

    let resp = server.will_save_wait_until("wswu_clean.php").await;
    assert!(resp["error"].is_null(), "unexpected error: {resp:?}");

    expect![r#"(no formatter available)"#].assert_eq(&render_text_edits(&resp));
}

#[tokio::test]
async fn will_save_wait_until_on_already_formatted_code() {
    let mut server = TestServer::new().await;
    server
        .open(
            "wswu_formatted.php",
            "<?php\n\nfunction greet(): void\n{\n}\n",
        )
        .await;

    let resp = server.will_save_wait_until("wswu_formatted.php").await;
    assert!(resp["error"].is_null(), "unexpected error: {resp:?}");

    // If a formatter is available and code is already clean, it returns
    // null or no changes needed; both are valid per LSP spec
    let result = &resp["result"];
    assert!(
        result.is_null() || result.as_array().map(|a| a.is_empty()).unwrap_or(false),
        "expected null or empty edits for already-formatted file: {resp:?}"
    );
}

#[tokio::test]
async fn will_save_wait_until_returns_edits_or_null_for_unformatted_file() {
    let mut server = TestServer::new().await;
    server
        .open("wswu_ugly.php", "<?php\nfunction ugly( $x ){return $x;}\n")
        .await;

    let resp = server.will_save_wait_until("wswu_ugly.php").await;
    assert!(resp["error"].is_null(), "unexpected error: {resp:?}");

    let result = &resp["result"];
    if let Some(edits) = result.as_array() {
        // Formatter is available; verify the edit structure
        for edit in edits {
            assert!(
                edit["range"]["start"].is_object() && edit["range"]["end"].is_object(),
                "edit missing range: {edit:?}"
            );
            assert!(
                edit["newText"].is_string(),
                "edit missing newText: {edit:?}"
            );
        }
    } else {
        assert!(result.is_null(), "expected null or array, got: {result:?}");
    }
}

#[tokio::test]
async fn will_save_wait_until_on_unopened_file_returns_null() {
    // If the file is not open in the editor, willSaveWaitUntil should still
    // handle it gracefully (even though LSP spec says it's for open documents).
    let mut server = TestServer::new().await;

    let resp = server.will_save_wait_until("wswu_never_opened.php").await;
    assert!(resp["error"].is_null(), "unexpected error: {resp:?}");

    // Result should be null because the file is not in the document store
    expect!["(no formatter available)"].assert_eq(&render_text_edits(&resp));
}

#[tokio::test]
async fn will_save_wait_until_on_empty_file() {
    let mut server = TestServer::new().await;
    server.open("wswu_empty.php", "").await;

    let resp = server.will_save_wait_until("wswu_empty.php").await;
    assert!(resp["error"].is_null(), "unexpected error: {resp:?}");

    // Empty file should return null or no edits needed
    expect!["(no formatter available)"].assert_eq(&render_text_edits(&resp));
}

#[tokio::test]
async fn will_save_wait_until_without_php_tag() {
    // PHP snippets without <?php tag should be handled (formatter adds wrapper)
    let mut server = TestServer::new().await;
    server.open("wswu_no_tag.php", "function test( ){}\n").await;

    let resp = server.will_save_wait_until("wswu_no_tag.php").await;
    assert!(resp["error"].is_null(), "unexpected error: {resp:?}");

    // Should return null or edits depending on formatter availability
    let result = &resp["result"];
    assert!(
        result.is_null() || result.as_array().is_some(),
        "expected null or TextEdit array, got: {result:?}"
    );
}

// --- didChange ---

#[tokio::test]
async fn did_change_updates_document() {
    let mut server = TestServer::new().await;
    server.open("change.php", "<?php\n").await;

    server
        .change("change.php", 2, "<?php\nfunction updated() {}\n")
        .await;

    let resp = server.hover("change.php", 1, 10).await;

    assert!(
        resp["error"].is_null(),
        "hover after change should not error"
    );
}

// --- endpoint wiring ---

#[tokio::test]
async fn document_link_returns_array() {
    let mut server = TestServer::new().await;
    server
        .open("dlink.php", "<?php\nrequire_once 'vendor/autoload.php';\n")
        .await;

    let resp = server.document_link("dlink.php").await;

    assert!(resp["error"].is_null(), "documentLink error: {:?}", resp);
    let links = resp["result"]
        .as_array()
        .expect("documentLink must return an array");
    let out = if links.is_empty() {
        "<empty>".to_owned()
    } else {
        links
            .iter()
            .map(|l| {
                let start = &l["range"]["start"];
                let line = start["line"].as_u64().unwrap_or(0);
                let col = start["character"].as_u64().unwrap_or(0);
                let target = l["target"].as_str().unwrap_or("?");
                // Replace absolute file path with a stable placeholder.
                let display = if let Some(rest) = target.rfind('/').map(|i| &target[i + 1..]) {
                    rest.to_owned()
                } else {
                    target.to_owned()
                };
                format!("{line}:{col} -> {display}")
            })
            .collect::<Vec<_>>()
            .join("\n")
    };
    expect!["1:14 -> autoload.php"].assert_eq(&out);
}

// --- cross-file analysis cache invalidation ---
//
// When a dependency's method return type changes (without changing the
// dependent's own diagnostics), the dependent's mir-resolved cross-file variable
// type must refresh. mir-analyzer 0.31.0 fixed this: `ingest_file` now evicts
// dependents' cached analysis on a content change, so the stale result is no
// longer replayed across incremental re-analysis.

/// Regression guard for cross-file type freshness after a dependency edit.
#[tokio::test]
async fn dependency_edit_refreshes_cross_file_hover_type() {
    let mut server = TestServer::new().await;
    server
        .open(
            "maker.php",
            "<?php\nclass Maker { public function make(): Apple { return new Apple(); } }\nclass Apple {}\nclass Banana {}\n",
        )
        .await;
    server
        .open(
            "use_maker.php",
            "<?php\n$m = new Maker();\n$x = $m->make();\necho $x;\n",
        )
        .await;

    // Hover `$x` (the use on line 3) — its type comes from Maker::make() in the
    // other file.
    let before = server.hover("use_maker.php", 3, 5).await;
    expect!["`$x` `Apple`"].assert_eq(&render_hover(&before));

    // Change ONLY maker.php so make() returns Banana; use_maker.php is untouched.
    server
        .change(
            "maker.php",
            2,
            "<?php\nclass Maker { public function make(): Banana { return new Banana(); } }\nclass Apple {}\nclass Banana {}\n",
        )
        .await;

    let after = server.hover("use_maker.php", 3, 5).await;
    expect!["`$x` `Banana`"].assert_eq(&render_hover(&after));
}

/// Same cross-file freshness guard as the hover test, but for the *completion*
/// surface (a separate code path that also reads `cached_analysis`).
#[tokio::test]
async fn dependency_edit_refreshes_cross_file_completion_members() {
    let mut server = TestServer::new().await;
    server
        .open(
            "dep.php",
            "<?php\nclass Maker { public function make(): Alpha { return new Alpha(); } }\nclass Alpha { public function alpha(): void {} }\nclass Beta { public function beta(): void {} }\n",
        )
        .await;
    server
        .open(
            "uses.php",
            "<?php\n$m = new Maker();\n$x = $m->make();\n$x->\n",
        )
        .await;

    let before = server.completion("uses.php", 3, 4).await;
    expect!["Method      alpha"].assert_eq(&render_completion(&before));

    // make() now returns Beta; uses.php is untouched.
    server
        .change(
            "dep.php",
            2,
            "<?php\nclass Maker { public function make(): Beta { return new Beta(); } }\nclass Alpha { public function alpha(): void {} }\nclass Beta { public function beta(): void {} }\n",
        )
        .await;

    let after = server.completion("uses.php", 3, 4).await;
    expect!["Method      beta"].assert_eq(&render_completion(&after));
}