phpantom_lsp 0.7.0

Fast PHP language server with deep type intelligence. Generics, Laravel, PHPStan annotations. Ready in an instant.
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
use crate::common::create_test_backend;
use tower_lsp::LanguageServer;
use tower_lsp::lsp_types::*;

/// Helper: open a file and request completion at the given line/character.
async fn complete_at(
    backend: &phpantom_lsp::Backend,
    uri: &Url,
    text: &str,
    line: u32,
    character: u32,
) -> Vec<CompletionItem> {
    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri: uri.clone() },
            position: Position { line, character },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    match backend.completion(completion_params).await.unwrap() {
        Some(CompletionResponse::Array(items)) => items,
        Some(CompletionResponse::List(list)) => list.items,
        _ => vec![],
    }
}

/// Returns `true` when the item carries the `DEPRECATED` tag.
fn is_tagged_deprecated(item: &CompletionItem) -> bool {
    item.tags
        .as_ref()
        .is_some_and(|t| t.contains(&CompletionItemTag::DEPRECATED))
}

// ─── Deprecated method ──────────────────────────────────────────────────────

/// A method marked `@deprecated` in its PHPDoc should carry the
/// `DEPRECATED` tag in its completion item.
#[tokio::test]
async fn test_deprecated_method_is_marked() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_method.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Mailer {\n",
        "    /**\n",
        "     * @deprecated Use sendAsync() instead.\n",
        "     */\n",
        "    public function sendLegacy(): void {}\n",
        "\n",
        "    public function sendAsync(): void {}\n",
        "\n",
        "    public function run(): void {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    );

    // Cursor after `$this->` on line 10
    let items = complete_at(&backend, &uri, text, 10, 15).await;

    let legacy = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("sendLegacy"));
    assert!(legacy.is_some(), "Should find sendLegacy in completions");
    assert!(
        is_tagged_deprecated(legacy.unwrap()),
        "sendLegacy should be tagged deprecated"
    );

    let async_method = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("sendAsync"));
    assert!(
        async_method.is_some(),
        "Should find sendAsync in completions"
    );
    assert!(
        !is_tagged_deprecated(async_method.unwrap()),
        "sendAsync should NOT be tagged deprecated"
    );
}

// ─── Non-deprecated method ──────────────────────────────────────────────────

/// A method without `@deprecated` should NOT be tagged deprecated.
#[tokio::test]
async fn test_non_deprecated_method_is_not_marked() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///non_deprecated_method.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Service {\n",
        "    /**\n",
        "     * @return void\n",
        "     */\n",
        "    public function doWork(): void {}\n",
        "\n",
        "    public function run(): void {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 8, 15).await;

    let work = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("doWork"));
    assert!(work.is_some(), "Should find doWork in completions");
    assert!(
        !is_tagged_deprecated(work.unwrap()),
        "doWork should NOT be tagged deprecated"
    );
}

// ─── Deprecated property ────────────────────────────────────────────────────

/// A property marked `@deprecated` in its PHPDoc should carry the
/// `DEPRECATED` tag in its completion item.
#[tokio::test]
async fn test_deprecated_property_is_marked() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_property.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Config {\n",
        "    /**\n",
        "     * @deprecated Use getDebugMode() instead.\n",
        "     */\n",
        "    public bool $debug = false;\n",
        "\n",
        "    public string $name = 'app';\n",
        "\n",
        "    public function test(): void {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 10, 15).await;

    let debug = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("debug"));
    assert!(debug.is_some(), "Should find debug in completions");
    assert!(
        is_tagged_deprecated(debug.unwrap()),
        "debug property should be tagged deprecated"
    );

    let name = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("name"));
    assert!(name.is_some(), "Should find name in completions");
    assert!(
        !is_tagged_deprecated(name.unwrap()),
        "name property should NOT be tagged deprecated"
    );
}

// ─── Deprecated constant ────────────────────────────────────────────────────

/// A class constant marked `@deprecated` in its PHPDoc should carry the
/// `DEPRECATED` tag in its completion item.
#[tokio::test]
async fn test_deprecated_constant_is_marked() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_constant.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class HttpStatus {\n",
        "    /**\n",
        "     * @deprecated Use OK instead.\n",
        "     */\n",
        "    const SUCCESS = 200;\n",
        "\n",
        "    const OK = 200;\n",
        "}\n",
        "$x = HttpStatus::\n",
    );

    // Cursor after `HttpStatus::` on line 9
    let items = complete_at(&backend, &uri, text, 9, 17).await;

    let success = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("SUCCESS"));
    assert!(success.is_some(), "Should find SUCCESS in completions");
    assert!(
        is_tagged_deprecated(success.unwrap()),
        "SUCCESS constant should be tagged deprecated"
    );

    let ok = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("OK"));
    assert!(ok.is_some(), "Should find OK in completions");
    assert!(
        !is_tagged_deprecated(ok.unwrap()),
        "OK constant should NOT be tagged deprecated"
    );
}

