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
use crate::common::{create_psr4_workspace, create_test_backend};
use tower_lsp::LanguageServer;
use tower_lsp::lsp_types::*;

#[tokio::test]
async fn test_completion_inside_namespaced_class() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///namespaced.php").unwrap();
    let text = concat!(
        "<?php\n",
        "namespace App\\Models;\n",
        "\n",
        "class User {\n",
        "    public function login() {}\n",
        "    public function logout() {}\n",
        "    public function test() {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    )
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$this->` on line 7
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 7,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    assert!(
        result.is_some(),
        "Completion should return results for namespaced class"
    );

    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let method_items: Vec<&CompletionItem> = items
                .iter()
                .filter(|i| i.kind == Some(CompletionItemKind::METHOD))
                .collect();
            assert_eq!(method_items.len(), 3, "Should return 3 method completions");

            let filter_texts: Vec<&str> = method_items
                .iter()
                .map(|i| i.filter_text.as_deref().unwrap())
                .collect();
            assert!(filter_texts.contains(&"login"), "Should contain 'login'");
            assert!(filter_texts.contains(&"logout"), "Should contain 'logout'");

            for item in &method_items {
                assert_eq!(item.kind, Some(CompletionItemKind::METHOD));
            }
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

#[tokio::test]
async fn test_completion_namespaced_class_with_properties_and_methods() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///ns_full.php").unwrap();
    let text = concat!(
        "<?php\n",
        "namespace App\\Entity;\n",
        "\n",
        "class Product {\n",
        "    public string $name;\n",
        "    public float $price;\n",
        "    public function getName(): string {}\n",
        "    public function setPrice(float $price): void {}\n",
        "    public function test() {\n",
        "        $this->\n",
        "    }\n",
        "}\n",
    )
    .to_string();

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text,
        },
    };
    backend.did_open(open_params).await;

    // Cursor right after `$this->` on line 9
    let completion_params = CompletionParams {
        text_document_position: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri },
            position: Position {
                line: 9,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
        context: None,
    };

    let result = backend.completion(completion_params).await.unwrap();
    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let method_items: Vec<&CompletionItem> = items
                .iter()
                .filter(|i| i.kind == Some(CompletionItemKind::METHOD))
                .collect();
            let property_items: Vec<&CompletionItem> = items
                .iter()
                .filter(|i| i.kind == Some(CompletionItemKind::PROPERTY))
                .collect();

            assert_eq!(method_items.len(), 3, "Should have 3 methods");
            assert_eq!(property_items.len(), 2, "Should have 2 properties");

            // Check method insert texts
            let get_name = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("getName"))
                .unwrap();
            assert_eq!(get_name.insert_text.as_deref(), Some("getName()$0"));
            assert_eq!(get_name.insert_text_format, Some(InsertTextFormat::SNIPPET));
            assert_eq!(get_name.label, "getName()");

            let set_price = method_items
                .iter()
                .find(|i| i.filter_text.as_deref() == Some("setPrice"))
                .unwrap();
            assert_eq!(
                set_price.insert_text.as_deref(),
                Some("setPrice(${1:\\$price})$0")
            );
            assert_eq!(
                set_price.insert_text_format,
                Some(InsertTextFormat::SNIPPET)
            );
            assert_eq!(set_price.label, "setPrice($price)");

            // Check property labels
            let prop_labels: Vec<&str> = property_items.iter().map(|i| i.label.as_str()).collect();
            assert!(prop_labels.contains(&"name"));
            assert!(prop_labels.contains(&"price"));
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

// ─── Use-As Alias Resolution ────────────────────────────────────────────────

/// `use Swagger\OpenAPI as OA;` followed by `new OA\Endpoint()` should
/// resolve `OA\Endpoint` to `Swagger\OpenAPI\Endpoint` and offer its
/// members for completion.
#[tokio::test]
async fn test_completion_use_as_alias_same_file() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///use_alias.php").unwrap();
    let text = concat!(
        "<?php\n",                                                  // 0
        "namespace Swagger\\OpenAPI;\n",                            // 1
        "\n",                                                       // 2
        "class Endpoint {\n",                                       // 3
        "    public function getPath(): string { return ''; }\n",   // 4
        "    public function getMethod(): string { return ''; }\n", // 5
        "}\n",                                                      // 6
    );

    let consumer_uri = Url::parse("file:///consumer.php").unwrap();
    let consumer_text = concat!(
        "<?php\n",                                   // 0
        "use Swagger\\OpenAPI as OA;\n",             // 1
        "\n",                                        // 2
        "class App {\n",                             // 3
        "    public function run(): void {\n",       // 4
        "        $endpoint = new OA\\Endpoint();\n", // 5
        "        $endpoint->\n",                     // 6
        "    }\n",                                   // 7
        "}\n",                                       // 8
    );

    // Open both files so the class is in the AST map
    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: consumer_uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: consumer_text.to_string(),
            },
        })
        .await;

    // Cursor after `$endpoint->` on line 6
    let result = backend
        .completion(CompletionParams {
            text_document_position: TextDocumentPositionParams {
                text_document: TextDocumentIdentifier { uri: consumer_uri },
                position: Position {
                    line: 6,
                    character: 20,
                },
            },
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
            context: None,
        })
        .await
        .unwrap();

    assert!(
        result.is_some(),
        "Should return completions for OA\\Endpoint resolved via use-as alias"
    );

    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let labels: Vec<&str> = items.iter().map(|i| i.label.as_str()).collect();
            assert!(
                labels.iter().any(|l| l.starts_with("getPath")),
                "Should include getPath from Endpoint, got: {:?}",
                labels
            );
            assert!(
                labels.iter().any(|l| l.starts_with("getMethod")),
                "Should include getMethod from Endpoint, got: {:?}",
                labels
            );
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

/// Cross-file PSR-4: `use App\Services as Svc;` alias resolving to a class
/// loaded from a PSR-4 mapped file.
#[tokio::test]
async fn test_completion_use_as_alias_cross_file_psr4() {
    let composer_json = r#"{
        "autoload": {
            "psr-4": {
                "App\\Services\\": "src/Services/"
            }
        }
    }"#;

    let service_content = concat!(
        "<?php\n",
        "namespace App\\Services;\n",
        "\n",
        "class PaymentGateway {\n",
        "    public function charge(int $amount): bool { return true; }\n",
        "    public function refund(int $amount): bool { return true; }\n",
        "}\n",
    );

    let consumer_content = concat!(
        "<?php\n",                            // 0
        "use App\\Services as Svc;\n",        // 1
        "\n",                                 // 2
        "$gw = new Svc\\PaymentGateway();\n", // 3
        "$gw->\n",                            // 4
    );

    let (backend, _dir) = create_psr4_workspace(
        composer_json,
        &[("src/Services/PaymentGateway.php", service_content)],
    );

    let uri = Url::parse("file:///consumer_psr4.php").unwrap();
    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: consumer_content.to_string(),
            },
        })
        .await;

    // Cursor after `$gw->` on line 4
    let result = backend
        .completion(CompletionParams {
            text_document_position: TextDocumentPositionParams {
                text_document: TextDocumentIdentifier { uri },
                position: Position {
                    line: 4,
                    character: 5,
                },
            },
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
            context: None,
        })
        .await
        .unwrap();

    assert!(
        result.is_some(),
        "Should return completions for Svc\\PaymentGateway resolved via use-as alias + PSR-4"
    );

    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let labels: Vec<&str> = items.iter().map(|i| i.label.as_str()).collect();
            assert!(
                labels.iter().any(|l| l.starts_with("charge")),
                "Should include charge() from PaymentGateway, got: {:?}",
                labels
            );
            assert!(
                labels.iter().any(|l| l.starts_with("refund")),
                "Should include refund() from PaymentGateway, got: {:?}",
                labels
            );
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

/// `use Foo\Bar as FB;` with a class alias (not namespace alias).
/// `$x = new FB();` should resolve to `Foo\Bar` and offer its members.
#[tokio::test]
async fn test_completion_use_as_class_alias() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///class_alias_def.php").unwrap();
    let text = concat!(
        "<?php\n",                                                  // 0
        "namespace Foo;\n",                                         // 1
        "\n",                                                       // 2
        "class Bar {\n",                                            // 3
        "    public function doWork(): void {}\n",                  // 4
        "    public function getStatus(): string { return ''; }\n", // 5
        "}\n",                                                      // 6
    );

    let consumer_uri = Url::parse("file:///class_alias_use.php").unwrap();
    let consumer_text = concat!(
        "<?php\n",               // 0
        "use Foo\\Bar as FB;\n", // 1
        "\n",                    // 2
        "$x = new FB();\n",      // 3
        "$x->\n",                // 4
    );

    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: consumer_uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: consumer_text.to_string(),
            },
        })
        .await;

    // Cursor after `$x->` on line 4
    let result = backend
        .completion(CompletionParams {
            text_document_position: TextDocumentPositionParams {
                text_document: TextDocumentIdentifier { uri: consumer_uri },
                position: Position {
                    line: 4,
                    character: 4,
                },
            },
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
            context: None,
        })
        .await
        .unwrap();

    assert!(
        result.is_some(),
        "Should return completions for FB resolved via use-as class alias"
    );

    match result.unwrap() {
        CompletionResponse::Array(items) => {
            let labels: Vec<&str> = items.iter().map(|i| i.label.as_str()).collect();
            assert!(
                labels.iter().any(|l| l.starts_with("doWork")),
                "Should include doWork from Bar, got: {:?}",
                labels
            );
            assert!(
                labels.iter().any(|l| l.starts_with("getStatus")),
                "Should include getStatus from Bar, got: {:?}",
                labels
            );
        }
        _ => panic!("Expected CompletionResponse::Array"),
    }
}

/// Go-to-definition on a method accessed via an aliased namespace should
/// resolve correctly.
#[tokio::test]
async fn test_goto_definition_use_as_alias() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///alias_goto_def.php").unwrap();
    let text = concat!(
        "<?php\n",                                  // 0
        "namespace Vendor\\Lib;\n",                 // 1
        "\n",                                       // 2
        "class Client {\n",                         // 3
        "    public function request(): void {}\n", // 4
        "}\n",                                      // 5
    );

    let consumer_uri = Url::parse("file:///alias_goto_consumer.php").unwrap();
    let consumer_text = concat!(
        "<?php\n",                  // 0
        "use Vendor\\Lib as VL;\n", // 1
        "\n",                       // 2
        "$c = new VL\\Client();\n", // 3
        "$c->request();\n",         // 4
    );

    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: text.to_string(),
            },
        })
        .await;

    backend
        .did_open(DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: consumer_uri.clone(),
                language_id: "php".to_string(),
                version: 1,
                text: consumer_text.to_string(),
            },
        })
        .await;

    // Cursor on `request` in `$c->request();` (line 4)
    let params = GotoDefinitionParams {
        text_document_position_params: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier {
                uri: consumer_uri.clone(),
            },
            position: Position {
                line: 4,
                character: 5,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = backend.goto_definition(params).await.unwrap();
    assert!(
        result.is_some(),
        "Should resolve $c->request() when $c is VL\\Client via use-as alias"
    );

    match result.unwrap() {
        GotoDefinitionResponse::Scalar(location) => {
            assert_eq!(location.uri, uri);
            assert_eq!(
                location.range.start.line, 4,
                "request() is declared on line 4 in Client"
            );
        }
        other => panic!("Expected Scalar location, got: {:?}", other),
    }
}