// ─── Deprecated function ────────────────────────────────────────────────────

/// A standalone function marked `@deprecated` in its PHPDoc should carry
/// the `DEPRECATED` tag in its completion item.
#[tokio::test]
async fn test_deprecated_function_is_marked() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_function.php").unwrap();
    let text = concat!(
        "<?php\n",
        "/**\n",
        " * @deprecated Use newHelper() instead.\n",
        " */\n",
        "function oldHelper(): void {}\n",
        "\n",
        "function newHelper(): void {}\n",
        "\n",
        "Helper\n",
    );

    // Cursor at end of `Helper` on line 8 — matches both oldHelper and newHelper
    let items = complete_at(&backend, &uri, text, 8, 6).await;

    let old = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("oldHelper"));
    assert!(
        old.is_some(),
        "Should find oldHelper in completions. Got: {:?}",
        items.iter().map(|i| i.label.as_str()).collect::<Vec<_>>()
    );
    assert!(
        is_tagged_deprecated(old.unwrap()),
        "oldHelper should be tagged deprecated"
    );

    let new = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("newHelper"));
    assert!(
        new.is_some(),
        "Should find newHelper in completions. Got: {:?}",
        items.iter().map(|i| i.label.as_str()).collect::<Vec<_>>()
    );
    assert!(
        !is_tagged_deprecated(new.unwrap()),
        "newHelper should NOT be tagged deprecated"
    );
}

// ─── Deprecated method with bare @deprecated ────────────────────────────────

/// A method with a bare `@deprecated` tag (no description) should still
/// be tagged deprecated.
#[tokio::test]
async fn test_deprecated_method_bare_tag() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_bare.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /**\n",
        "     * @deprecated\n",
        "     */\n",
        "    public function oldMethod(): void {}\n",
        "\n",
        "    public function test(): void {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 8, 15).await;

    let old = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("oldMethod"));
    assert!(old.is_some(), "Should find oldMethod in completions");
    assert!(
        is_tagged_deprecated(old.unwrap()),
        "oldMethod should be tagged deprecated even with bare @deprecated tag"
    );
}

// ─── Multiple deprecated members ────────────────────────────────────────────

/// When a class has multiple deprecated and non-deprecated members,
/// only the deprecated ones should be tagged.
#[tokio::test]
async fn test_multiple_deprecated_and_non_deprecated_members() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///multi_deprecated.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Api {\n",
        "    /**\n",
        "     * @deprecated Use fetchV2() instead.\n",
        "     */\n",
        "    public function fetchV1(): array { return []; }\n",
        "\n",
        "    public function fetchV2(): array { return []; }\n",
        "\n",
        "    /**\n",
        "     * @deprecated\n",
        "     */\n",
        "    public string $legacyUrl = '';\n",
        "\n",
        "    public string $baseUrl = '';\n",
        "\n",
        "    /**\n",
        "     * @deprecated Use VERSION_2 instead.\n",
        "     */\n",
        "    const VERSION_1 = 1;\n",
        "\n",
        "    const VERSION_2 = 2;\n",
        "}\n",
        "class Client {\n",
        "    public function run(Api $api): void {\n",
        "        $api->\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 25, 14).await;

    // Deprecated items
    let fetch_v1 = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("fetchV1"));
    assert!(fetch_v1.is_some(), "Should find fetchV1");
    assert!(
        is_tagged_deprecated(fetch_v1.unwrap()),
        "fetchV1 should be tagged deprecated"
    );

    let legacy_url = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("legacyUrl"));
    assert!(legacy_url.is_some(), "Should find legacyUrl");
    assert!(
        is_tagged_deprecated(legacy_url.unwrap()),
        "legacyUrl should be tagged deprecated"
    );

    // Non-deprecated items
    let fetch_v2 = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("fetchV2"));
    assert!(fetch_v2.is_some(), "Should find fetchV2");
    assert!(
        !is_tagged_deprecated(fetch_v2.unwrap()),
        "fetchV2 should NOT be tagged deprecated"
    );

    let base_url = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("baseUrl"));
    assert!(base_url.is_some(), "Should find baseUrl");
    assert!(
        !is_tagged_deprecated(base_url.unwrap()),
        "baseUrl should NOT be tagged deprecated"
    );
}

/// Constant completion via `::` should also respect deprecated tags.
#[tokio::test]
async fn test_deprecated_constant_via_double_colon() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_const_dc.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Api {\n",
        "    /**\n",
        "     * @deprecated Use VERSION_2 instead.\n",
        "     */\n",
        "    const VERSION_1 = 1;\n",
        "\n",
        "    const VERSION_2 = 2;\n",
        "}\n",
        "$v = Api::\n",
    );

    let items = complete_at(&backend, &uri, text, 9, 10).await;

    let v1 = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("VERSION_1"));
    assert!(v1.is_some(), "Should find VERSION_1");
    assert!(
        is_tagged_deprecated(v1.unwrap()),
        "VERSION_1 should be tagged deprecated"
    );

    let v2 = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("VERSION_2"));
    assert!(v2.is_some(), "Should find VERSION_2");
    assert!(
        !is_tagged_deprecated(v2.unwrap()),
        "VERSION_2 should NOT be tagged deprecated"
    );
}

// ─── Deprecated with other docblock tags ─────────────────────────────────────

/// `@deprecated` mixed with `@param`, `@return`, etc. should still be detected.
#[tokio::test]
async fn test_deprecated_mixed_with_other_tags() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_mixed.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Processor {\n",
        "    /**\n",
        "     * Process the given data.\n",
        "     *\n",
        "     * @param array $data The data to process.\n",
        "     * @deprecated since 3.0, use processV2() instead.\n",
        "     * @return bool\n",
        "     */\n",
        "    public function process(array $data): bool { return true; }\n",
        "\n",
        "    public function test(): void {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 12, 15).await;

    let process = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("process"));
    assert!(process.is_some(), "Should find process in completions");
    assert!(
        is_tagged_deprecated(process.unwrap()),
        "process should be tagged deprecated even with mixed tags"
    );
}

// ─── Static deprecated method ───────────────────────────────────────────────

/// Deprecated static methods should be tagged when accessed via `::`.
#[tokio::test]
async fn test_deprecated_static_method() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_static.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Factory {\n",
        "    /**\n",
        "     * @deprecated Use createNew() instead.\n",
        "     */\n",
        "    public static function createLegacy(): self { return new self(); }\n",
        "\n",
        "    public static function createNew(): self { return new self(); }\n",
        "}\n",
        "Factory::\n",
    );

    let items = complete_at(&backend, &uri, text, 9, 10).await;

    let legacy = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("createLegacy"));
    assert!(legacy.is_some(), "Should find createLegacy in completions");
    assert!(
        is_tagged_deprecated(legacy.unwrap()),
        "createLegacy should be tagged deprecated"
    );

    let new_method = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("createNew"));
    assert!(new_method.is_some(), "Should find createNew in completions");
    assert!(
        !is_tagged_deprecated(new_method.unwrap()),
        "createNew should NOT be tagged deprecated"
    );
}

// ─── Deprecated static property ─────────────────────────────────────────────

/// Deprecated static properties should be tagged when accessed via `::`.
#[tokio::test]
async fn test_deprecated_static_property() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_static_prop.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Settings {\n",
        "    /**\n",
        "     * @deprecated Use $newDefault instead.\n",
        "     */\n",
        "    public static string $oldDefault = 'legacy';\n",
        "\n",
        "    public static string $newDefault = 'modern';\n",
        "}\n",
        "Settings::\n",
    );

    let items = complete_at(&backend, &uri, text, 9, 11).await;

    let old = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("$oldDefault"));
    assert!(
        old.is_some(),
        "Should find $oldDefault in completions. Got: {:?}",
        items
            .iter()
            .map(|i| (&i.label, &i.filter_text))
            .collect::<Vec<_>>()
    );
    assert!(
        is_tagged_deprecated(old.unwrap()),
        "$oldDefault should be tagged deprecated"
    );

    let new = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("$newDefault"));
    assert!(new.is_some(), "Should find $newDefault in completions");
    assert!(
        !is_tagged_deprecated(new.unwrap()),
        "$newDefault should NOT be tagged deprecated"
    );
}

// ─── No false positive on similar words ─────────────────────────────────────

/// A docblock mentioning "deprecated" in prose (not as a tag) should NOT
/// cause the member to be flagged.
#[tokio::test]
async fn test_deprecated_word_in_prose_not_flagged() {
    let backend = create_test_backend();
    let uri = Url::parse("file:///deprecated_prose.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Docs {\n",
        "    /**\n",
        "     * This method replaces the deprecated v1 API.\n",
        "     */\n",
        "    public function replaceOld(): void {}\n",
        "\n",
        "    public function test(): void {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    );

    let items = complete_at(&backend, &uri, text, 8, 15).await;

    let method = items
        .iter()
        .find(|i| i.filter_text.as_deref() == Some("replaceOld"));
    assert!(method.is_some(), "Should find replaceOld in completions");
    assert!(
        !is_tagged_deprecated(method.unwrap()),
        "replaceOld should NOT be tagged deprecated — 'deprecated' in prose is not a tag"
    );
}